-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
138 lines (120 loc) · 2.82 KB
/
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
package patentes_cl
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"time"
)
type Client struct {
httpClient *http.Client
token string
clientUser string
clientPassword string
authUser string
authPassword string
tokenMutex sync.RWMutex
}
func NewClient() *Client {
return &Client{
httpClient: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
clientUser: clientUser,
clientPassword: clientPassword,
authUser: authUser,
authPassword: authPassword,
}
}
func (c *Client) assertAuth() error {
c.tokenMutex.RLock()
tokenIsEmpty := c.token == ""
c.tokenMutex.RUnlock()
if tokenIsEmpty {
err := c.refreshToken()
if err != nil {
return fmt.Errorf("failed to refresh token: %w", err)
}
}
return nil
}
func (c *Client) refreshToken() error {
c.tokenMutex.Lock()
defer c.tokenMutex.Unlock()
if c.token != "" {
return nil
}
reqBodyBytes := fmt.Sprintf("grant_type=password&username=%s&password=%s", c.clientUser, c.clientPassword)
reqBody := bytes.NewBuffer([]byte(reqBodyBytes))
req, err := http.NewRequest("POST", tokenURL, reqBody)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.authUser, c.authPassword)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New("failed to get token")
}
var tokenResp TokenResponse
err = json.NewDecoder(resp.Body).Decode(&tokenResp)
if err != nil {
return err
}
c.token = tokenResp.AccessToken
return nil
}
func (c *Client) GetByRut(rut string) ([]Vehicle, error) {
err := c.assertAuth()
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", vehiclesURL+rut, nil)
if err != nil {
return nil, err
}
var currentToken string
c.tokenMutex.RLock()
currentToken = c.token
c.tokenMutex.RUnlock()
req.Header.Add("Authorization", "bearer "+currentToken)
req.Header.Add("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
// Token might be expired, try to refresh it
c.tokenMutex.RLock()
tokenWasRefreshed := c.token != currentToken
c.tokenMutex.RUnlock()
if !tokenWasRefreshed {
c.tokenMutex.Lock()
c.token = ""
c.tokenMutex.Unlock()
if err := c.refreshToken(); err != nil {
return nil, err
}
}
return c.GetByRut(rut)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to get vehicle information")
}
var vehicles []Vehicle
if err := json.NewDecoder(resp.Body).Decode(&vehicles); err != nil {
return nil, err
}
return vehicles, nil
}