-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
141 lines (120 loc) · 3.29 KB
/
Copy pathapi.go
File metadata and controls
141 lines (120 loc) · 3.29 KB
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
package pixela
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// HTTPClient is an interface for making HTTP requests.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// APIBaseURL is Base URL for API requests.
const APIBaseURL = "https://pixe.la"
// APIBaseURLForV1 is Base URL for API version 1 requests.
const APIBaseURLForV1 = APIBaseURL + "/v1"
const (
contentType = "Content-Type"
contentLength = "Content-Length"
userToken = "X-USER-TOKEN"
)
type requestParameter struct {
Method string
URL string
Header map[string]string
Body []byte
}
// Result is Pixela API Result struct.
type Result struct {
Message string `json:"message"`
IsSuccess bool `json:"isSuccess"`
IsRejected bool `json:"isRejected"`
StatusCode int `json:"statusCode"`
}
func newHTTPRequest(ctx context.Context, param *requestParameter) (*http.Request, error) {
req, err := http.NewRequestWithContext(
ctx,
param.Method,
param.URL,
bytes.NewReader(param.Body))
if err != nil {
return &http.Request{}, fmt.Errorf("failed to create http.Request: %w", err)
}
if param.Header != nil {
for k, v := range param.Header {
req.Header.Add(k, v)
}
}
req.Header.Set(contentType, "application/json")
return req, nil
}
func doRequest(ctx context.Context, httpClient HTTPClient, param *requestParameter) ([]byte, int, error) {
retry := retryer{
processFunc: processFunc(ctx, httpClient, param),
maxRetry: getRetryCount(),
}
if err := retry.do(ctx); err != nil {
return []byte{}, 0, err
}
return retry.body, retry.statusCode, nil
}
func processFunc(ctx context.Context, httpClient HTTPClient, param *requestParameter) func(m *retryer) {
return func(m *retryer) {
req, err := newHTTPRequest(ctx, param)
if err != nil {
m.err = fmt.Errorf("failed to create http.Request: %w", err)
return
}
resp, err := httpClient.Do(req)
if err != nil {
m.err = fmt.Errorf("failed http.Client do: %w", err)
return
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
m.err = fmt.Errorf("failed to read response body: %w", err)
return
}
m.body = b
m.statusCode = resp.StatusCode
m.err = nil
}
}
func mustDoRequest(ctx context.Context, httpClient HTTPClient, param *requestParameter) ([]byte, error) {
retry := retryer{
processFunc: processFunc(ctx, httpClient, param),
maxRetry: getRetryCount(),
}
if err := retry.do(ctx); err != nil {
return []byte{}, err
}
if retry.statusCode >= 300 {
return retry.body, fmt.Errorf("failed to call API: %s", string(retry.body))
}
return retry.body, nil
}
func doRequestAndParseResponse(ctx context.Context, httpClient HTTPClient, param *requestParameter) (*Result, error) {
retry := retryer{
processFunc: processFunc(ctx, httpClient, param),
maxRetry: getRetryCount(),
}
if err := retry.do(ctx); err != nil {
return &Result{}, err
}
r, err := parseNormalResponse(retry.body)
if err != nil {
return &Result{}, fmt.Errorf("failed to parse normal response: %w", err)
}
r.StatusCode = retry.statusCode
return r, nil
}
func parseNormalResponse(b []byte) (*Result, error) {
var result Result
if err := json.Unmarshal(b, &result); err != nil {
return &Result{}, fmt.Errorf("failed to unmarshal json: %s: %w", string(b), err)
}
return &result, nil
}