Skip to content

Commit

Permalink
clean-ups, add AddInternalHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
atavism committed Jun 18, 2024
1 parent e46036a commit 7361dac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
15 changes: 10 additions & 5 deletions internalsdk/common/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ var (
func AddCommonNonUserHeaders(uc UserConfig, req *http.Request) {
req.Header.Set(AppVersionHeader, ApplicationVersion)
req.Header.Set(LibraryVersionHeader, LibraryVersion)
for k, v := range uc.GetInternalHeaders() {
if v != "" {
req.Header.Set(k, v)
}
}
AddInternalHeaders(uc, req)
if len(uc.GetEnabledExperiments()) > 0 {
req.Header.Set("x-lantern-dev-experiments", strings.Join(uc.GetEnabledExperiments(), ","))
}
Expand All @@ -74,6 +70,15 @@ func AddCommonNonUserHeaders(uc UserConfig, req *http.Request) {
req.Header.Add(RandomNoiseHeader, randomizedString())
}

// AddInternalHeaders adds the internal headers from the user config to the headers of the given http.Request
func AddInternalHeaders(uc UserConfig, req *http.Request) {
for k, v := range uc.GetInternalHeaders() {
if v != "" {
req.Header.Set(k, v)
}
}
}

// AddCommonUserHeaders adds common headers that are user or device specific.
func AddCommonUserHeaders(uc UserConfig, req *http.Request) {
if req.Header.Get(DeviceIdHeader) == "" {
Expand Down
12 changes: 4 additions & 8 deletions internalsdk/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,12 @@ func NewClient(baseURL string, opts *webclient.Opts) ProClient {
client := &proClient{
userConfig: opts.UserConfig,
}
client.webclient = webclient.NewRESTClient(defaultwebclient.SendToURL(httpClient, baseURL, func(rc *resty.Client, req *resty.Request) error {
client.webclient = webclient.NewRESTClient(defaultwebclient.SendToURL(httpClient, baseURL, func(rc *resty.Client, restyReq *resty.Request) error {
uc := client.userConfig()
req := restyReq.RawRequest
req.Header.Set(common.Referer, "http://localhost:37457/")
common.AddCommonUserHeaders(uc, req.RawRequest)
headers := map[string]string{}
// Import all the internal headers
for k, v := range uc.GetInternalHeaders() {
headers[k] = v
}
req.SetHeaders(headers)
common.AddCommonUserHeaders(uc, req)
common.AddInternalHeaders(uc, req)
return nil
}, nil))
return client
Expand Down

0 comments on commit 7361dac

Please sign in to comment.