This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
forked from zoom-lib-golang/zoom-lib-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
183 lines (147 loc) · 3.63 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
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
183
package zoom
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
"github.com/google/go-querystring/query"
)
const (
apiURI = "api.zoom.us"
apiVersion = "/v2"
)
var (
// Debug causes debugging message to be printed, using the log package,
// when set to true
Debug = false
// APIKey is a package-wide API key, used when no client is instantiated
APIKey string
// APISecret is a package-wide API secret, used when no client is instantiated
APISecret string
defaultClient *Client
)
// Client is responsible for making API requests
type Client struct {
Key string
Secret string
Transport http.RoundTripper
Timeout time.Duration // set to value > 0 to enable a request timeout
endpoint string
}
// NewClient returns a new API client
func NewClient(apiKey string, apiSecret string) *Client {
var uri = url.URL{
Scheme: "https",
Host: apiURI,
Path: apiVersion,
}
return &Client{
Key: apiKey,
Secret: apiSecret,
endpoint: uri.String(),
}
}
type requestV2Opts struct {
Client *Client
Method HTTPMethod
URLParameters interface{}
Path string
DataParameters interface{}
Ret interface{}
// HeadResponse represents responses that don't have a body
HeadResponse bool
}
func initializeDefault(c *Client) *Client {
if c == nil {
if defaultClient == nil {
defaultClient = NewClient(APIKey, APISecret)
}
return defaultClient
}
return c
}
func (c *Client) executeRequest(opts requestV2Opts) (*http.Response, error) {
client := c.httpClient()
req, err := c.addRequestAuth(c.httpRequest(opts))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
return client.Do(req)
}
func (c *Client) httpRequest(opts requestV2Opts) (*http.Request, error) {
var buf bytes.Buffer
// encode body parameters if any
if err := json.NewEncoder(&buf).Encode(&opts.DataParameters); err != nil {
return nil, err
}
// set URL parameters
values, err := query.Values(opts.URLParameters)
if err != nil {
return nil, err
}
// set request URL
requestURL := c.endpoint + opts.Path
if len(values) > 0 {
requestURL += "?" + values.Encode()
}
if Debug {
log.Printf("Request URL: %s", requestURL)
log.Printf("URL Parameters: %s", values.Encode())
log.Printf("Body Parameters: %s", buf.String())
}
// create HTTP request
return http.NewRequest(string(opts.Method), requestURL, &buf)
}
func (c *Client) httpClient() *http.Client {
client := &http.Client{Transport: c.Transport}
if c.Timeout > 0 {
client.Timeout = c.Timeout
}
return client
}
func (c *Client) requestV2(opts requestV2Opts) error {
// make sure the defaultClient is not nil if we are using it
c = initializeDefault(c)
// execute HTTP request
resp, err := c.executeRequest(opts)
if err != nil {
return err
}
// If there is no body in response
if opts.HeadResponse {
return c.requestV2HeadOnly(resp)
}
return c.requestV2WithBody(opts, resp)
}
func (c *Client) requestV2WithBody(opts requestV2Opts, resp *http.Response) error {
// read HTTP response
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if len(body) == 0 {
return nil
}
if Debug {
log.Printf("Response Body: %s", string(body))
}
// check for Zoom errors in the response
if err := checkError(body); err != nil {
return err
}
// unmarshal the response body into the return object
return json.Unmarshal(body, &opts.Ret)
}
func (c *Client) requestV2HeadOnly(resp *http.Response) error {
if resp.StatusCode != 204 {
return errors.New(resp.Status)
}
// there were no errors, just return
return nil
}