-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
122 lines (94 loc) · 2.42 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
package melhorenvio
import (
"context"
"io"
"net/http"
"sync"
"time"
)
type Environment string
const (
SandboxApiUrl = "https://sandbox.melhorenvio.com.br"
ProductionApiUrl = "https://melhorenvio.com.br"
)
type CredentialsChangedCallback = func(credentials Credentials) error
type Credentials struct {
ClientId int32
ClientSecret string
AccessToken string
RefreshToken string
ExpiresAt time.Time
Code string
}
type Config struct {
Credentials Credentials
ApiUrl string
RedirectUri string
ApplicationName string
Email string
CredentialsChangedCallback CredentialsChangedCallback
}
type Client struct {
context context.Context
config Config
httpClient *http.Client
initialized bool
mutex sync.Mutex
}
func NewClient(ctx context.Context, config Config) *Client {
c := &Client{}
c.context = ctx
c.config = config
if c.config.ApiUrl == "" {
c.config.ApiUrl = SandboxApiUrl
}
c.httpClient = http.DefaultClient
c.initialized = true
return c
}
func (c *Client) injectDefaultHeaders(req *http.Request) {
if req == nil {
return
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", c.config.ApplicationName+" ("+c.config.Email+")")
}
func (c *Client) doRequest(req *http.Request) (*http.Response, error) {
// faz a requisição, já injetando a autenticação e gerenciando o processo de refresh de token
// ao dar retry por conta da autenticação, pode dar erro se o body do request não for um dos tipos
// que é possível fazer o retry (ex: bytes.Buffer)
c.injectDefaultHeaders(req)
if c.config.Credentials.ExpiresAt.Before(time.Now()) {
err := c.RefreshToken()
if err != nil {
return nil, err
}
}
req.Header.Set("Authorization", "Bearer "+c.config.Credentials.AccessToken)
response, err := c.httpClient.Do(req)
if err != nil {
return response, err
}
switch response.StatusCode {
case http.StatusUnauthorized:
io.Copy(io.Discard, response.Body)
response.Body.Close()
err = c.RefreshToken()
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.config.Credentials.AccessToken)
response, err = c.httpClient.Do(req)
if err != nil {
return response, err
}
// don't close body
// probably not needed, but I'll check anyway
switch response.StatusCode {
case http.StatusUnauthorized:
return nil, ErrInvalidToken
}
}
return response, err
}