-
Notifications
You must be signed in to change notification settings - Fork 4
/
verify.go
53 lines (48 loc) · 1.4 KB
/
verify.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
package thingscloud
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// AccountStatus describes possible thingscloud account statuses
type AccountStatus string
const (
// AccountStatusActive is for active accounts
AccountStatusActive AccountStatus = "SYAccountStatusActive"
)
// VerifyResponse contains details about your account
type VerifyResponse struct {
SLAVersionAccepted string `json:"SLA-version-accepted"`
Issues json.RawMessage `json:"issues"`
Email string `json:"email"`
MaildropEmail string `json:"maildrop-email"`
Status AccountStatus `json:"status"`
HistoryKey string `json:"history-key"`
}
// Verify checks that the provided API credentials are valid.
func (c *Client) Verify() (*VerifyResponse, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("/version/1/account/%s", c.EMail), nil)
req.Header.Set("Authorization", fmt.Sprintf("Password %s", c.password))
if err != nil {
return nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusUnauthorized {
return nil, ErrUnauthorized
}
return nil, fmt.Errorf("http response code: %s", resp.Status)
}
var v VerifyResponse
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
json.Unmarshal(bs, &v)
return &v, nil
}