Skip to content

Commit b6514aa

Browse files
authored
Merge pull request #192 from thejoeker12/main
Cookies fixed hopefully
2 parents 2d4e0c7 + 1bd9dbc commit b6514aa

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

cookiejar/cookiejar.go renamed to cookiejar/cookiejar.go.bk

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,42 @@ import (
4545
"fmt"
4646
"net/http"
4747
"net/http/cookiejar"
48+
"net/url"
4849
"strings"
4950

51+
"github.com/deploymenttheory/go-api-http-client/httpclient"
5052
"github.com/deploymenttheory/go-api-http-client/logger"
5153
"go.uber.org/zap"
5254
)
5355

5456
// SetupCookieJar initializes the HTTP client with a cookie jar if enabled in the configuration.
55-
func SetupCookieJar(client *http.Client, enableCookieJar bool, log logger.Logger) error {
56-
if enableCookieJar {
57+
func SetupCookieJar(client *http.Client, clientConfig httpclient.ClientConfig, log logger.Logger) error {
58+
if clientConfig.ClientOptions.Cookies.EnableCookieJar {
5759
jar, err := cookiejar.New(nil) // nil options use default options
5860
if err != nil {
5961
log.Error("Failed to create cookie jar", zap.Error(err))
6062
return fmt.Errorf("setupCookieJar failed: %w", err) // Wrap and return the error
6163
}
64+
65+
if clientConfig.ClientOptions.Cookies.CustomCookies != nil {
66+
var CookieList []*http.Cookie
67+
CookieList = make([]*http.Cookie, 0)
68+
for k, v := range clientConfig.ClientOptions.Cookies.CustomCookies {
69+
newCookie := &http.Cookie{
70+
Name: k,
71+
Value: v,
72+
}
73+
CookieList = append(CookieList, newCookie)
74+
}
75+
76+
cookieUrl, err := url.Parse(fmt.Sprintf("http://%s.jamfcloud.com"))
77+
if err != nil {
78+
return err
79+
}
80+
81+
jar.SetCookies(cookieUrl, CookieList)
82+
}
83+
6284
client.Jar = jar
6385
}
6486
return nil
File renamed without changes.

httpclient/client.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ like the baseURL, authentication details, and an embedded standard HTTP client.
88
package httpclient
99

1010
import (
11+
"fmt"
1112
"net/http"
13+
"net/http/cookiejar"
14+
"net/url"
1215
"time"
1316

1417
"github.com/deploymenttheory/go-api-http-client/apiintegrations/apihandler"
1518
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
1619
"github.com/deploymenttheory/go-api-http-client/concurrency"
17-
"github.com/deploymenttheory/go-api-http-client/cookiejar"
20+
1821
"github.com/deploymenttheory/go-api-http-client/logger"
1922
"github.com/deploymenttheory/go-api-http-client/redirecthandler"
2023
"go.uber.org/zap"
@@ -156,7 +159,7 @@ func BuildClient(config ClientConfig) (*Client, error) {
156159
}
157160

158161
// Conditionally setup cookie jar
159-
if err := cookiejar.SetupCookieJar(httpClient, config.ClientOptions.Cookies.EnableCookieJar, log); err != nil {
162+
if err := SetupCookieJar(httpClient, config, log); err != nil {
160163
log.Error("Error setting up cookie jar", zap.Error(err))
161164
return nil, err
162165
}
@@ -214,3 +217,35 @@ func BuildClient(config ClientConfig) (*Client, error) {
214217
return client, nil
215218

216219
}
220+
221+
func SetupCookieJar(client *http.Client, clientConfig ClientConfig, log logger.Logger) error {
222+
if clientConfig.ClientOptions.Cookies.EnableCookieJar {
223+
jar, err := cookiejar.New(nil) // nil options use default options
224+
if err != nil {
225+
log.Error("Failed to create cookie jar", zap.Error(err))
226+
return fmt.Errorf("setupCookieJar failed: %w", err) // Wrap and return the error
227+
}
228+
229+
if clientConfig.ClientOptions.Cookies.CustomCookies != nil {
230+
var CookieList []*http.Cookie
231+
CookieList = make([]*http.Cookie, 0)
232+
for k, v := range clientConfig.ClientOptions.Cookies.CustomCookies {
233+
newCookie := &http.Cookie{
234+
Name: k,
235+
Value: v,
236+
}
237+
CookieList = append(CookieList, newCookie)
238+
}
239+
240+
cookieUrl, err := url.Parse(fmt.Sprintf("http://%s.jamfcloud.com"))
241+
if err != nil {
242+
return err
243+
}
244+
245+
jar.SetCookies(cookieUrl, CookieList)
246+
}
247+
248+
client.Jar = jar
249+
}
250+
return nil
251+
}

httpclient/request.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
11-
"github.com/deploymenttheory/go-api-http-client/cookiejar"
1211
"github.com/deploymenttheory/go-api-http-client/headers"
1312
"github.com/deploymenttheory/go-api-http-client/httpmethod"
1413
"github.com/deploymenttheory/go-api-http-client/logger"
@@ -161,7 +160,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
161160
}
162161

163162
// Apply custom cookies if configured
164-
cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, log)
163+
// cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, log)
165164

166165
// Set request headers
167166
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)
@@ -325,7 +324,7 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
325324
}
326325

327326
// Apply custom cookies if configured
328-
cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, log)
327+
// cookiejar.ApplyCustomCookies(req, c.clientConfig.ClientOptions.Cookies.CustomCookies, log)
329328

330329
// Set request headers
331330
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)

0 commit comments

Comments
 (0)