-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
203 lines (180 loc) · 4.55 KB
/
Copy pathclient.go
File metadata and controls
203 lines (180 loc) · 4.55 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
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
203
package eramba
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"strings"
"time"
)
const (
PageSize = 200
DefaultTimeoutSeconds = 30
)
type Pagination struct {
HasNextPage bool `json:"has_next_page"`
CurrentPage int32 `json:"current_page"`
}
type responseList[K any] struct {
Data []K `json:"data"`
Pagination Pagination `json:"pagination"`
}
type responseSingle[K any] struct {
Data K `json:"data"`
Pagination Pagination `json:"pagination"`
}
type Client struct {
username string
password string
url string
httpClient *http.Client
}
func New(erambaUrl, username, password string) Client {
return Client{
username: username,
password: password,
url: erambaUrl,
httpClient: &http.Client{Timeout: DefaultTimeoutSeconds * time.Second},
}
}
func (a *Client) BaseUrl() string {
return strings.ReplaceAll(a.url, "/api", "")
}
func (a *Client) getByPath(ctx context.Context, path string) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/%s", a.url, path), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.SetBasicAuth(a.username, a.password)
resp, err := a.httpClient.Do(req)
if err != nil {
return nil, err
}
return resp.Body, nil
}
func (a *Client) postOrPatchJsonByPath(ctx context.Context, method, path string, data []byte) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, method, fmt.Sprintf("%s/%s", a.url, path), bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(a.username, a.password)
resp, err := a.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode >= http.StatusBadRequest {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
slog.Error(fmt.Sprintf("Failed %s", method), "body", body)
return nil, fmt.Errorf("failed to %s", method)
}
return resp.Body, nil
}
func (a *Client) deleteById(ctx context.Context, path string, id int32) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("%s/%s/%d", a.url, path, id), nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
req.SetBasicAuth(a.username, a.password)
resp, err := a.httpClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode >= http.StatusBadRequest {
return errors.New("failed to delete")
}
return nil
}
func getDataById[K any](
ctx context.Context,
path string,
id int32,
getByPath func(ctx context.Context, path string) (io.ReadCloser, error),
) (K, error) {
return getData[K](ctx, fmt.Sprintf("%s/%d", path, id), getByPath)
}
func getData[K any](
ctx context.Context,
path string,
getByPath func(ctx context.Context, path string) (io.ReadCloser, error),
) (K, error) {
res := responseSingle[K]{}
body, err := getByPath(ctx, path)
if err != nil {
return res.Data, err
}
defer body.Close()
err = json.NewDecoder(body).Decode(&res)
if err != nil {
return res.Data, err
}
return res.Data, nil
}
func getAllData[K any](
ctx context.Context,
path string,
getByPath func(ctx context.Context, path string) (io.ReadCloser, error),
) ([]K, error) {
hasNextPage := true
risks := make([]K, 0)
for i := 1; hasNextPage; i++ {
res, err := getDataForPagination[K](ctx, path, i, getByPath)
if err != nil {
return risks, err
}
hasNextPage = res.Pagination.HasNextPage
risks = append(risks, res.Data...)
}
return risks, nil
}
func getDataForPagination[K any](
ctx context.Context,
path string,
i int,
getByPath func(ctx context.Context, path string) (io.ReadCloser, error),
) (responseList[K], error) {
res := responseList[K]{}
body, err := getByPath(ctx, fmt.Sprintf("%s?limit=%d&page=%d", path, PageSize, i))
if err != nil {
return res, err
}
defer body.Close()
err = json.NewDecoder(body).Decode(&res)
if err != nil {
return res, err
}
return res, nil
}
func postOrPatchJsonByPath[K any](
ctx context.Context,
method, path string,
data *K,
postByPath func(ctx context.Context, method, path string, data []byte) (io.ReadCloser, error),
) (*K, error) {
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
body, err := postByPath(ctx, method, path, dataBytes)
if err != nil {
return nil, err
}
defer body.Close()
res := responseSingle[K]{}
err = json.NewDecoder(body).Decode(&res)
if err != nil {
return nil, err
}
return &res.Data, nil
}