Skip to content

Commit

Permalink
Enabled trace
Browse files Browse the repository at this point in the history
  • Loading branch information
rhomber committed Aug 10, 2019
1 parent 9569c1a commit 133ce9c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions auspost/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ func NewClient(apiKey string, baseUrl string) *Client {
type Client struct {
restClient *resty.Client
baseUrl string
trace bool
}

func (c *Client) getUrl(uri string) string {
return fmt.Sprintf("%s/%s", c.baseUrl, uri)
}

func (c *Client) EnableTrace() *Client {
c.trace = true

return c
}
36 changes: 35 additions & 1 deletion auspost/postcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auspost

import (
"errors"
"fmt"
"github.com/rhomber/auspost-postcode/auspost/model"
"strconv"
)
Expand All @@ -13,7 +14,13 @@ const (
)

func (c *Client) PostcodeSearch(q string, state string, excludePostboxFlag bool) (model.PostcodeSearchResult, error) {
resp, err := c.restClient.R().
req := c.restClient.R()

if c.trace {
req.EnableTrace()
}

resp, err := req.
SetQueryParams(map[string]string{
"q": q,
"state": state,
Expand All @@ -22,6 +29,33 @@ func (c *Client) PostcodeSearch(q string, state string, excludePostboxFlag bool)
SetResult(model.PostcodeSearchResult{}).
Get(c.getUrl(ausPostUriPostcodeSearch))

if c.trace {
fmt.Println("AusPost API Request")
fmt.Println("===================")
fmt.Println()
fmt.Println("Response Info:")
fmt.Println("Error :", err)
fmt.Println("Status Code:", resp.StatusCode())
fmt.Println("Status :", resp.Status())
fmt.Println("Time :", resp.Time())
fmt.Println("Received At:", resp.ReceivedAt())
fmt.Println("Body :\n", resp)
fmt.Println()

fmt.Println("Request Trace Info:")
ti := resp.Request.TraceInfo()
fmt.Println("DNSLookup :", ti.DNSLookup)
fmt.Println("ConnTime :", ti.ConnTime)
fmt.Println("TLSHandshake :", ti.TLSHandshake)
fmt.Println("ServerTime :", ti.ServerTime)
fmt.Println("ResponseTime :", ti.ResponseTime)
fmt.Println("TotalTime :", ti.TotalTime)
fmt.Println("IsConnReused :", ti.IsConnReused)
fmt.Println("IsConnWasIdle:", ti.IsConnWasIdle)
fmt.Println("ConnIdleTime :", ti.ConnIdleTime)
fmt.Println()
}

if err != nil {
return model.PostcodeSearchResult{}, err
}
Expand Down

0 comments on commit 133ce9c

Please sign in to comment.