-
Notifications
You must be signed in to change notification settings - Fork 1
/
categories.go
117 lines (97 loc) · 3.37 KB
/
categories.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
package gocancel
import (
"context"
"fmt"
)
// CategoriesService provides access to the category related functions
// in the GoCancel API.
type CategoriesService service
// Category represents a GoCancel category.
type Category struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Slug *string `json:"slug,omitempty"`
RequiresConsent *bool `json:"requires_consent,omitempty"`
Locales []*CategoryLocale `json:"locales,omitempty"`
Metadata *AccountMetadata `json:"metadata,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
}
func (c Category) String() string {
return Stringify(c)
}
// CategoryLocale represents the localized variant of the category.
type CategoryLocale struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Slug *string `json:"slug,omitempty"`
RequiresConsent *bool `json:"requires_consent,omitempty"`
Locale *string `json:"locale,omitempty"`
Providers []*CategoryProvider `json:"providers,omitempty"`
LetterTemplate *LetterTemplate `json:"letter_template,omitempty"`
Metadata *AccountMetadata `json:"metadata,omitempty"`
}
func (o CategoryLocale) String() string {
return Stringify(o)
}
// CategoryProvider represents the provider of the category.
type CategoryProvider struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Type *string `json:"type,omitempty"`
Method *string `json:"method,omitempty"`
}
func (c CategoryProvider) String() string {
return Stringify(c)
}
type CategoriesSortOptions struct {
CreatedAt string `url:"created_at,omitempty"`
UpdatedAt string `url:"updated_at,omitempty"`
}
type CategoriesListOptions struct {
Cursor string `url:"cursor,omitempty"`
Limit int `url:"limit,omitempty"`
Locales []string `url:"locales[],omitempty"`
Metadata map[string]string `url:"metadata,omitempty"`
Slug string `url:"slug,omitempty"`
Sort CategoriesSortOptions `url:"sort,omitempty"`
}
type categoryRoot struct {
Category *Category `json:"category"`
}
type categoriesRoot struct {
Categories []*Category `json:"categories"`
Metadata *Metadata `json:"metadata"`
}
// List lists all categories
func (s *CategoriesService) List(ctx context.Context, opts *CategoriesListOptions) ([]*Category, *Response, error) {
u, err := addOptions("api/v1/categories", opts)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
root := new(categoriesRoot)
resp, err := s.client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}
resp.Metadata = root.Metadata
return root.Categories, resp, nil
}
// Get fetches a category.
func (s *CategoriesService) Get(ctx context.Context, category string) (*Category, *Response, error) {
u := fmt.Sprintf("api/v1/categories/%s", category)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
root := new(categoryRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Category, resp, nil
}