-
Notifications
You must be signed in to change notification settings - Fork 7
/
method.go
113 lines (97 loc) · 2.66 KB
/
method.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
package sendcloud
import (
"encoding/json"
"strconv"
)
type Method struct {
ID int64
Name string
CarrierCode string
IsServicePoint bool
Amount int64
MinWeight int64
MaxWeight int64
Countries []Country
}
type Country struct {
Code string
Amount int64
}
type MethodListResponseContainer struct {
ShippingMethods []MethodResponse `json:"shipping_methods"`
}
type MethodResponseContainer struct {
ShippingMethod MethodResponse `json:"shipping_method"`
}
type MethodResponse struct {
ServicePointInput string `json:"service_point_input"`
MaxWeight string `json:"max_weight"`
Name string `json:"name"`
Carrier string `json:"carrier"`
Countries []CountryResponse `json:"countries"`
MinWeight string `json:"min_weight"`
ID int64 `json:"id"`
Price float64 `json:"price"`
}
type CountryResponse struct {
Iso2 string `json:"iso_2"`
Iso3 string `json:"iso_3"`
ID int `json:"id"`
Price float64 `json:"price"`
Name string `json:"name"`
LeadTimeHours *float64 `json:"lead_time_hours"`
}
//Get formatted response
func (a *MethodListResponseContainer) GetResponse() interface{} {
var methods []*Method
for _, sm := range a.ShippingMethods {
method := sm.ToMethod()
methods = append(methods, method)
}
return methods
}
//Get formatted response
func (m *MethodResponseContainer) GetResponse() interface{} {
method := m.ShippingMethod.ToMethod()
return method
}
//Set the response
func (m *MethodResponseContainer) SetResponse(body []byte) error {
err := json.Unmarshal(body, &m)
if err != nil {
return err
}
return nil
}
//Set the response
func (a *MethodListResponseContainer) SetResponse(body []byte) error {
err := json.Unmarshal(body, &a)
if err != nil {
return err
}
return nil
}
//Parse methods to a stricter format
func (sm *MethodResponse) ToMethod() *Method {
maxWeightFloat, _ := strconv.ParseFloat(sm.MaxWeight, 64)
maxWeight := int64(maxWeightFloat * 1000)
minWeightFloat, _ := strconv.ParseFloat(sm.MinWeight, 64)
minWeight := int64(minWeightFloat * 1000)
method := &Method{
ID: sm.ID,
Name: sm.Name,
CarrierCode: sm.Carrier,
Amount: int64(sm.Price) * 100,
MinWeight: minWeight,
MaxWeight: maxWeight,
IsServicePoint: sm.ServicePointInput != "none",
}
for _, c := range sm.Countries {
country := Country{
Code: c.Iso2,
Amount: int64(c.Price * 100),
}
method.Countries = append(method.Countries, country)
}
return method
}