-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_client_test.go
70 lines (53 loc) · 1.33 KB
/
example_client_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
// Copyright (c) 2020 Pieoneers Software Incorporated. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package client_test
import (
"fmt"
jsonapiClient "github.com/pieoneers/jsonapi-client-go"
"log"
"time"
)
type Item struct {
ID string `json:"-"`
Type string `json:"-"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
}
func (d *Item) SetID(id string) error {
d.ID = id
return nil
}
func (d *Item) SetType(t string) error {
d.Type = t
return nil
}
func (b *Item) SetData(to func(target interface{}) error) error {
return to(b)
}
type Items []Item
func (b *Items) SetData(to func(target interface{}) error) error {
return to(b)
}
func ExampleClient() {
var target []Item
config := jsonapiClient.Config{
BaseURL: "http://localhost",
Timeout: time.Second,
}
client := jsonapiClient.NewClient(config)
request, requestErr := jsonapiClient.NewRequest("GET", "/data", nil)
if requestErr != nil {
log.Println("requestErr: ", requestErr)
return
}
request.Query.Set("filter[color]", "silver")
_, responseErr := client.Do(request, &target)
if responseErr != nil {
log.Println("responseErr: ", responseErr)
return
}
for _, item := range target {
fmt.Printf("\titem#%v:\t%+v\n", item.ID, item)
}
}