This repository has been archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
examples_test.go
126 lines (104 loc) · 2.28 KB
/
examples_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
package halgo_test
import (
"."
"encoding/json"
"fmt"
"net/http"
)
func ExampleLinks() {
type person struct {
halgo.Links
Id int
Name string
}
p := person{
Id: 1,
Name: "James",
Links: halgo.Links{}.
Self("http://example.com/users/1").
Link("invoices", "http://example.com/users/1/invoices"),
}
b, _ := json.MarshalIndent(p, "", " ")
fmt.Println(string(b))
// Output:
// {
// "_links": {
// "invoices": {
// "href": "http://example.com/users/1/invoices"
// },
// "self": {
// "href": "http://example.com/users/1"
// }
// },
// "Id": 1,
// "Name": "James"
// }
}
func ExampleLinks_templated() {
type root struct{ halgo.Links }
p := root{
Links: halgo.Links{}.
Link("invoices", "http://example.com/invoices{?q,sort}"),
}
b, _ := json.MarshalIndent(p, "", " ")
fmt.Println(string(b))
// Output:
// {
// "_links": {
// "invoices": {
// "href": "http://example.com/invoices{?q,sort}",
// "templated": true
// }
// }
// }
}
func ExampleLinks_multiple() {
type person struct {
halgo.Links
Id int
Name string
}
p := person{
Id: 1,
Name: "James",
Links: halgo.Links{}.
Add("aliases", halgo.Link{Href: "http://example.com/users/4"}, halgo.Link{Href: "http://example.com/users/19"}),
}
b, _ := json.MarshalIndent(p, "", " ")
fmt.Println(string(b))
// Output:
// {
// "_links": {
// "aliases": [
// {
// "href": "http://example.com/users/4"
// },
// {
// "href": "http://example.com/users/19"
// }
// ]
// },
// "Id": 1,
// "Name": "James"
// }
}
func ExampleNavigator() {
var me struct{ Username string }
halgo.Navigator("http://haltalk.herokuapp.com/").
Followf("ht:me", halgo.P{"name": "jagregory"}).
Unmarshal(&me)
fmt.Println(me.Username)
// Output: jagregory
}
func ExampleNavigator_logging() {
var me struct{ Username string }
nav := halgo.Navigator("http://haltalk.herokuapp.com/")
nav.HttpClient = halgo.LoggingHttpClient{http.DefaultClient}
nav.Followf("ht:me", halgo.P{"name": "jagregory"}).
Unmarshal(&me)
fmt.Printf("Username: %s", me.Username)
// Output:
// GET http://haltalk.herokuapp.com/
// GET http://haltalk.herokuapp.com/users/jagregory
// Username: jagregory
}