Skip to content

Commit a4b0d3d

Browse files
authored
feat: [PIPE-26358]: Added Debug Logs for Policy Api Client (#652)
1 parent 8b28e06 commit a4b0d3d

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

harness/policymgmt/client.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"encoding/xml"
1616
"errors"
1717
"fmt"
18+
"github.com/hashicorp/go-retryablehttp"
1819
"io"
1920
"mime/multipart"
2021
"net/http"
@@ -68,9 +69,6 @@ type service struct {
6869
// NewAPIClient creates a new API client. Requires a userAgent string describing your application.
6970
// optionally a custom http.Client to allow for advanced features such as caching.
7071
func NewAPIClient(cfg *Configuration) *APIClient {
71-
if cfg.HTTPClient == nil {
72-
cfg.HTTPClient = http.DefaultClient
73-
}
7472

7573
c := &APIClient{}
7674
c.cfg = cfg
@@ -164,7 +162,7 @@ func parameterToString(obj interface{}, collectionFormat string) string {
164162
}
165163

166164
// callAPI do the request.
167-
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
165+
func (c *APIClient) callAPI(request *retryablehttp.Request) (*http.Response, error) {
168166
return c.cfg.HTTPClient.Do(request)
169167
}
170168

@@ -182,7 +180,7 @@ func (c *APIClient) prepareRequest(
182180
queryParams url.Values,
183181
formParams url.Values,
184182
fileName string,
185-
fileBytes []byte) (localVarRequest *http.Request, err error) {
183+
fileBytes []byte) (localVarRequest *retryablehttp.Request, err error) {
186184

187185
var body *bytes.Buffer
188186

@@ -269,9 +267,9 @@ func (c *APIClient) prepareRequest(
269267

270268
// Generate a new request
271269
if body != nil {
272-
localVarRequest, err = http.NewRequest(method, url.String(), body)
270+
localVarRequest, err = retryablehttp.NewRequest(method, url.String(), body)
273271
} else {
274-
localVarRequest, err = http.NewRequest(method, url.String(), nil)
272+
localVarRequest, err = retryablehttp.NewRequest(method, url.String(), nil)
275273
}
276274
if err != nil {
277275
return nil, err
@@ -308,7 +306,7 @@ func (c *APIClient) prepareRequest(
308306
return nil, err
309307
}
310308

311-
latestToken.SetAuthHeader(localVarRequest)
309+
latestToken.SetAuthHeader(localVarRequest.Request)
312310
}
313311

314312
// Basic HTTP Authentication
@@ -330,17 +328,17 @@ func (c *APIClient) prepareRequest(
330328
}
331329

332330
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
333-
if strings.Contains(contentType, "application/xml") {
334-
if err = xml.Unmarshal(b, v); err != nil {
335-
return err
336-
}
337-
return nil
338-
} else if strings.Contains(contentType, "application/json") {
339-
if err = json.Unmarshal(b, v); err != nil {
340-
return err
341-
}
342-
return nil
331+
if strings.Contains(contentType, "application/xml") {
332+
if err = xml.Unmarshal(b, v); err != nil {
333+
return err
343334
}
335+
return nil
336+
} else if strings.Contains(contentType, "application/json") {
337+
if err = json.Unmarshal(b, v); err != nil {
338+
return err
339+
}
340+
return nil
341+
}
344342
return errors.New("undefined response type")
345343
}
346344

harness/policymgmt/configuration.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
package policymgmt
1010

1111
import (
12-
"net/http"
12+
"fmt"
13+
"github.com/harness/harness-go-sdk/harness"
14+
"github.com/harness/harness-go-sdk/harness/helpers"
15+
"github.com/harness/harness-go-sdk/harness/utils"
16+
"github.com/harness/harness-go-sdk/logging"
17+
"github.com/hashicorp/go-retryablehttp"
18+
log "github.com/sirupsen/logrus"
1319
)
1420

1521
// contextKeys are used to identify the type of value in the context.
@@ -54,14 +60,23 @@ type Configuration struct {
5460
Scheme string `json:"scheme,omitempty"`
5561
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
5662
UserAgent string `json:"userAgent,omitempty"`
57-
HTTPClient *http.Client
63+
HTTPClient *retryablehttp.Client
64+
Logger *log.Logger
65+
DebugLogging bool
5866
}
5967

6068
func NewConfiguration() *Configuration {
69+
logger := logging.NewLogger()
70+
if helpers.EnvVars.TfLog.Get() == "DEBUG" {
71+
logger.SetLevel(log.DebugLevel)
72+
}
73+
6174
cfg := &Configuration{
6275
BasePath: "https://app.harness.io/gateway/pm/",
6376
DefaultHeader: make(map[string]string),
64-
UserAgent: "Swagger-Codegen/1.0.0/go",
77+
HTTPClient: utils.GetDefaultHttpClient(logger),
78+
Logger: logger,
79+
UserAgent: fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion),
6580
}
6681
return cfg
6782
}

0 commit comments

Comments
 (0)