This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
142 lines (120 loc) · 3.56 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
/*
Package nbiot-go provides a client for the REST API for Telenor NB-IoT.
All Create* and Update* methods return the created and updated entity, respectively, which may include setting or updating fields.
*/
package nbiot
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Client is a client for Telenor NB-IoT.
type Client struct {
addr string
token string
client http.Client
}
// New creates a new client with the default configuration. The default
// configuration can be specified in a configuration file or through
// environment variables.
func New() (*Client, error) {
address, token, err := addressTokenFromConfig(ConfigFile)
if err != nil {
return nil, err
}
if address == "" {
return nil, fmt.Errorf("No API address. Define %s environment variable or create config in %s", AddressEnvironmentVariable, ConfigFile)
}
if token == "" {
return nil, fmt.Errorf("No API token. Define %s environment variable or create config in %s", TokenEnvironmentVariable, ConfigFile)
}
return NewWithAddr(address, token)
}
// NewWithAddr creates a new client with the specified address and token.
func NewWithAddr(addr, token string) (*Client, error) {
c := &Client{
addr: addr,
token: token,
}
return c, c.ping()
}
// Address returns the client's address.
func (c *Client) Address() string {
return c.addr
}
// SystemDefaults is the system defaults.
type SystemDefaults struct {
DefaultFieldMask *FieldMask `json:"defaultFieldMask,omitempty"`
ForcedFieldMask *FieldMask `json:"forcedFieldMask,omitempty"`
}
// SystemDefaults returns the system defaults.
func (c *Client) SystemDefaults() (SystemDefaults, error) {
var cfg SystemDefaults
err := c.get("/system", &cfg)
return cfg, err
}
func (c *Client) ping() error {
err := c.get("/", nil)
if err, ok := err.(ClientError); ok && err.HTTPStatusCode == http.StatusForbidden {
// A token with restricted access will receive 403 Forbidden from "/"
// but that still indicates a succesful connection.
return nil
}
return err
}
func (c *Client) get(path string, x interface{}) error {
return c.request(http.MethodGet, path, nil, x)
}
func (c *Client) create(path string, x interface{}) error {
return c.request(http.MethodPost, path, x, x)
}
func (c *Client) update(path string, x interface{}) error {
return c.request(http.MethodPatch, path, x, x)
}
func (c *Client) delete(path string) error {
return c.request(http.MethodDelete, path, nil, nil)
}
func (c *Client) request(method, path string, input, output interface{}) error {
body := new(bytes.Buffer)
if input != nil {
if err := json.NewEncoder(body).Encode(input); err != nil {
return err
}
}
req, err := http.NewRequest(method, c.addr+path, body)
if err != nil {
return err
}
req.Header.Set("X-API-Token", c.token)
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return newClientError(resp)
}
if output != nil {
return json.NewDecoder(resp.Body).Decode(output)
}
return nil
}
// ClientError describes what went wrong with a request that otherwise succeeded
// but which resulted in an HTTP status code >= 300.
type ClientError struct {
HTTPStatusCode int
Message string
}
func newClientError(resp *http.Response) ClientError {
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ClientError{resp.StatusCode, err.Error()}
}
return ClientError{resp.StatusCode, string(buf)}
}
func (e ClientError) Error() string {
return fmt.Sprintf("%s: %s", http.StatusText(e.HTTPStatusCode), e.Message)
}