-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
62 lines (56 loc) · 1.29 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
package vex_go
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type Account struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreatedOn time.Time `json:"created_on"`
ModifiedOn time.Time `json:"modified_on"`
}
type AccountResponse struct {
Data Account `json:"data"`
Response
}
func (c *Client) GetAccount(ctx context.Context, id string) (*Account, error) {
url := fmt.Sprintf("/accounts/%s", id)
resp, err := c.doRequestContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
a := &AccountResponse{}
err = json.Unmarshal(resp, a)
if err != nil {
return nil, err
}
return &a.Data, nil
}
func (c *Client) UpdateAccount(ctx context.Context, a *Account) error {
url := fmt.Sprintf("/accounts/%s", a.ID)
j, err := json.Marshal(a)
if err != nil {
return err
}
resp, err := c.doRequestContext(ctx, http.MethodPut, url, bytes.NewReader(j))
if err != nil {
return err
}
ar := &AccountResponse{}
err = json.Unmarshal(resp, a)
if err != nil {
return err
}
*a = ar.Data
return nil
}
func (c *Client) DeleteAccount(ctx context.Context, id string) error {
url := fmt.Sprintf("/accounts/%s", id)
_, err := c.doRequestContext(ctx, http.MethodDelete, url, nil)
return err
}