generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 14
/
http.go
202 lines (174 loc) · 5.19 KB
/
http.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
package store
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"strings"
"github.com/brevdev/brev-cli/pkg/cmd/version"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/featureflag"
resty "github.com/go-resty/resty/v2"
)
type NoAuthHTTPStore struct {
FileStore
noAuthHTTPClient *NoAuthHTTPClient
BasicStore
}
func (f *FileStore) WithNoAuthHTTPClient(c *NoAuthHTTPClient) *NoAuthHTTPStore {
return &NoAuthHTTPStore{*f, c, f.b}
}
// Used if need new instance to customize settings
func (n NoAuthHTTPStore) NewNoAuthHTTPStore() *NoAuthHTTPStore {
return n.WithNoAuthHTTPClient(NewNoAuthHTTPClient(n.noAuthHTTPClient.restyClient.BaseURL))
}
type NoAuthHTTPClient struct {
restyClient *resty.Client
}
func NewNoAuthHTTPClient(brevAPIURL string) *NoAuthHTTPClient {
restyClient := NewRestyClient(brevAPIURL)
return &NoAuthHTTPClient{restyClient}
}
func NewRestyClient(brevAPIURL string) *resty.Client {
restyClient := resty.New()
restyClient.SetBaseURL(brevAPIURL)
restyClient.SetQueryParam("utm_source", "cli")
restyClient.SetQueryParam("cli_version", version.Version)
restyClient.SetQueryParam("os", runtime.GOOS)
return restyClient
}
type AuthHTTPStore struct {
NoAuthHTTPStore
authHTTPClient *AuthHTTPClient
isRefreshTokenHandlerSet bool
BasicStore
}
func (n *NoAuthHTTPStore) GetWindowsDir() (string, error) {
return n.GetWSLHostHomeDir()
}
func (s *AuthHTTPStore) GetWindowsDir() (string, error) {
return s.GetWSLHostHomeDir()
}
func (f *FileStore) WithAuthHTTPClient(c *AuthHTTPClient) *AuthHTTPStore {
// err never returned from GetCurrentWorkspaceID
id, _ := f.GetCurrentWorkspaceID()
if id == "" {
c.restyClient.SetQueryParam("local", "true")
}
na := f.WithNoAuthHTTPClient(NewNoAuthHTTPClient(c.restyClient.BaseURL))
return &AuthHTTPStore{NoAuthHTTPStore: *na, authHTTPClient: c}
}
func (n *NoAuthHTTPStore) WithAuthHTTPClient(c *AuthHTTPClient) *AuthHTTPStore {
// err never returned from GetCurrentWorkspaceID
id, _ := n.GetCurrentWorkspaceID()
if id == "" {
c.restyClient.SetQueryParam("local", "true")
}
return &AuthHTTPStore{NoAuthHTTPStore: *n, authHTTPClient: c}
}
func (n *NoAuthHTTPStore) WithAuth(auth Auth) *AuthHTTPStore {
return n.WithAuthHTTPClient(NewAuthHTTPClient(auth, n.noAuthHTTPClient.restyClient.BaseURL))
}
// Used if need new instance to customize settings
func (s AuthHTTPStore) NewAuthHTTPStore() *AuthHTTPStore {
return s.WithAuth(s.authHTTPClient.auth)
}
func (s *AuthHTTPStore) SetForbiddenStatusRetryHandler(handler func() error) error {
if s.isRefreshTokenHandlerSet { // need to create a way to idempotently set this
return fmt.Errorf("refresh token handler alreay set")
}
attemptsThresh := 1
s.authHTTPClient.restyClient.OnAfterResponse(func(c *resty.Client, r *resty.Response) error {
if r.StatusCode() == http.StatusForbidden && r.Request.Attempt < attemptsThresh+1 {
err := handler()
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
return nil
})
s.authHTTPClient.restyClient.AddRetryCondition(
func(r *resty.Response, e error) bool {
if e != nil {
return false
}
return r.StatusCode() == http.StatusForbidden
})
s.authHTTPClient.restyClient.SetRetryCount(attemptsThresh)
s.isRefreshTokenHandlerSet = true
return nil
}
type AuthHTTPClient struct {
restyClient *resty.Client
auth Auth
}
type Auth interface {
GetAccessToken() (string, error)
}
func (s *AuthHTTPStore) WithStaticHeader(header string, value string) *AuthHTTPStore {
s.authHTTPClient.restyClient.SetHeader(header, value)
return s
}
func NewAuthHTTPClient(auth Auth, brevAPIURL string) *AuthHTTPClient {
restyClient := NewRestyClient(brevAPIURL)
restyClient.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
token, err := auth.GetAccessToken()
if err != nil {
return breverrors.WrapAndTrace(err)
}
r.SetAuthToken(token)
return nil
})
return &AuthHTTPClient{restyClient, auth}
}
type BrevDeployErrorList struct {
Errors []BrevDeployError
}
type BrevDeployError struct {
Kind string `json:"type"`
Message string `json:"message"`
}
type HTTPResponseError struct {
Response *resty.Response
}
func NewHTTPResponseError(response *resty.Response) *HTTPResponseError {
return &HTTPResponseError{
Response: response,
}
}
func (e HTTPResponseError) Error() string {
body := e.Response.Body()
if featureflag.Debug() {
return fmt.Sprintf("%s %s %s", e.Response.Request.URL, e.Response.Status(), body)
}
errors := &BrevDeployErrorList{}
err := json.Unmarshal(body, errors)
if err != nil {
return fmt.Sprintf("%s %s %s", e.Response.Request.URL, e.Response.Status(), body)
}
msg := ""
for _, e := range errors.Errors {
msg = msg + e.Message + "\n"
}
if strings.TrimSpace(msg) == "" {
return fmt.Sprintf("%s %s %s", e.Response.Request.URL, e.Response.Status(), body)
}
return msg
}
func IsNetwork404Or403Error(err error) bool {
return IsNetworkErrorWithStatus(err, []int{404, 403})
}
func IsNetworkErrorWithStatus(err error, statusCodes []int) bool {
switch err := err.(type) {
case *HTTPResponseError:
statusCode := err.Response.StatusCode()
for _, c := range statusCodes {
if c == statusCode {
return true
}
}
return false
default:
return false
}
}