This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
/
mattercloud.go
101 lines (82 loc) · 2.33 KB
/
mattercloud.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
/*
Package mattercloud is the unofficial golang implementation for the MatterCloud API
*/
package mattercloud
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
)
// NewClient creates a new client to submit requests
// Parameters values are set to the defaults defined by the API documentation.
//
// For more information: https://developers.mattercloud.io/
func NewClient(apiKey string, network NetworkType, clientOptions *Options,
customHTTPClient HTTPInterface) (c ClientInterface, err error) {
// Make sure we have an API key
if len(apiKey) == 0 {
return nil, errors.New("missing required api key")
}
// Create a client using the given options
c = createClient(apiKey, network, clientOptions, customHTTPClient)
return
}
// Request is a generic request wrapper that can be used without constraints
func (c *Client) Request(ctx context.Context, endpoint, method string,
payload []byte) (response string, err error) {
// Set reader
var bodyReader io.Reader
// Add the network value
endpoint = fmt.Sprintf("%s%s/%s", apiEndpoint, c.Parameters.Network, endpoint)
// Switch on Methods
switch method {
case http.MethodPost, http.MethodPut:
{
bodyReader = bytes.NewBuffer(payload)
}
}
// Store for debugging purposes
c.LastRequest.Method = method
c.LastRequest.URL = endpoint
// Start the request
var request *http.Request
if request, err = http.NewRequestWithContext(
ctx, method, endpoint, bodyReader,
); err != nil {
return
}
// Change the header (user agent is in case they block default Go user agents)
request.Header.Set("User-Agent", c.Parameters.UserAgent)
request.Header.Set(apiKeyField, c.Parameters.apiKey)
// Set the content type on Method
if method == http.MethodPost || method == http.MethodPut {
request.Header.Set("Content-Type", "application/json")
}
// Fire the http request
var resp *http.Response
if resp, err = c.httpClient.Do(request); err != nil {
return
}
// Close the response body
defer func() {
_ = resp.Body.Close()
}()
// Save the status
c.LastRequest.StatusCode = resp.StatusCode
// Read the body
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return
}
// Parse the response
response = string(body)
return
}
// Network will return the current network
func (c *Client) Network() NetworkType {
return c.Parameters.Network
}