-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensibo.go
172 lines (145 loc) · 3.66 KB
/
sensibo.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
// Copyright 2021 To Levan Giguashvili. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package sensibo
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sort"
)
// Sensibo holds all of the available functions to interact with the Sensibo API.
type Sensibo struct {
APIKey string
httpClient HTTPClient
}
// HTTPClient interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
// New creates new Sensibo instance.
//
// apiKey is the API key that you got from https://home.sensibo.com/me/api
// To generate an API key just go to https://home.sensibo.com/me/api
// and click on "Add API Key" buttone, fill in the name and it will create the key
//
// httpClient is the client we want to use for http requests (e.g: http.DefaultClient)
//
// It returns a pointed to Sensibo with the key already stored in it
func New(httpClient HTTPClient, apikey string) *Sensibo {
return &Sensibo{
APIKey: apikey,
httpClient: httpClient,
}
}
func (s *Sensibo) getRequestURL(
version string,
endpoint string,
params map[string]string,
) string {
baseURL := fmt.Sprintf("https://home.sensibo.com/api/%s/%s?apiKey=%s", version, endpoint, s.APIKey)
queryKeys := []string{}
queryParams := ""
for k := range params {
queryKeys = append(queryKeys, k)
}
sort.Strings(queryKeys)
for _, k := range queryKeys {
queryParams = fmt.Sprintf("%s&%s=%s", queryParams, k, url.QueryEscape(params[k]))
}
return fmt.Sprintf("%s%s", baseURL, queryParams)
}
func (s *Sensibo) makeRequest(ctx context.Context, method string, url string, body io.Reader) (string, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return "", fmt.Errorf("unable to create new request: \n\t%v", err)
}
req.Header.Set("Content-type", "application/json")
req.Header.Set("accept", "*/*")
res, err := s.httpClient.Do(req)
if err != nil || res.StatusCode != http.StatusOK {
resBytes, ioErr := ioutil.ReadAll(res.Body)
if ioErr != nil {
return "", fmt.Errorf("io error occurred: \n\t%v", ioErr)
}
defer res.Body.Close()
return "", fmt.Errorf(
"failed making request \n\tCode: %v \n\tMsg: %v \n\tErr: %v",
res.StatusCode,
string(resBytes),
err,
)
}
defer res.Body.Close()
resBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: \n\t%v", err)
}
return string(resBytes), nil
}
func (s *Sensibo) makeGetRequest(
ctx context.Context,
version string,
endpoint string,
params map[string]string,
) (string, error) {
return s.makeRequest(ctx,
http.MethodGet,
s.getRequestURL(version, endpoint, params),
nil,
)
}
func (s *Sensibo) makePutRequest(
ctx context.Context,
version string,
endpoint string,
body io.Reader,
) (string, error) {
return s.makeRequest(
ctx,
http.MethodPut,
s.getRequestURL(version, endpoint, map[string]string{}),
body,
)
}
func (s *Sensibo) makePatchRequest(
ctx context.Context,
version string,
endpoint string,
body io.Reader,
) (string, error) {
return s.makeRequest(
ctx,
http.MethodPatch,
s.getRequestURL(version, endpoint, map[string]string{}),
body,
)
}
func (s *Sensibo) makePostRequest(
ctx context.Context,
version string,
endpoint string,
body io.Reader,
) (string, error) {
return s.makeRequest(
ctx,
http.MethodPost,
s.getRequestURL(version, endpoint, map[string]string{}),
body,
)
}
func (s *Sensibo) makeDeleteRequest(
ctx context.Context,
version string,
endpoint string,
) (string, error) {
return s.makeRequest(
ctx,
http.MethodDelete,
s.getRequestURL(version, endpoint, map[string]string{}),
nil,
)
}