forked from lokalise/go-lokalise-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
91 lines (75 loc) · 2.43 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
package lokalise
import (
"context"
"net/http"
"time"
"github.com/go-resty/resty/v2"
)
const (
apiTokenHeader = "X-Api-Token"
defaultBaseURL = "https://api.lokalise.com/api2"
defaultRetryCount = 3
)
type restClient struct {
*resty.Client
timeout time.Duration
baseURL string
apiToken string
retryCount int
}
func newClient(apiToken string) *restClient {
c := restClient{
apiToken: apiToken,
retryCount: defaultRetryCount,
baseURL: defaultBaseURL,
}
c.Client = resty.New().
SetHostURL(c.baseURL).
SetRetryCount(c.retryCount).
SetHeader(apiTokenHeader, c.apiToken).
SetError(errorResponse{}).
AddRetryCondition(requestRetryCondition())
return &c
}
// requestRetryCondition indicates a retry if the HTTP status code of the response
// is >= 500.
// failing requests due to network conditions, eg. "no such host", are handled by resty internally
func requestRetryCondition() resty.RetryConditionFunc {
return func(res *resty.Response, err error) bool {
if res == nil || err != nil {
return true
}
if res.StatusCode() >= http.StatusInternalServerError {
return true
}
return false
}
}
func (c *restClient) get(ctx context.Context, path string, res interface{}) (*resty.Response, error) {
return c.req(ctx, path, res).Get(path)
}
func (c *restClient) getWithOptions(ctx context.Context, path string, res interface{}, options OptionsApplier) (*resty.Response, error) {
req := c.req(ctx, path, res)
options.Apply(req)
return req.Get(path)
}
func (c *restClient) post(ctx context.Context, path string, res, body interface{}) (*resty.Response, error) {
return c.reqWithBody(ctx, path, res, body).Post(path)
}
func (c *restClient) put(ctx context.Context, path string, res, body interface{}) (*resty.Response, error) {
return c.reqWithBody(ctx, path, res, body).Put(path)
}
func (c *restClient) delete(ctx context.Context, path string, res interface{}) (*resty.Response, error) {
return c.req(ctx, path, res).Delete(path)
}
func (c *restClient) deleteWithBody(ctx context.Context, path string, res, body interface{}) (*resty.Response, error) {
return c.reqWithBody(ctx, path, res, body).Delete(path)
}
func (c *restClient) req(ctx context.Context, path string, res interface{}) *resty.Request {
return c.R().
SetResult(&res).
SetContext(ctx)
}
func (c *restClient) reqWithBody(ctx context.Context, path string, res, body interface{}) *resty.Request {
return c.req(ctx, path, res).SetBody(body)
}