This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
147 lines (107 loc) · 3.03 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
package axcessms
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"golang.org/x/net/context/ctxhttp"
)
// Client represents a full API client instance.
type Client struct {
conn *http.Client
DebugWriter io.Writer
testMode bool
tokenProvider TokenProvider
}
const (
// TestHost is the base API URL to be used for test-mode interactions
TestHost = "https://test.oppwa.com"
// LiveHost is the base API URL to be used for live-mode interactions
LiveHost = "https://oppwa.com"
)
// New creates a new client
func New(ctx context.Context, provider TokenProvider) *Client {
hc := &http.Client{
Transport: http.DefaultTransport,
}
return &Client{
conn: hc,
testMode: false,
tokenProvider: provider,
}
}
// Do performs the given HTTP request on the client
func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", "Axcessms-go +https://github.com/mrzen/axcessms")
token, err := c.tokenProvider()
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)
if c.testMode && c.DebugWriter != nil {
if req.Body != nil {
fmt.Fprintln(c.DebugWriter, "---- REQUEST ----")
cb := new(bytes.Buffer)
tr := io.TeeReader(req.Body, cb)
req.Body = ioutil.NopCloser(tr)
req.Write(c.DebugWriter)
req.Body = ioutil.NopCloser(bytes.NewReader(cb.Bytes()))
}
}
return ctxhttp.Do(ctx, c.conn, req)
}
// URLEncodable is a type which is URL Encodable
type URLEncodable interface {
URLEncode() url.Values
}
// Run sends the given request body as a WWW-Form to the given path, and decodes a JSON response
// into the given interface, returning any error
func (c *Client) Run(ctx context.Context, method, path string, body URLEncodable, into interface{}) error {
var reader io.ReadCloser
if body != nil {
reader = ioutil.NopCloser(bytes.NewBufferString(body.URLEncode().Encode()))
}
req, err := http.NewRequest(method, c.getEndpoint()+path, reader)
req.Header.Set("Accept", "application/json")
if body != nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
}
resp, err := c.Do(ctx, req)
if err != nil {
return err
}
defer resp.Body.Close()
if c.testMode && c.DebugWriter != nil {
cb := new(bytes.Buffer)
tr := io.TeeReader(resp.Body, cb)
resp.Body = ioutil.NopCloser(tr)
fmt.Fprintln(c.DebugWriter, "--- Response")
resp.Write(c.DebugWriter)
resp.Body = ioutil.NopCloser(bytes.NewReader(cb.Bytes()))
}
if resp.StatusCode >= 400 {
errb := APIResponse{}
cb := new(bytes.Buffer)
tr := io.TeeReader(resp.Body, cb)
if err := json.NewDecoder(tr).Decode(&errb); err != nil {
return err
}
return errors.New(errb.Result.Description)
}
return json.NewDecoder(resp.Body).Decode(&into)
}
// SetTestMode sets if the API will communicate in test or production mode
func (c *Client) SetTestMode(test bool) {
c.testMode = test
}
func (c Client) getEndpoint() string {
if c.testMode {
return TestHost
}
return LiveHost
}