-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevice.go
154 lines (136 loc) · 4.77 KB
/
device.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
package bunq
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"time"
)
type deviceResponse struct {
Response []struct {
DevicePhone *struct {
ID int `json:"id"`
Created Time `json:"created"`
Updated Time `json:"updated"`
Description string `json:"description"`
PhoneNumber string `json:"phone_number"`
OS string `json:"os"`
Status string `json:"status"`
} `json:"DevicePhone,omitempty"`
DeviceServer *struct {
ID int `json:"id"`
Created Time `json:"created"`
Updated Time `json:"updated"`
Description string `json:"description"`
IP string `json:"ip"`
Status string `json:"status"`
} `json:"DeviceServer,omitempty"`
} `json:"Response"`
}
// A DevicePhone represents a Device at the bunq API.
type DevicePhone struct {
ID int
CreatedAt time.Time
UpdatedAt time.Time
Description string
PhoneNumber string
OS string
Status string
}
// ErrDeviceNotFound is returned when a single Device resource was
// not found.
var ErrDeviceNotFound = errors.New("device not found")
// GetDevice gets a Device resource at the bunq API.
func (c *Client) GetDevice(id int) (interface{}, error) {
httpMethod := http.MethodGet
endpoint := apiVersion + "/device/" + strconv.Itoa(id)
req, err := http.NewRequest(httpMethod, fmt.Sprintf("%v/%v", c.BaseURL, endpoint), nil)
if err != nil {
return nil, fmt.Errorf("bunq: could not create new request: %v", err)
}
setCommonHeaders(req)
req.Header.Set("X-Bunq-Client-Authentication", c.Token)
if err = c.addSignature(req, fmt.Sprintf("%v /%v", httpMethod, endpoint), ""); err != nil {
return nil, fmt.Errorf("bunq: could not add signature: %v", err)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("bunq: could not send HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bunq: request was unsuccessful: %v", decodeError(resp.Body))
}
var devResp deviceResponse
if err = json.NewDecoder(resp.Body).Decode(&devResp); err != nil {
return nil, fmt.Errorf("bunq: could not decode HTTP response: %v", err)
}
devices, err := devResp.devices()
if err != nil {
return nil, fmt.Errorf("bunq: could not parse API response: %v", err)
}
if len(devices) == 0 {
return nil, ErrDeviceNotFound
}
return devices[0], nil
}
// ListDevices gets a list of Device resources at the bunq API.
func (c *Client) ListDevices() ([]interface{}, error) {
httpMethod := http.MethodGet
endpoint := apiVersion + "/device"
req, err := http.NewRequest(httpMethod, fmt.Sprintf("%v/%v", c.BaseURL, endpoint), nil)
if err != nil {
return nil, fmt.Errorf("bunq: could not create new request: %v", err)
}
setCommonHeaders(req)
req.Header.Set("X-Bunq-Client-Authentication", c.Token)
if err = c.addSignature(req, fmt.Sprintf("%v /%v", httpMethod, endpoint), ""); err != nil {
return nil, fmt.Errorf("bunq: could not add signature: %v", err)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("bunq: could not send HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bunq: request was unsuccessful: %v", decodeError(resp.Body))
}
var devResp deviceResponse
if err = json.NewDecoder(resp.Body).Decode(&devResp); err != nil {
return nil, fmt.Errorf("bunq: could not decode HTTP response: %v", err)
}
devices, err := devResp.devices()
if err != nil {
return nil, fmt.Errorf("bunq: could not parse API response: %v", err)
}
return devices, nil
}
func (devResp *deviceResponse) devices() ([]interface{}, error) {
devices := make([]interface{}, len(devResp.Response))
for i := range devResp.Response {
if devResp.Response[i].DevicePhone != nil {
device := DevicePhone{}
device.ID = devResp.Response[i].DevicePhone.ID
device.CreatedAt = time.Time(devResp.Response[i].DevicePhone.Created)
device.UpdatedAt = time.Time(devResp.Response[i].DevicePhone.Updated)
device.Description = devResp.Response[i].DevicePhone.Description
device.PhoneNumber = devResp.Response[i].DevicePhone.PhoneNumber
device.OS = devResp.Response[i].DevicePhone.OS
device.Status = devResp.Response[i].DevicePhone.Status
devices[i] = device
}
if devResp.Response[i].DeviceServer != nil {
device := DeviceServer{}
device.ID = devResp.Response[i].DeviceServer.ID
device.Description = devResp.Response[i].DeviceServer.Description
device.CreatedAt = time.Time(devResp.Response[i].DeviceServer.Created)
device.UpdatedAt = time.Time(devResp.Response[i].DeviceServer.Updated)
device.Status = devResp.Response[i].DeviceServer.Status
device.IP = net.ParseIP(devResp.Response[i].DeviceServer.IP)
devices[i] = device
}
}
return devices, nil
}