-
Notifications
You must be signed in to change notification settings - Fork 3
/
rajaongkir.go
209 lines (160 loc) · 4.87 KB
/
rajaongkir.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ro
import (
"errors"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
)
//rajaOngkir entry struct for Raja Ongkir
type rajaOngkir struct {
apiKey string
client *httpRequest
Env Env
}
// New function, create rajaOngkir pointer
// required parameter, your Raja Ongkir API KEY and timeout(http request timeout)
func New(apiKey string, timeout time.Duration) *rajaOngkir {
httpRequest := newRequest(timeout)
return &rajaOngkir{
apiKey: apiKey,
client: httpRequest,
//set default env to starter,
//latter just simply change with rajaOngkir.Env = Basic
Env: Starter,
}
}
//call function
func (r *rajaOngkir) call(method, path string, body io.Reader, v interface{}, headers map[string]string) error {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
headers["key"] = r.apiKey
path = r.Env.String() + path
return r.client.exec(method, path, body, v, headers)
}
//GetProvince
func (r *rajaOngkir) GetProvince(q QueryRequest) ServiceResult {
//make headers map
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
var (
//path
path string
//responseWrapper single result
responseWrapper struct {
Rajaongkir struct {
Query interface{} `json:"query"`
Status Status `json:"status"`
Results Province `json:"results"`
} `json:"rajaongkir"`
}
//responseWrapperList list result
responseWrapperList struct {
Rajaongkir struct {
Query interface{} `json:"query"`
Status Status `json:"status"`
Results []Province `json:"results"`
} `json:"rajaongkir"`
}
)
if q.ProvinceID != "" {
path = fmt.Sprintf("province?id=%s", q.ProvinceID)
err := r.call("GET", path, nil, &responseWrapper, headers)
if err != nil {
return ServiceResult{Error: err}
}
return ServiceResult{Result: responseWrapper.Rajaongkir.Results}
} else {
path = "province"
err := r.call("GET", path, nil, &responseWrapperList, headers)
if err != nil {
return ServiceResult{Error: err}
}
return ServiceResult{Result: responseWrapperList.Rajaongkir.Results}
}
return ServiceResult{Error: errors.New("Invalid Request")}
}
//GetCity
func (r *rajaOngkir) GetCity(q QueryRequest) ServiceResult {
//make headers map
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
var (
//path
path string
//responseWrapper single result
responseWrapper struct {
Rajaongkir struct {
Query interface{} `json:"query"`
Status Status `json:"status"`
Results City `json:"results"`
} `json:"rajaongkir"`
}
//responseWrapperList list result
responseWrapperList struct {
Rajaongkir struct {
Query interface{} `json:"query"`
Status Status `json:"status"`
Results []City `json:"results"`
} `json:"rajaongkir"`
}
)
if q.CityID != "" && q.ProvinceID == "" {
path = fmt.Sprintf("city?id=%s", q.CityID)
err := r.call("GET", path, nil, &responseWrapper, headers)
if err != nil {
return ServiceResult{Error: err}
}
return ServiceResult{Result: responseWrapper.Rajaongkir.Results}
} else if q.CityID != "" && q.ProvinceID != "" {
path = fmt.Sprintf("city?id=%s&province=%s", q.CityID, q.ProvinceID)
err := r.call("GET", path, nil, &responseWrapper, headers)
if err != nil {
return ServiceResult{Error: err}
}
return ServiceResult{Result: responseWrapper.Rajaongkir.Results}
} else if q.CityID == "" && q.ProvinceID != "" {
path = fmt.Sprintf("city?province=%s", q.ProvinceID)
err := r.call("GET", path, nil, &responseWrapperList, headers)
if err != nil {
return ServiceResult{Error: err}
}
return ServiceResult{Result: responseWrapperList.Rajaongkir.Results}
} else {
path = "city"
err := r.call("GET", path, nil, &responseWrapperList, headers)
if err != nil {
return ServiceResult{Error: err}
}
return ServiceResult{Result: responseWrapperList.Rajaongkir.Results}
}
return ServiceResult{Error: errors.New("Invalid Request")}
}
//GetCost
func (r *rajaOngkir) GetCost(q QueryRequest) ServiceResult {
//make headers map
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
if q.Origin != "" && q.Destination != "" && q.Weight > 0 && q.Courier != "" {
var costWrapper CostWrapper
query := url.Values{}
query.Set("origin", q.Origin)
query.Set("destination", q.Destination)
weightString := strconv.Itoa(q.Weight)
query.Set("weight", weightString)
query.Set("courier", q.Courier)
err := r.call("POST", "cost", strings.NewReader(query.Encode()), &costWrapper, headers)
if err != nil {
return ServiceResult{Error: err}
}
var cost Cost
cost.OriginDetails = costWrapper.Rajaongkir.OriginDetails
cost.DestinationDetails = costWrapper.Rajaongkir.DestinationDetails
cost.Providers = costWrapper.Rajaongkir.Results
return ServiceResult{Result: cost}
}
return ServiceResult{Error: errors.New("Invalid Request")}
}