forked from andrew-waters/gomo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_options.go
110 lines (94 loc) · 2.78 KB
/
request_options.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
package gomo
import (
"strconv"
)
// Body sets the body for a Post() or Put() request
func Body(target interface{}) RequestResource {
return func(w *wrapper) {
w.body = jsonBody{target}
// set the resource type if the entity has the SetType method
if resource, ok := target.(interface{ SetType() }); ok {
resource.SetType()
}
}
}
// Form sets multipart/form data for a Post() or Put() request
func Form(target interface{}) RequestResource {
return func(w *wrapper) {
w.body = &formBody{data: target}
// set the resource type if the entity has the SetType method
if resource, ok := target.(interface{ SetType() }); ok {
resource.SetType()
}
}
}
// Data sets a target for a responses data resource
func Data(target interface{}) RequestResource {
return func(w *wrapper) {
w.addResource("data", target)
}
}
// Included sets a target for a responses included resource
func Included(target interface{}) RequestResource {
return func(w *wrapper) {
w.addResource("included", target)
}
}
// Meta sets the a for a responses meta resource
func Meta(target interface{}) RequestResource {
return func(w *wrapper) {
w.addResource("meta", target)
}
}
// Links sets a target for a responses links resource
func Links(target interface{}) RequestResource {
return func(w *wrapper) {
w.addResource("links", target)
}
}
// Errors sets a target for the responses errors
func Errors(target *[]APIError) RequestResource {
return func(w *wrapper) {
w.addResource("errors", target)
}
}
// Paginate sets the page to select bases on the offset and limit.
// See https://docs.moltin.com/api/basics/pagination
func Paginate(offset, limit int) RequestResource {
return func(w *wrapper) {
w.query.Add("page[limit]", strconv.Itoa(limit))
w.query.Add("page[offset]", strconv.Itoa(offset))
}
}
// Filter adds a filter to a query, prepending to any existing
// filters. See https://docs.moltin.com/api/basics/filtering
func Filter(filter string) RequestResource {
return queryParameter("filter", filter, ":")
}
// Sort sorts the results.
// See https://docs.moltin.com/api/basics/sorting
func Sort(by string) RequestResource {
return func(w *wrapper) {
w.query.Set("sort", by)
}
}
// Include adds a resource to be included in the request.
// See https://docs.moltin.com/api/basics/includes
func Include(include string) RequestResource {
return queryParameter("include", include, ",")
}
// ExecutionTime returns a pointer to the ExecutionTime for the request
func ExecutionTime(e **APIExecution) RequestResource {
return func(w *wrapper) {
*e = &w.executionTime
}
}
func queryParameter(parameter, value, separator string) RequestResource {
return func(w *wrapper) {
existing := w.query.Get(parameter)
if existing != "" {
value += separator + existing
}
w.query.Set(parameter, value)
}
}