-
Notifications
You must be signed in to change notification settings - Fork 2
/
rates.go
43 lines (34 loc) · 1012 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
42
43
package biteship
import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"log"
"net/http"
"reflect"
)
func (bite *BiteshipImpl) GetRatesCouriers(request *RequestCourierRates) (*ResponseListRatesCouriers, *Error) {
var resp = &ResponseListRatesCouriers{}
validate = validator.New()
errValidate := validate.Struct(request)
if errValidate != nil {
return resp, ErrorRequestParam(errValidate)
}
var url = fmt.Sprintf("%s/v1/rates/couriers", bite.Config.BiteshipUrl)
var errMarshal error
jsonRequest := []byte("")
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 := bite.HttpRequest.Call(http.MethodPost, url, bite.Config.SecretKey, bytes.NewBuffer(jsonRequest), resp)
if errRequest != nil {
return resp, errRequest
}
return resp, nil
}