-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
189 lines (154 loc) · 5.92 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package eygo
import (
"encoding/json"
"fmt"
)
// Account is a data structure that models an Engine Yard account.
type Account struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Plan string `json:"plan,omitempty"`
SupportPlan string `json:"support_plan,omitempty"`
Type string `json:"type,omitempty"`
EmergencyContact string `json:"emergency_contact,omitempty"`
AccountNotesURL string `json:"account_notes,omitempty"`
Billable bool `json:"billable,omitempty"`
BillingPlanSupport string `json:"billing_plan_support,omitempty"`
BillingRequired bool `json:"billing_required,omitempty"`
BillingSignupURL string `json:"billing_signup_url,omitempty"`
Finalized bool `json:"finalized,omitempty"`
SignupVia string `json:"signup_via,omitempty"`
SupportTrialStatus string `json:"support_trial_status,omitempty"`
Cancellation string `json:cancellation,omitempty`
CanceledAt string `json:"canceled_at,omitempty"`
CancelledAt string `json:"cancelled_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
// AccountService is a repository one can use to retrieve and save Account
// records on the API.
type AccountService struct {
Driver Driver
}
// NewAccountService returns an AccountService configured to use the provided
// Driver.
func NewAccountService(driver Driver) *AccountService {
return &AccountService{Driver: driver}
}
// All returns an array of all Account records that match the given Params.
func (service *AccountService) All(params Params) []*Account {
return service.collection("accounts", params)
}
// Find returns the Account record identified by the given account id. If there
// are errors in retrieving this information, an error is returned as well.
func (service *AccountService) Find(id string) (*Account, error) {
response := service.Driver.Get("accounts/"+id, nil)
if response.Okay() {
wrapper := struct {
Account *Account `json:"account,omitempty"`
}{}
err := json.Unmarshal(response.Pages[0], &wrapper)
if err != nil {
return nil, err
}
return wrapper.Account, nil
}
return nil, response.Error
}
// ForUser returns an array of Accounts that are both associated with the
// given User and that match the given Params.
func (service *AccountService) ForUser(user *User, params Params) []*Account {
return service.collection(fmt.Sprintf("users/%s/accounts", user.ID), params)
}
type accountName struct {
Name string `json:"name,omitempty"`
}
// Rename takes an Account and a string, saving the Account on the upstream API
// with the new name. IF there are any issues along the way, an error is
// returned. Otherwise, the updated Account is returned.
func (service *AccountService) Rename(account *Account, name string) (*Account, error) {
wrapper := struct {
Account *accountName `json:"account,omitempty"`
}{Account: &accountName{Name: name}}
body, err := json.Marshal(&wrapper)
if err != nil {
return nil, err
}
return service.update(account, body)
}
type accountEmergencyContact struct {
EmergencyContact string `json:"emergency_contact,omitempty"`
}
// UpdateEmergencyContact takes an Account and a string, saving the Account on
// the upstream API with the new emergency contact. If there are issues along
// the way, an error is returned. Otherwise, the updated Account is returned.
func (service *AccountService) UpdateEmergencyContact(account *Account, contact string) (*Account, error) {
wrapper := struct {
Account *accountEmergencyContact `json:"account,omitempty"`
}{Account: &accountEmergencyContact{EmergencyContact: contact}}
body, err := json.Marshal(&wrapper)
if err != nil {
return nil, err
}
return service.update(account, body)
}
type accountSupportPlan struct {
SupportPlan string `json:"support_plan,omitempty"`
}
// UpdateSupportPlan takes an Account and a string, saving the Account on
// the upstream API with the new support plan. If there are issues along
// the way, an error is returned. Otherwise, the updated Account is returned.
func (service *AccountService) UpdateSupportPlan(account *Account, plan string) (*Account, error) {
wrapper := struct {
Account *accountSupportPlan `json:"account, omitempty"`
}{&accountSupportPlan{SupportPlan: plan}}
body, err := json.Marshal(&wrapper)
if err != nil {
return nil, err
}
return service.update(account, body)
}
func (service *AccountService) update(account *Account, data []byte) (*Account, error) {
if len(account.ID) == 0 {
return nil, fmt.Errorf("can't update an account without an ID")
}
response := service.Driver.Put("accounts/"+account.ID, nil, data)
if response.Okay() {
wrapped := struct {
Account *Account `json:"account,omitempty"`
}{}
err := json.Unmarshal(response.Pages[0], &wrapped)
if err != nil {
return nil, err
}
return wrapped.Account, nil
}
return nil, response.Error
}
func (service *AccountService) collection(path string, params Params) []*Account {
accounts := make([]*Account, 0)
response := service.Driver.Get(path, params)
if response.Okay() {
for _, page := range response.Pages {
wrapper := struct {
Accounts []*Account `json:"accounts,omitempty"`
}{}
if err := json.Unmarshal(page, &wrapper); err == nil {
accounts = append(accounts, wrapper.Accounts...)
}
}
}
return accounts
}
/*
Copyright 2018 Dennis Walters
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/