This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
128 lines (114 loc) · 3.05 KB
/
api.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
package chatbase
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
var (
client = http.Client{}
)
// SetAPITransport allows setting a transport for the http.Client that is being used
// for handling Chatbase API calls
func SetAPITransport(t http.RoundTripper) {
client.Transport = t
}
// SetAPITimeout allows setting a timeout value for the http.Client that is being used
// for handling Chatbase API calls
func SetAPITimeout(t time.Duration) {
client.Timeout = t
}
func apiCall(method, endpoint string, v interface{}) (io.ReadCloser, error) {
payload, payloadErr := json.Marshal(v)
if payloadErr != nil {
return nil, payloadErr
}
req, reqErr := http.NewRequest(method, endpoint, bytes.NewBuffer(payload))
if reqErr != nil {
return nil, reqErr
}
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode >= http.StatusInternalServerError {
return nil, fmt.Errorf("request failed with status %v", res.StatusCode)
}
return res.Body, nil
}
func apiPost(endpoint string, v interface{}) (io.ReadCloser, error) {
return apiCall(http.MethodPost, endpoint, v)
}
func apiPut(endpoint string, v interface{}) (io.ReadCloser, error) {
return apiCall(http.MethodPut, endpoint, v)
}
func newMessageResponse(thunk func() (io.ReadCloser, error)) (*MessageResponse, error) {
data, err := decodeInto(&MessageResponse{}, thunk)
if res, ok := data.(*MessageResponse); ok {
return res, err
}
if err == nil {
err = fmt.Errorf("%v was not of expected type", data)
}
return nil, err
}
func newMessagesResponse(thunk func() (io.ReadCloser, error)) (*MessagesResponse, error) {
data, err := decodeInto(&MessagesResponse{}, thunk)
if res, ok := data.(*MessagesResponse); ok {
return res, err
}
if err == nil {
err = fmt.Errorf("%v was not of expected type", data)
}
return nil, err
}
func newLinkResponse(thunk func() (io.ReadCloser, error)) (*LinkResponse, error) {
data, err := decodeInto(&LinkResponse{}, thunk)
if res, ok := data.(*LinkResponse); ok {
return res, err
}
if err == nil {
err = fmt.Errorf("%v was not of expected type", data)
}
return nil, err
}
func newUpdateResponse(thunk func() (io.ReadCloser, error)) (*UpdateResponse, error) {
data, err := decodeInto(&UpdateResponse{}, thunk)
if res, ok := data.(*UpdateResponse); ok {
return res, err
}
if err == nil {
err = fmt.Errorf("%v was not of expected type", data)
}
return nil, err
}
func decodeInto(target interface{}, thunk func() (io.ReadCloser, error)) (interface{}, error) {
body, err := thunk()
if err != nil {
return nil, err
}
if body == nil {
return nil, nil
}
defer body.Close()
if err := json.NewDecoder(body).Decode(target); err != nil {
return nil, err
}
return target, nil
}
func augmentURL(endpoint string, params map[string]string) (string, error) {
e, endpointErr := url.Parse(endpoint)
if endpointErr != nil {
return "", endpointErr
}
q := e.Query()
for key, value := range params {
q.Set(key, value)
}
e.RawQuery = q.Encode()
return e.String(), nil
}