-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.go
244 lines (197 loc) · 6.45 KB
/
params.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
package trakt
import (
"context"
"net/http"
"strconv"
)
// BasicListParams is the structure that contains the common properties
// of any *ListParams structure, it is used when no OAuth token is required.
type BasicListParams struct {
// Context used for request. It may carry deadlines, cancelation signals,
// and other request-scoped values across API boundaries and between
// processes.
//
// Note that a cancelled or timed out context does not provide any
// guarantee whether the operation was or was not completed on Stripe's API
// servers. For certainty, you must either retry with the same idempotency
// key or queryFunc the state of the API.
Context context.Context `url:"-" json:"-"`
// Headers may be used to provide extra header lines on the HTTP request.
Headers http.Header `url:"-" json:"-"`
Page *int64 `url:"page,omitempty" json:"-"`
Limit *int64 `url:"limit,omitempty" json:"-"`
}
func (p *BasicListParams) context() context.Context {
if p != nil && p.Context != nil {
return p.Context
}
return context.Background()
}
func (p *BasicListParams) headers() http.Header {
if p == nil {
return nil
}
return p.Headers
}
func (p *BasicListParams) oauth() string { return `` }
func (p *BasicListParams) setPagination(page, limit int64) {
p.Page = Int64(page)
p.Limit = Int64(limit)
}
// setDefaultPagination sets the default pagination values supplied if
// any of the values are not defined.
func (p *BasicListParams) setDefaultPagination(page, limit int64) {
if p.Page == nil {
p.Page = Int64(page)
}
if p.Limit == nil {
p.Limit = Int64(limit)
}
}
// ListParams is the structure that contains the common properties
// of any *ListParams structure.
type ListParams struct {
// Context used for request. It may carry deadlines, cancelation signals,
// and other request-scoped values across API boundaries and between
// processes.
//
// Note that a cancelled or timed out context does not provide any
// guarantee whether the operation was or was not completed on Stripe's API
// servers. For certainty, you must either retry with the same idempotency
// key or queryFunc the state of the API.
Context context.Context `url:"-" json:"-"`
// Headers may be used to provide extra header lines on the HTTP request.
Headers http.Header `url:"-" json:"-"`
Page *int64 `url:"page,omitempty" json:"-"`
Limit *int64 `url:"limit,omitempty" json:"-"`
// OAuth token to use with the request.
// this is passed as a header if supplied.
OAuth string `url:"-" json:"-"`
}
func (p *ListParams) context() context.Context {
if p != nil && p.Context != nil {
return p.Context
}
return context.Background()
}
func (p *ListParams) headers() http.Header {
if p == nil {
return nil
}
return p.Headers
}
func (p *ListParams) oauth() string {
if p == nil {
return ``
}
return p.OAuth
}
func (p *ListParams) setPagination(page, limit int64) {
p.Page = Int64(page)
p.Limit = Int64(limit)
}
// setDefaultPagination sets the default pagination values supplied if
// any of the values are not defined.
func (p *ListParams) setDefaultPagination(page, limit int64) {
if p.Page == nil {
p.Page = Int64(page)
}
if p.Limit == nil {
p.Limit = Int64(limit)
}
}
// ListParamsContainer is a general interface for which all list parameter
// structs should comply. They achieve this by embedding a ListParams struct
// and inheriting its implementation of this interface.
type ListParamsContainer interface {
ParamsContainer
// setPagination sets the current page and limit.
setPagination(page, limit int64)
// setDefaultPagination sets the default initial pagination
// values to ensure that they are defined.
setDefaultPagination(page, limit int64)
}
// BasicParams parameters which do not require an OAuth token.
type BasicParams struct {
// Context used for request. It may carry deadlines, cancelation signals,
// and other request-scoped values across API boundaries and between
// processes.
//
// Note that a cancelled or timed out context does not provide any
// guarantee whether the operation was or was not completed on Stripe's API
// servers. For certainty, you must either retry with the same idempotency
// key or queryFunc the state of the API.
Context context.Context `url:"-" json:"-"`
// Headers may be used to provide extra header lines on the HTTP request.
Headers http.Header `url:"-" json:"-"`
}
func (p *BasicParams) setPagination(_, _ int64) {}
func (p *BasicParams) setDefaultPagination(_, _ int64) {}
func (p *BasicParams) context() context.Context {
if p != nil && p.Context != nil {
return p.Context
}
return context.Background()
}
func (p *BasicParams) headers() http.Header {
if p == nil {
return nil
}
return p.Headers
}
func (p *BasicParams) oauth() string { return `` }
// Params is the structure that contains the common properties
// of any *Params structure.
type Params struct {
// Context used for request. It may carry deadlines, cancelation signals,
// and other request-scoped values across API boundaries and between
// processes.
//
// Note that a cancelled or timed out context does not provide any
// guarantee whether the operation was or was not completed on Stripe's API
// servers. For certainty, you must either retry with the same idempotency
// key or queryFunc the state of the API.
Context context.Context `url:"-" json:"-"`
// Headers may be used to provide extra header lines on the HTTP request.
Headers http.Header `url:"-" json:"-"`
// OAuth token to use with the request.
// this is passed as a header if supplied.
OAuth string `url:"-" json:"-"`
}
func (p *Params) setPagination(_, _ int64) {}
func (p *Params) setDefaultPagination(_, _ int64) {}
func (p *Params) context() context.Context {
if p != nil && p.Context != nil {
return p.Context
}
return context.Background()
}
func (p *Params) headers() http.Header {
if p == nil {
return nil
}
return p.Headers
}
func (p *Params) oauth() string {
if p == nil {
return ``
}
return p.OAuth
}
// ParamsContainer is a general interface for which all parameter structs
// should comply. They achieve this by embedding a Params struct and inheriting
// its implementation of this interface.
type ParamsContainer interface {
context() context.Context
headers() http.Header
oauth() string
}
// parseInt helper function to parse a uint from a string.
func parseInt(s string) int64 {
i, _ := strconv.Atoi(s)
return int64(i)
}
// Int64 returns a pointer to the int64 value passed in.
func Int64(v int64) *int64 {
return &v
}