-
Notifications
You must be signed in to change notification settings - Fork 0
/
inpost_api_client.go
266 lines (214 loc) · 6.17 KB
/
inpost_api_client.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
const userAgent = "InPost-Mobile/3.19.0-release (iOS 17.2; iPhone14,2; pl)"
const apiHost = "api-inmobile-pl.easypack24.net"
type InPostAPIClient struct {
configFilePath string
authToken string
refreshToken string
client *http.Client
baseURL *url.URL
getConfig func() []byte
setConfig func([]byte)
}
type APIError struct {
Status int
Error string
Description string
}
type Pollutant struct {
Value float32
Percent float32
}
type Point struct {
Name string
AirSensor bool
AirSensorData struct {
AirQuality string
Weather struct {
Temperature float32
Pressure float32
Humidity float32
}
Pollutants struct {
PM10 Pollutant
PM25 Pollutant
}
UpdatedUntil time.Time
}
}
type Config struct {
RefreshToken string `json:"refreshToken"`
AuthToken string `json:"authToken"`
}
func NewInPostAPIClient(getConfig func() []byte, setConfig func([]byte)) *InPostAPIClient {
inpost := new(InPostAPIClient)
inpost.client = &http.Client{Timeout: 10 * time.Second}
inpost.baseURL = &url.URL{
Scheme: "https",
Host: apiHost,
}
inpost.getConfig = getConfig
inpost.setConfig = setConfig
inpost.ReadConfig()
return inpost
}
func (inpost *InPostAPIClient) GetPoint(pointId string) (*Point, error) {
if !inpost.isAuthTokenValid() && inpost.refreshToken != "" {
err := inpost.Authenticate()
if err != nil {
return nil, err
}
}
endpoint := url.URL{Path: fmt.Sprintf("/v3/points/%s", pointId)}
response, body := inpost.request("GET", &endpoint, nil)
if response.StatusCode != http.StatusOK {
apiErr := APIError{}
json.Unmarshal(body, &apiErr)
if apiErr.Error == "tokenExpiredException" {
err := inpost.Authenticate()
if err != nil {
return nil, err
}
return inpost.GetPoint(pointId)
}
return nil, errors.New(fmt.Sprintf("[%d] %s", response.StatusCode, apiErr.Error))
}
data := new(Point)
json.Unmarshal(body, data)
return data, nil
}
func (inpost *InPostAPIClient) Authenticate() error {
if inpost.refreshToken == "" {
return errors.New("Please log in again (--login).")
}
type RequestPayload struct {
RefreshToken string `json:"refreshToken"`
PhoneOS string `json:"phoneOS"`
}
type Response struct {
AuthToken string
}
endpoint := url.URL{Path: "/v1/authenticate"}
payload := RequestPayload{inpost.refreshToken, "Apple"}
jsonData, _ := json.Marshal(payload)
response, body := inpost.request("POST", &endpoint, bytes.NewBuffer(jsonData))
if response.StatusCode != http.StatusOK {
apiErr := APIError{}
json.Unmarshal(body, &apiErr)
return errors.New(fmt.Sprintf("[%d] %s", response.StatusCode, apiErr.Error))
}
data := Response{}
json.Unmarshal(body, &data)
inpost.authToken = data.AuthToken
inpost.SaveConfig()
return nil
}
func (inpost *InPostAPIClient) SendSMSCode(phoneNumber string) error {
type RequestPayload struct {
PhoneNumber string `json:"phoneNumber"`
}
endpoint := url.URL{Path: "/v1/sendSMSCode"}
payload := RequestPayload{phoneNumber}
jsonData, _ := json.Marshal(payload)
response, body := inpost.request("POST", &endpoint, bytes.NewBuffer(jsonData))
if response.StatusCode != http.StatusOK {
apiErr := APIError{}
json.Unmarshal(body, &apiErr)
return errors.New(fmt.Sprintf("[%d] %s", response.StatusCode, apiErr.Error))
}
return nil
}
func (inpost *InPostAPIClient) ConfirmSMSCode(phoneNumber string, smsCode string) error {
type RequestPayload struct {
PhoneNumber string `json:"phoneNumber"`
SmsCode string `json:"smsCode"`
PhoneOS string `json:"phoneOS"`
}
type Response struct {
RefreshToken string
AuthToken string
}
endpoint := url.URL{Path: "/v1/confirmSMSCode"}
payload := RequestPayload{phoneNumber, smsCode, "Apple"}
jsonData, _ := json.Marshal(payload)
response, body := inpost.request("POST", &endpoint, bytes.NewBuffer(jsonData))
if response.StatusCode != http.StatusOK {
apiErr := APIError{}
json.Unmarshal(body, &apiErr)
return errors.New(fmt.Sprintf("[%d] %s", response.StatusCode, apiErr.Error))
}
data := Response{}
json.Unmarshal(body, &data)
inpost.refreshToken = data.RefreshToken
inpost.authToken = data.AuthToken
inpost.SaveConfig()
return nil
}
func (inpost *InPostAPIClient) SaveConfig() {
text, _ := json.MarshalIndent(Config{inpost.refreshToken, inpost.authToken}, "", " ")
inpost.setConfig(text)
}
func (inpost *InPostAPIClient) ReadConfig() {
text := inpost.getConfig()
config := Config{}
json.Unmarshal(text, &config)
inpost.refreshToken = config.RefreshToken
inpost.authToken = config.AuthToken
}
func (inpost *InPostAPIClient) request(method string, apiURL *url.URL, requestBody io.Reader) (*http.Response, []byte) {
resolvedURL := inpost.baseURL.ResolveReference(apiURL)
req, err := http.NewRequest(method, resolvedURL.String(), requestBody)
if err != nil {
log.Fatalf("Error occurred: %+v", err)
}
req.Header.Set("User-Agent", userAgent)
req.Header.Add("Accept-Language", "en-US")
if method == "POST" || method == "PUT" {
req.Header.Add("Content-Type", "application/json")
}
if inpost.authToken != "" {
req.Header.Add("Authorization", inpost.authToken)
}
response, err := inpost.client.Do(req)
if err != nil {
log.Fatalf("Error sending request to API endpoint: %+v", err)
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("Couldn't parse response body: %+v", err)
}
return response, responseBody
}
func (inpost *InPostAPIClient) isAuthTokenValid() bool {
type TokenPayload struct {
Exp int64
}
if !strings.HasPrefix(inpost.authToken, "Bearer ") {
return false
}
jwt := strings.Replace(inpost.authToken, "Bearer ", "", 1)
encodedTokenPayload := strings.Split(jwt, ".")[1]
jsonData, _ := base64.RawStdEncoding.DecodeString(encodedTokenPayload)
decodedTokenPayload := TokenPayload{}
err := json.Unmarshal(jsonData, &decodedTokenPayload)
if err != nil {
log.Fatalf("Couldn't validate auth token.")
}
tokenExpirationDate := time.Unix(decodedTokenPayload.Exp, 0)
return time.Now().Before(tokenExpirationDate)
}