-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.go
More file actions
182 lines (163 loc) · 3.91 KB
/
Copy pathapi.go
File metadata and controls
182 lines (163 loc) · 3.91 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
package webapi
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)
// API is the main object of the web api
type API struct {
server string
secure bool
public *Public
userAgent string
errorGen func(msg string, statusCode int) error
http http.Client
}
// NewAPI creates a new API object
func NewAPI(server string, https bool, userAgent string) *API {
if userAgent == "" {
userAgent = "webapi/1.0"
}
q := &API{
server: server,
secure: https,
userAgent: userAgent,
http: http.Client{
Transport: &http.Transport{
MaxIdleConns: 3,
IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: 60 * time.Second,
DisableKeepAlives: false,
},
Timeout: 30 * time.Second,
},
}
q.public = newPublic(q)
return q
}
// EventAPI returns an EventAPI for the given event
func (q *API) EventAPI(eventID string) *EventAPI {
return NewEventAPI(eventID, q)
}
// Public returns the endpoint group for public servers
func (q *API) Public() *Public {
return q.public
}
// General returns the endpoint group for general functions
func (q *API) General() *General {
return newGeneral(q)
}
// SetTimeout sets the timeout for all following requests. Default is 30 seconds.
func (q *API) SetTimeout(timeout time.Duration) {
q.http.Timeout = timeout
}
// GetTimeout returns the current request timeout
func (q *API) GetTimeout() time.Duration {
return q.http.Timeout
}
// SetErrorGen sets a custom function used to create errors
func (q *API) SetErrorGen(fn func(msg string, statusCode int) error) {
q.errorGen = fn
}
// SessionID returns the session ID
func (q *API) SessionID() string {
return q.public.sessionID
}
// SetSessionID sets the session ID
func (q *API) SetSessionID(sessID string) {
q.public.sessionID = sessID
}
// get makes a GET request on the server
func (q *API) get(eventID, cmd string, values urlValues) ([]byte, error) {
req, err := http.NewRequest("GET", q.buildURL(eventID, cmd, values), nil)
if err != nil {
return nil, err
}
return q.do(req)
}
// post makes a POST request on the server
func (q *API) post(eventID, cmd string, values urlValues, contentType string, data interface{}) ([]byte, error) {
var reader io.Reader
if data != nil {
switch d := data.(type) {
case []byte:
reader = bytes.NewReader(d)
case string:
reader = strings.NewReader(d)
case io.Reader:
reader = d
default:
btsData, err := json.Marshal(data)
if err != nil {
return nil, err
}
reader = bytes.NewReader(btsData)
}
}
// make request
req, err := http.NewRequest("POST", q.buildURL(eventID, cmd, values), reader)
if err != nil {
return nil, err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
return q.do(req)
}
// do executes a http request
func (q *API) do(req *http.Request) ([]byte, error) {
// make request
req.Header.Set("Authorization", "Bearer "+q.public.sessionID)
req.Header.Set("User-Agent", q.userAgent)
resp, err := q.http.Do(req)
if err != nil {
return nil, err
}
// read response
defer func() { _ = resp.Body.Close() }()
bts, err := ioutil.ReadAll(resp.Body)
// success=
if resp.StatusCode == 200 {
return bts, err
}
// error happened
var errMsg string
var e struct {
Error string
}
if json.Unmarshal(bts, &e) == nil {
errMsg = e.Error
} else {
errMsg = string(bts)
}
if q.errorGen != nil {
return nil, q.errorGen(errMsg, resp.StatusCode)
}
return nil, errors.New(errMsg)
}
// buildURL compiles the url for any request
func (q *API) buildURL(eventID, cmd string, values urlValues) string {
var sb strings.Builder
if q.secure {
sb.WriteString("https://")
} else {
sb.WriteString("http://")
}
sb.WriteString(q.server)
if eventID != "" {
sb.WriteString("/_")
sb.WriteString(eventID)
}
sb.WriteString("/api/")
sb.WriteString(cmd)
if len(values) != 0 {
sb.WriteString("?")
sb.WriteString(values.URLEncode())
}
return sb.String()
}