Skip to content

Commit

Permalink
Moved template creation and parsing to top level vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ezilber-akamai committed Jul 30, 2024
1 parent 7bdd3de commit 18f6349
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ const (
APIDefaultCacheExpiration = time.Minute * 15
)

var (
reqLogTemplate = template.Must(template.New("request").Parse(`Sending request:

Check failure on line 53 in client.go

View workflow job for this annotation

GitHub Actions / lint-tidy

var `reqLogTemplate` is unused (unused)
Method: {{.Method}}
URL: {{.URL}}
Headers: {{.Headers}}
Body: {{.Body}}`))

respLogTemplate = template.Must(template.New("response").Parse(`Received response:

Check failure on line 59 in client.go

View workflow job for this annotation

GitHub Actions / lint-tidy

var `respLogTemplate` is unused (unused)
Status: {{.Status}}
Headers: {{.Headers}}
Body: {{.Body}}`))
)

var envDebug = false

// Client is a wrapper around the Resty client
Expand Down Expand Up @@ -170,14 +183,8 @@ func (c *httpClient) doRequest(ctx context.Context, method, url string, params R
reqBody = bodyBuffer.String()
}

reqLog := `Sending request:
Method: {{.Method}}
URL: {{.URL}}
Headers: {{.Headers}}
Body: {{.Body}}`
tmpl, _ := template.New("request").Parse(reqLog)
var logBuf bytes.Buffer
err = tmpl.Execute(&logBuf, map[string]interface{}{
err = reqLogTemplate.Execute(&logBuf, map[string]interface{}{
"Method": method,
"URL": url,
"Headers": req.Header,
Expand Down Expand Up @@ -214,13 +221,8 @@ Body: {{.Body}}`
c.logger.Errorf("failed to read response body: %v", err)
}

respLog := `Received response:
Status: {{.Status}}
Headers: {{.Headers}}
Body: {{.Body}}`
tmpl, _ := template.New("response").Parse(respLog)
var logBuf bytes.Buffer
err = tmpl.Execute(&logBuf, map[string]interface{}{
err = respLogTemplate.Execute(&logBuf, map[string]interface{}{
"Status": resp.Status,
"Headers": resp.Header,
"Body": respBody.String(),
Expand Down

0 comments on commit 18f6349

Please sign in to comment.