-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
162 lines (130 loc) · 2.92 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
package smartcharge
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
const (
LibraryVersion = "0.1.2"
DefaultBaseUrl = "https://api.smartcharge.io/"
userAgent = "smartcharge-go/" + LibraryVersion
DefaultAppId = "40a38b82-d7a5-4c0f-bd25-9736bc20a4d2"
DefaultAppToken = ""
DefaultPushState = "unknown"
)
type Client struct {
httpClient *http.Client
BaseURL *url.URL
UserAgent string
Auth *Authentication
ChargePoint *ChargePointService
Session *SessionService
Invoice *InvoiceService
}
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
baseUrl, _ := url.Parse(DefaultBaseUrl)
c := &Client{
httpClient: httpClient,
BaseURL: baseUrl,
UserAgent: userAgent,
}
c.ChargePoint = &ChargePointService{client: c}
c.Session = &SessionService{client: c}
c.Invoice = &InvoiceService{client: c}
return c
}
func (c *Client) Authenticate(email, password string) {
if email == "" {
log.Fatalln("email cannot be empty")
}
if password == "" {
log.Fatalln("password cannot empty")
}
auth, err := authenticate(email, password)
if err != nil {
log.Fatalf("unable to authenticate: %s", err)
}
c.setAuthentication(auth)
}
func (c *Client) setAuthentication(auth *Authentication) {
c.Auth = auth
}
func (c *Client) NewRequest(method string, urlStr string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
u := c.BaseURL.ResolveReference(rel)
bodyReader, ok := body.(io.Reader)
if !ok && body != nil {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
bodyReader = buf
}
req, err := http.NewRequest(method, u.String(), bodyReader)
if err != nil {
return nil, err
}
if c.UserAgent != "" {
req.Header.Add("User-Agent", c.UserAgent)
}
if c.Auth != nil {
req.Header.Add("Authorization", "Bearer "+c.Auth.AccessToken)
}
return req, nil
}
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if v != nil {
defer resp.Body.Close()
}
err = CheckResponse(resp)
if err != nil {
if v == nil {
_ = resp.Body.Close()
}
return resp, err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
_, _ = io.Copy(w, resp.Body)
} else {
err = json.NewDecoder(resp.Body).Decode(v)
}
}
return resp, err
}
// TODO handle 401
func CheckResponse(r *http.Response) error {
c := r.StatusCode
if 200 <= c && c <= 299 {
return nil
}
errBody := ""
if data, err := ioutil.ReadAll(r.Body); err == nil {
errBody = strings.TrimSpace(string(data))
}
errMsg := fmt.Sprintf("HTTP code %v: %q: ", c, r.Status)
if errBody == "" {
errMsg += "no response body"
} else {
errMsg += fmt.Sprintf("response body: %q", errBody)
}
return errors.New(errMsg)
}