/
client.go
74 lines (65 loc) · 2.04 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
package main
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/tidwall/gjson"
)
type client struct {
httpClient *http.Client
tokenURL string
ratelimitURL string
}
func createClient() client {
return client{httpClient: &http.Client{Timeout: time.Duration(2) * time.Second}, tokenURL: TOKEN_URL, ratelimitURL: RATELIMIT_URL}
}
// getRateLimit returns the value of the headers 'ratelimit-limit' and 'ratelimit-remaining' as json formatted string
func (c *client) getRateLimit(token string) (string, error) {
req, err := http.NewRequest("HEAD", c.ratelimitURL, nil)
if err != nil {
return "", errors.New("error while creating request to get rate limit")
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err := c.httpClient.Do(req)
if err != nil {
return "", errors.New("error while performing http request to get rate limit")
}
defer resp.Body.Close()
if isHTTPResponseCode200(resp) {
return fmt.Sprintf("{\"rate limit\": \"%s\",\n\"rate limit remaining\": \"%s\"}", resp.Header.Get("ratelimit-limit"), resp.Header.Get("ratelimit-remaining")), nil
} else {
log.Printf("Status is: %s\n", resp.Status)
return "", errors.New("rate limit http response was not 200")
}
}
// getClientToken returns a token as string for an unauthenticated user
func (c *client) getClientToken() (string, error) {
resp, err := c.httpClient.Get(c.tokenURL)
if err != nil {
return "", errors.New("error while performing http request to get token")
}
defer resp.Body.Close()
if isHTTPResponseCode200(resp) {
b := readRespBody(resp)
return getJSONKey(string(b), "token"), nil
} else {
log.Printf("Status is: %s\n", resp.Status)
return "", errors.New("token http response was not 200")
}
}
func getJSONKey(json string, key string) string {
return gjson.Get(json, key).String()
}
func isHTTPResponseCode200(resp *http.Response) bool {
return resp.StatusCode == 200
}
func readRespBody(resp *http.Response) []byte {
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("error while reading response body: %v", err)
}
return b
}