-
Notifications
You must be signed in to change notification settings - Fork 1
/
rates.go
41 lines (34 loc) · 993 Bytes
/
rates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package biteship
import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"log"
"net/http"
"reflect"
)
func (client *Client) GetCourierRates(request *RequestCourierRates) (*ResponseListRatesCouriers, *Error) {
var (
resp = &ResponseListRatesCouriers{}
url = fmt.Sprintf("%s/v1/rates/couriers", client.BiteshipUrl)
jsonRequest = []byte("")
errMarshal error
)
if errValidate := validator.New().Struct(request); errValidate != nil {
return resp, ErrorRequestParam(errValidate)
}
isParamsNil := reflect.ValueOf(request).Kind() == reflect.Ptr && reflect.ValueOf(request).IsNil()
if !isParamsNil {
jsonRequest, errMarshal = json.Marshal(request)
if errMarshal != nil {
log.Println(errMarshal)
return nil, ErrorGo(errMarshal)
}
}
errRequest := client.HttpRequest.Call(http.MethodPost, url, client.SecretKey, bytes.NewBuffer(jsonRequest), resp)
if errRequest != nil {
return resp, errRequest
}
return resp, nil
}