-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
125 lines (98 loc) · 2.75 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
123
124
125
package migagoapi
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"time"
)
const BaseEndpoint = "https://api.migadu.com/v1/"
type Client struct {
User string
Token string
Endpoint string
Domain string
HttpClient *http.Client
}
type ErrorRequestStatus struct {
StatusBody []byte
StatusCode int
}
func (e ErrorRequestStatus) Error() string {
return fmt.Sprintf("status code: %d, response body: %s", e.StatusCode, e.StatusBody)
}
// Returns a new migagoapi client to run requests with.
// Timeout will be used as the client's http timeout duration and will overrule
// any context timeout duration (if longer). When the timeout of the client
// making the request is reached, it is treated as if the context of the
// request has ended.
func NewClient(user, token, endpoint, domain string, timeout *time.Duration) (*Client, error) {
if user == "" {
return nil, errors.New("No username supplied.")
}
if token == "" {
return nil, errors.New("No token supplied.")
}
if domain == "" {
return nil, errors.New("No domain supplied")
}
c := Client{
user,
token,
BaseEndpoint,
domain,
&http.Client{Timeout: time.Duration(time.Second * 120)},
}
if endpoint != "" {
c.Endpoint = endpoint
}
if timeout != nil {
c.HttpClient.Timeout = *timeout
}
return &c, nil
}
func (c *Client) Get(ctx context.Context, path string) ([]byte, error) {
return c.commonMethod(ctx, path, http.NoBody, http.MethodGet)
}
func (c *Client) Post(ctx context.Context, path string, jsonBody []byte) ([]byte, error) {
return c.commonMethod(ctx, path, bytes.NewBuffer(jsonBody), http.MethodPost)
}
func (c *Client) Put(ctx context.Context, path string, jsonBody []byte) ([]byte, error) {
return c.commonMethod(ctx, path, bytes.NewBuffer(jsonBody), http.MethodPut)
}
func (c *Client) Delete(ctx context.Context, path string) ([]byte, error) {
return c.commonMethod(ctx, path, http.NoBody, http.MethodDelete)
}
func (c *Client) commonMethod(
ctx context.Context, path string,
readerBody io.Reader, method string) ([]byte, error) {
url := fmt.Sprintf("%s/domains/%s/%s", c.Endpoint, c.Domain, path)
req, err := http.NewRequestWithContext(ctx, method, url, readerBody)
if err != nil {
return nil, err
}
body, err := c.doReq(req)
if err != nil {
return nil, err
}
return body, nil
}
func (c *Client) doReq(request *http.Request) ([]byte, error) {
request.SetBasicAuth(c.User, c.Token)
request.Header.Add("Content-Type", "application/json")
resp, err := c.HttpClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jsonBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, &ErrorRequestStatus{jsonBody, resp.StatusCode}
}
return jsonBody, nil
}