-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.go
58 lines (47 loc) · 1.2 KB
/
store.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
package nodeless
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// Store represents a Store.
type Store struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Email string `json:"email"`
CreatedAt time.Time `json:"createdAt"`
}
func (s *Store) String() string {
return fmt.Sprintf("id=%s name=%s", s.ID, s.Name)
}
// GetStores gets a list of Stores.
func (c *Client) GetStores(ctx context.Context) ([]Store, error) {
endpoint, err := url.JoinPath(c.config.apiBase(), "api/v1/store")
if err != nil {
return nil, fmt.Errorf("url JoinPath: %w", err)
}
var resp struct {
Data []Store `json:"data"`
}
if err := c.do(ctx, http.MethodGet, endpoint, nil, &resp); err != nil {
return nil, err
}
return resp.Data, nil
}
// GetStore gets a Store.
func (c *Client) GetStore(ctx context.Context, id string) (*Store, error) {
endpoint, err := url.JoinPath(c.config.apiBase(), "api/v1/store", id)
if err != nil {
return nil, fmt.Errorf("url JoinPath: %w", err)
}
var resp struct {
Data Store `json:"data"`
}
if err := c.do(ctx, http.MethodGet, endpoint, nil, &resp); err != nil {
return nil, err
}
return &resp.Data, nil
}