Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -12,19 +13,12 @@ import (
)

var (
baseUrl string
apiVersion string
)

func init() {

baseUrl = "https://api.trackingmore.com/"

baseUrl = "https://api.trackingmore.com/"
apiVersion = "v4"
)

}

func (client *Client) sendApiRequest(ctx context.Context, method, path string, queryParams interface{}, inputData interface{}, resultData interface{}) (*Response, error) {
func (client *Client) sendApiRequest(ctx context.Context, method, path string, queryParams interface{},
inputData interface{}, resultData interface{}) (*Response, error) {
var body io.Reader
if inputData != nil {
jsonData, err := json.Marshal(inputData)
Expand All @@ -34,7 +28,7 @@ func (client *Client) sendApiRequest(ctx context.Context, method, path string, q
body = bytes.NewBuffer(jsonData)
}

requestUrl := baseUrl + apiVersion + path
requestUrl := fmt.Sprintf("%s%s%s", baseUrl, apiVersion, path)
req, err := http.NewRequestWithContext(ctx, method, requestUrl, body)
if err != nil {
return nil, err
Expand All @@ -56,16 +50,20 @@ func (client *Client) sendApiRequest(ctx context.Context, method, path string, q
if err != nil {
return nil, err
}
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()

respBody := new(bytes.Buffer)
respBody.ReadFrom(resp.Body)
_, err = respBody.ReadFrom(resp.Body)
if err != nil {
return nil, err
}

result := &Response{
Meta: Meta{},
Data: resultData,
}

err = json.Unmarshal([]byte(respBody.String()), result)
if err != nil {
return nil, err
Expand All @@ -81,20 +79,25 @@ func addStructParams(params interface{}, values *url.Values) error {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return fmt.Errorf("params must be a struct or a pointer to struct")
return errors.New("params must be a struct or a pointer to struct")
}

typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag.Get("url")
if tag == "" {
tag = field.Name
}
value := val.Field(i).Interface()
if value != reflect.Zero(val.Field(i).Type()).Interface() {

// Optimize the val fetch field reflection code here
valField := val.Field(i)
value := valField.Interface()
if value != reflect.Zero(valField.Type()).Interface() {
v.Add(tag, fmt.Sprintf("%v", value))
}
}

*values = v
return nil
}