-
Notifications
You must be signed in to change notification settings - Fork 0
/
cotacao.go
145 lines (123 loc) · 3.86 KB
/
cotacao.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package melhorenvio
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type ToFrom struct {
PostalCode string `json:"postal_code"`
}
type Dimensions struct {
Height float64 `json:"height"`
Width float64 `json:"width"`
Length float64 `json:"length"`
}
type Product struct {
ID string `json:"id"`
Dimensions
Weight float64 `json:"weight"`
InsuranceValue float64 `json:"insurance_value"`
Quantity int32 `json:"quantity"`
}
type Volume struct {
Dimensions
Weight float64 `json:"weight"`
// tá na api mas aparentemente não faz nada quando usado no cálculo de fretes, então removi do json, mas mantive aqui pra usar em lógicas internas em algumas aplicações
InsuranceValue float64 `json:"-"`
}
type Options struct {
Receipt bool `json:"receipt"`
OwnHand bool `json:"own_hand"`
InsuranceValue float64 `json:"insurance_value,omitempty"` // utilizado no cálculo de frete por volumes
}
type CotacaoRequest struct {
From ToFrom `json:"from"`
To ToFrom `json:"to"`
Products []Product `json:"products,omitempty"`
Volumes []Volume `json:"volumes,omitempty"`
Options Options `json:"options,omitempty"`
}
type DeliveryRange struct {
Min int32 `json:"min"`
Max int32 `json:"max"`
}
type Package struct {
Price string `json:"price"`
Discount string `json:"discount"`
Format string `json:"format"`
Dimensions Dimensions `json:"dimensions"`
Weight json.Number `json:"weight"`
InsuranceValue json.Number `json:"insurance_value"`
Products []Product `json:"products"`
}
type AdditionalService struct {
Receipt bool `json:"receipt"`
OwnHand bool `json:"own_hand"`
Collect bool `json:"collect"`
}
// type Company struct {
// ID int32 `json:"id"`
// Name string `json:"name"`
// Picture string `json:"picture"`
// }
type CotacaoResponse struct {
ID int32 `json:"id"`
Name string `json:"name"`
Price string `json:"price"`
CustomPrice string `json:"custom_price"`
Discount string `json:"discount"`
Currency string `json:"currency"`
DeliveryTime int32 `json:"delivery_time"`
DeliveryRange DeliveryRange `json:"delivery_range"`
CustomDeliveryTime int32 `json:"custom_delivery_time"`
CustomDeliveryRange DeliveryRange `json:"custom_delivery_range"`
Packages []Package `json:"packages"`
AdditionalServices AdditionalService `json:"additional_services"`
Company Company `json:"company"`
}
type CotacaoError struct {
Message string `json:"message"`
Errors map[string][]string `json:"errors"`
}
func (ce *CotacaoError) Error() string {
return "melhor envio: cotacao: " + ce.Message + ": " + fmt.Sprintf("%v", ce.Errors)
}
func (c *Client) CotarFrete(req *CotacaoRequest) ([]*CotacaoResponse, error) {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(req)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequestWithContext(c.context, "POST", c.config.ApiUrl+"/api/v2/me/shipment/calculate", buf)
if err != nil {
return nil, err
}
httpResp, err := c.doRequest(httpReq)
if err != nil {
return nil, err
}
defer httpResp.Body.Close()
body, _ := io.ReadAll(httpResp.Body)
switch httpResp.StatusCode {
case http.StatusOK:
var resp []*CotacaoResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
return resp, nil
case http.StatusUnprocessableEntity:
ret := &CotacaoError{}
err = json.Unmarshal(body, ret)
if err != nil {
return nil, err
}
return nil, ret
case http.StatusUnauthorized:
return nil, ErrInvalidToken
default:
return nil, fmt.Errorf("melhor envio: cotacao: unrecognized response: %v %v", httpResp.StatusCode, string(body))
}
}