/
client.go
129 lines (106 loc) · 2.86 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
126
127
128
129
// Copyright (c) 2022, John Moore
// All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package clickup
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
type APITokenAuthenticator struct {
APIToken string
}
// AuthenticateFor adds an API token to the Authorization header of req.
func (d *APITokenAuthenticator) AuthenticateFor(req *http.Request) error {
req.Header.Add("Authorization", d.APIToken)
return nil
}
type ClientOpts struct {
Doer ClientDoer
Authenticator Authenticator
}
type Client struct {
doer ClientDoer
authenticator Authenticator
baseURL string
}
// wrapper for internal authenticator for convenience <shrug>.
func (c *Client) AuthenticateFor(req *http.Request) error {
return c.authenticator.AuthenticateFor(req)
}
const basePath = "https://api.clickup.com/api/v2"
// NewClient initializes and returns a pointer to a Client.
// If opts.Doer is not provided, an http.Client with a 20 seconds timeout is used.
// If opts.Authenticator is not provided, an APITokenAuthenticator is used.
func NewClient(opts *ClientOpts) *Client {
auth := opts.Authenticator
if auth == nil {
auth = &APITokenAuthenticator{}
}
if opts.Doer != nil {
return &Client{
doer: opts.Doer,
authenticator: auth,
baseURL: basePath,
}
}
return &Client{
doer: &http.Client{
Timeout: time.Duration(time.Second * 20),
},
authenticator: auth,
baseURL: basePath,
}
}
func (c *Client) call(ctx context.Context, method, uri string, data *bytes.Buffer, result interface{}) error {
var req *http.Request
var err error
endpoint := fmt.Sprintf("%s%s", c.baseURL, uri)
switch method {
case http.MethodGet:
req, err = http.NewRequestWithContext(ctx, method, endpoint, nil)
if err != nil {
return err
}
case http.MethodPost:
req, err = http.NewRequestWithContext(ctx, method, endpoint, data)
req.Header.Add("Content-Type", "application/json")
if err != nil {
return err
}
case http.MethodPut:
req, err = http.NewRequestWithContext(ctx, method, endpoint, data)
req.Header.Add("Content-Type", "application/json")
if err != nil {
return err
}
case http.MethodDelete:
req, err = http.NewRequestWithContext(ctx, method, endpoint, nil)
if err != nil {
return err
}
default:
return errors.New("unsupported http method")
}
if err := c.AuthenticateFor(req); err != nil {
return fmt.Errorf("failed to authenticate client: %w", err)
}
res, err := c.doer.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
if res.StatusCode != http.StatusOK {
return errorFromResponse(res, decoder)
}
if err := decoder.Decode(result); err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
return nil
}