-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccount.go
149 lines (134 loc) · 3.95 KB
/
account.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
package thingscloud
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type accountRequestBody struct {
Password string `json:"password,omitempty"`
SLAVersionAccepted string `json:"SLA-version-accepted,omitempty"`
ConfirmationCode string `json:"confirmation-code,omitempty"`
}
// AccountService allows account specific interaction with thingscloud
type AccountService service
// Delete deletes your current thingscloud account. This cannot be reversed
func (s *AccountService) Delete() error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("/version/1/account/%s", s.client.EMail), nil)
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Password %s", s.client.password))
resp, err := s.client.do(req)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
if resp.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return fmt.Errorf("http response code: %s", resp.Status)
}
return nil
}
func (s *AccountService) AcceptSLA() error {
data, err := json.Marshal(accountRequestBody{
SLAVersionAccepted: "https://cloud.culturedcode.com/sla/v1.5-rich.html?language=en",
})
if err != nil {
return err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("/version/1/account/%s", s.client.EMail), bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Password %s", s.client.password))
resp, err := s.client.do(req)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return fmt.Errorf("http response code: %s", resp.Status)
}
return nil
}
// Confirm finishes the account creation by providing the email token send by thingscloud
func (s *AccountService) Confirm(code string) error {
data, err := json.Marshal(accountRequestBody{
ConfirmationCode: code,
})
if err != nil {
return err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("/version/1/account/%s", s.client.EMail), bytes.NewBuffer(data))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Password %s", s.client.password))
resp, err := s.client.do(req)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return fmt.Errorf("http response code: %s", resp.Status)
}
return nil
}
// SignUp creates a new thingscloud account and returns a configured client
func (s *AccountService) SignUp(email, password string) (*Client, error) {
data, err := json.Marshal(accountRequestBody{
Password: password,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("/version/1/account/%s", email), bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := s.client.do(req)
if err != nil {
return nil, err
}
resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("http response code: %s", resp.Status)
}
return New(s.client.Endpoint, email, password), nil
}
// ChangePassword allows you to change your account password.
// Because things does not work with sessions you need to create a new client instance after
// executing this method
func (s *AccountService) ChangePassword(newPassword string) (*Client, error) {
data, err := json.Marshal(accountRequestBody{
Password: newPassword,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("/version/1/account/%s", s.client.EMail), bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := s.client.do(req)
if err != nil {
return nil, err
}
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)
}
return New(s.client.Endpoint, s.client.EMail, newPassword), nil
}