-
Notifications
You must be signed in to change notification settings - Fork 0
/
headless_client.go
193 lines (154 loc) · 4.99 KB
/
headless_client.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
190
191
192
193
package tebex
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
// Based on https://documenter.getpostman.com/view/10912536/2s9XxvTEmh
const DefaultBaseUrl = "https://headless.tebex.io"
var (
ErrHeadlessWebstoreNotFound = fmt.Errorf("webstore not found")
ErrHeadlessBasketNotFound = fmt.Errorf("basket not found")
ErrHeadlessCreatorCodeNotFound = fmt.Errorf("creator code not found")
)
var DefaultHeadlessClient = NewHeadlessClient(DefaultBaseUrl)
type HeadlessClient struct {
url string
httpClient *http.Client
}
type HeadlessClientParams struct {
Url string // Required
HttpClient *http.Client
}
func NewHeadlessClient(url string) *HeadlessClient {
return NewHeadlessClientWithOptions(HeadlessClientParams{Url: url})
}
func NewHeadlessClientWithOptions(params HeadlessClientParams) *HeadlessClient {
c := &HeadlessClient{
url: params.Url,
httpClient: params.HttpClient,
}
if strings.HasSuffix(c.url, "/") {
c.url = c.url[:len(c.url)-1]
}
if c.httpClient == nil {
c.httpClient = http.DefaultClient
}
return c
}
// MARK: Basket
func (c *HeadlessClient) CreateBasket(ctx context.Context, webstoreId string, body HeadlessCreateBasketRequest) (*HeadlessBasket, error) {
url := fmt.Sprintf("%s/api/accounts/%s/baskets", c.url, webstoreId)
bodyRaw, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bodyRaw))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res, err := do[HeadlessBasket](c.httpClient, req)
if err != nil {
return nil, err
}
return res.Data, nil
}
// MARK: Basket Packages
func (c *HeadlessClient) BasketAddPackage(ctx context.Context, basketId string, body HeadlessBasketAddPackageRequest) (*HeadlessBasket, error) {
url := fmt.Sprintf("%s/api/baskets/%s/packages", c.url, basketId)
bodyRaw, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bodyRaw))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
res, err := do[HeadlessBasket](c.httpClient, req)
if err != nil {
return nil, err
}
return res.Data, nil
}
// MARK: Creator Codes
func (c *HeadlessClient) BasketApplyCreatorCode(ctx context.Context, webstoreId, basketId, creatorCode string) error {
url := fmt.Sprintf("%s/api/accounts/%s/baskets/%s/creator-codes", c.url, webstoreId, basketId)
body := fmt.Sprintf(`{"code":"%s"}`, creatorCode)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
if _, err = do[any](c.httpClient, req); err != nil {
return err
}
// Don't care about the response unless it was non-200 which was handled in `do`.
return nil
}
func (c *HeadlessClient) BasketRemoveCreatorCode(ctx context.Context, webstoreId, basketId string) error {
url := fmt.Sprintf("%s/api/accounts/%s/baskets/%s/creator-codes/remove", c.url, webstoreId, basketId)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
if _, err = do[any](c.httpClient, req); err != nil {
return err
}
// Don't care about the response unless it was non-200 which was handled in `do`.
return nil
}
type headlessResponse[T any] struct {
// Always updated to the real http status code if zero (ie on success)
Status int `json:"status"`
// Success fields
Message string `json:"message"`
Data *T `json:"data"`
// Failure fields
Title string `json:"title"`
Detail string `json:"detail"`
// There are other fields here, but they are empty as far as I(matt) can tell
}
func (r *headlessResponse[T]) into() error {
if r.Status < 400 {
return nil
}
if err, ok := knownErrors[r.Detail]; ok {
return err
}
if r.Detail != "" {
return errors.New(r.Detail)
}
return fmt.Errorf("bad response: %d", r.Status)
}
func do[T any](httpClient *http.Client, req *http.Request) (*headlessResponse[T], error) {
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var res headlessResponse[T]
if err = json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
if res.Status == 0 {
res.Status = resp.StatusCode
}
if err = res.into(); err != nil {
return nil, err
}
return &res, nil
}
// kno wnErrors is a map of the plain text error messages that are known/handled explicitly.
// For some unknown reason the tebex error gives no useful info besides the user facing/plaintext message.
var knownErrors = map[string]error{
"Invalid account identifier provided": ErrHeadlessWebstoreNotFound,
"Invalid basket identifier provided": ErrHeadlessBasketNotFound,
"Invalid creator code provided": ErrHeadlessCreatorCodeNotFound,
}