Skip to content

Commit 609ec08

Browse files
committed
Refactor HTTP header setting logic into reusable helper function - Add setCommonHeaders helper to centralize Authorization, User-Agent, and Content-Type header logic - Improve maintainability by reducing code duplication across HTTP requests - Preserve existing behavior while enhancing code organization
1 parent 9830a5c commit 609ec08

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

cloudconnexa/cloudconnexa.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ func NewClient(baseURL, clientID, clientSecret string) (*Client, error) {
139139
return c, nil
140140
}
141141

142+
// setCommonHeaders sets the standard headers for API requests.
143+
// It sets Authorization and User-Agent headers, and sets Content-Type to application/json
144+
// if no Content-Type header is already present.
145+
func (c *Client) setCommonHeaders(req *http.Request) {
146+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
147+
req.Header.Set("User-Agent", c.UserAgent)
148+
149+
if req.Header.Get("Content-Type") == "" {
150+
req.Header.Set("Content-Type", "application/json")
151+
}
152+
}
153+
142154
// DoRequest executes an HTTP request with authentication and rate limiting.
143155
// It automatically adds the Bearer token, sets headers, and handles errors.
144156
func (c *Client) DoRequest(req *http.Request) ([]byte, error) {
@@ -147,11 +159,7 @@ func (c *Client) DoRequest(req *http.Request) ([]byte, error) {
147159
return nil, err
148160
}
149161

150-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
151-
req.Header.Set("User-Agent", c.UserAgent)
152-
if req.Header.Get("Content-Type") == "" {
153-
req.Header.Set("Content-Type", "application/json")
154-
}
162+
c.setCommonHeaders(req)
155163

156164
res, err := c.client.Do(req)
157165
if err != nil {

cloudconnexa/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ func (c *SettingsService) getString(path string) (string, error) {
391391
func (c *SettingsService) setString(path string, value string) (string, error) {
392392
endpoint := fmt.Sprintf("%s/settings"+path, c.client.GetV1Url())
393393
req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer([]byte(value)))
394-
req.Header.Set("Content-Type", "text/plain")
395394
if err != nil {
396395
return "", err
397396
}
397+
req.Header.Set("Content-Type", "text/plain")
398398
body, err := c.client.DoRequest(req)
399399
if err != nil {
400400
return "", err

0 commit comments

Comments
 (0)