forked from andrew-waters/gomo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc_test.go
294 lines (260 loc) · 5.29 KB
/
doc_test.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package gomo_test
import (
"fmt"
"log"
"os"
"github.com/moltin/gomo"
"github.com/moltin/gomo/core"
"github.com/moltin/gomo/form"
)
func Example() {
// create a new client with client credentials
client := gomo.NewClient(
gomo.ClientCredentials(
"client_id",
"client_secret",
),
)
// handle an authentication error
if err := client.Authenticate(); err != nil {
log.Fatal(err)
}
// create a product
product := core.Product{
Name: "My new product",
}
var executionTime *gomo.APIExecution
// send the create request
err := client.Post(
"products",
gomo.Body(product),
gomo.Data(&product),
gomo.ExecutionTime(&executionTime),
)
if err != nil {
log.Fatal(err)
}
// print the execution time metric
log.Println("Execution time:", executionTime.Elapsed())
// update a product field
product.Name = "Updated Product"
// send the update request
err = client.Put(
fmt.Sprintf("products/%s", product.ID),
gomo.Body(product),
)
if err != nil {
log.Fatal(err)
}
// delete the product
err = client.Delete(fmt.Sprintf("products/%s", product.ID))
if err != nil {
log.Fatal(err)
}
}
func ExampleClientCredentials() {
creds := gomo.ClientCredentials(
"client_id",
"client_secret",
)
gomo.NewClient(creds)
}
func ExampleImplicitCredentials() {
creds := gomo.ImplicitCredentials(
"client_id",
)
gomo.NewClient(creds)
}
func ExampleNewClient() {
client := gomo.NewClient(
gomo.ClientCredentials(
"client_id",
"client_secret",
),
gomo.Endpoint("http://test.example.com/"),
gomo.Debug(),
)
log.Println(client.APIVersion)
}
func ExampleIterate() {
client := gomo.NewClient()
_ = client.Authenticate()
gomo.Iterate(
100,
func(paginate gomo.RequestResource) error {
page := []core.Product{}
err := client.Get("product", gomo.Data(&page))
if err != nil {
log.Fatal(err)
}
log.Printf("%d products in page\n", len(page))
return nil
},
)
}
func ExampleMorePages() {
client := gomo.NewClient()
_ = client.Authenticate()
var orders []core.Order
var meta core.Meta
err := client.Get("orders", gomo.Data(&orders), gomo.Meta(&meta))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Got %d orders.\n", len(orders))
if gomo.MorePages(meta) {
fmt.Println("There are more orders to fetch!")
}
}
func ExampleNextPage() {
client := gomo.NewClient()
_ = client.Authenticate()
// Note that Iterate() is a neater way of doing this.
var allOrders []core.Order
offset := 0
limit := 100
for {
var page []core.Order
var meta core.Meta
err := client.Get(
"orders",
gomo.Data(&page),
gomo.Meta(&meta),
gomo.Paginate(offset, limit),
)
if err != nil {
log.Fatal(err)
}
allOrders = append(allOrders, page...)
if !gomo.MorePages(meta) {
break
}
offset, limit = gomo.NextPage(meta)
}
fmt.Printf("Got all the orders, a total of %d\n", len(allOrders))
}
func ExampleClient_Get() {
client := gomo.NewClient()
_ = client.Authenticate()
id := "96a52ef6-62c0-47ad-809d-6390d7727d49"
type MyProduct struct {
core.Product
FlowField string `json:"flow_field"`
}
var product MyProduct
err := client.Get(
fmt.Sprintf("products/%s", id),
gomo.Data(&product),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf(
"Product %s has flow field %s\n",
product.Product.ID,
product.FlowField,
)
}
func ExampleClient_Post() {
client := gomo.NewClient()
_ = client.Authenticate()
type MyProduct struct {
core.Product
FlowField string `json:"flow_field"`
}
product := MyProduct{
Product: core.Product{
Name: "foo",
},
FlowField: "foo custom",
}
err := client.Post(
"products",
gomo.Body(product),
gomo.Data(&product),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created product %s\n", product.Product.ID)
}
func ExampleFilter() {
client := gomo.NewClient()
_ = client.Authenticate()
var draftProducts []core.Product
err := client.Get(
"products",
gomo.Filter("eq(status,draft)"),
gomo.Data(&draftProducts),
)
if err != nil {
log.Fatal(err)
}
fmt.Println("Draft products:")
for _, product := range draftProducts {
fmt.Println(product.ID)
}
}
func ExampleInclude() {
client := gomo.NewClient()
_ = client.Authenticate()
id := "c82d2f00-bc66-4c7d-984a-8765222abb98"
var included struct {
Products []core.Product `json:"products"`
}
err := client.Get(
"categories/"+id,
gomo.Include("products"),
gomo.Included(&included),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Category %s has products:\n", id)
for _, product := range included.Products {
fmt.Println(product.ID)
}
}
func ExampleSort() {
client := gomo.NewClient()
_ = client.Authenticate()
var productsByName []core.Product
err := client.Get(
"products",
gomo.Sort("name"),
gomo.Data(&productsByName),
)
if err != nil {
log.Fatal(err)
}
fmt.Println("Product names:")
for _, product := range productsByName {
fmt.Println(product.Name)
}
}
func ExampleForm() {
client := gomo.NewClient()
_ = client.Authenticate()
img, err := os.Open("/tmp/product.jpg")
if err != nil {
log.Fatal(err)
}
file := core.File{
FileName: "product.jpg",
Public: true,
MimeType: "image/jpeg",
File: &form.File{
Name: "product.jpg",
Content: img,
},
}
err = client.Post(
"files",
gomo.Form(file),
gomo.Data(&file),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("File ID: %s\n", file.ID)
}