-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_fields_controller.go
463 lines (431 loc) · 22.4 KB
/
custom_fields_controller.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
Package advancedbilling
This file was automatically generated for Maxio by APIMATIC v3.0 ( https://www.apimatic.io ).
*/
package advancedbilling
import (
"context"
"fmt"
"github.com/apimatic/go-core-runtime/https"
"github.com/apimatic/go-core-runtime/utilities"
"github.com/maxio-com/ab-golang-sdk/errors"
"github.com/maxio-com/ab-golang-sdk/models"
"net/http"
"time"
)
// CustomFieldsController represents a controller struct.
type CustomFieldsController struct {
baseController
}
// NewCustomFieldsController creates a new instance of CustomFieldsController.
// It takes a baseController as a parameter and returns a pointer to the CustomFieldsController.
func NewCustomFieldsController(baseController baseController) *CustomFieldsController {
customFieldsController := CustomFieldsController{baseController: baseController}
return &customFieldsController
}
// CreateMetafields takes context, resourceType, body as parameters and
// returns an models.ApiResponse with []models.Metafield data and
// an error if there was an issue with the request or response.
// ## Custom Fields: Metafield Intro
// **Advanced Billing refers to Custom Fields in the API documentation as metafields and metadata.** Within the Advanced Billing UI, metadata and metafields are grouped together under the umbrella of "Custom Fields." All of our UI-based documentation that references custom fields will not cite the terminology metafields or metadata.
// + **Metafield is the custom field**
// + **Metadata is the data populating the custom field.**
// Advanced Billing Metafields are used to add meaningful attributes to subscription and customer resources. Full documentation on how to create Custom Fields in the Advanced Billing UI can be located [here](https://maxio.zendesk.com/hc/en-us/sections/24266118312589-Custom-Fields). For additional documentation on how to record data within custom fields, please see our subscription-based documentation [here](https://maxio.zendesk.com/hc/en-us/articles/24251701302925-Subscription-Summary-Custom-Fields-Tab).
// Metafield are the place where you will set up your resource to accept additional data. It is scoped to the site instead of a specific customer or subscription. Think of it as the key, and Metadata as the value on every record.
// ## Create Metafields
// Use this endpoint to create metafields for your Site. Metafields can be populated with metadata after the fact.
// Each site is limited to 100 unique Metafields (i.e. keys, or names) per resource. This means you can have 100 Metafields for Subscription and another 100 for Customer.
// ### Metafields "On-the-Fly"
// It is possible to create Metafields “on the fly” when you create your Metadata – if a non-existant name is passed when creating Metadata, a Metafield for that key will be automatically created. The Metafield API, however, gives you more control over your “keys”.
// ### Metafield Scope Warning
// If configuring metafields in the Admin UI or via the API, be careful sending updates to metafields with the scope attribute – **if a partial update is sent it will overwrite the current configuration**.
func (c *CustomFieldsController) CreateMetafields(
ctx context.Context,
resourceType models.ResourceType,
body *models.CreateMetafieldsRequest) (
models.ApiResponse[[]models.Metafield],
error) {
req := c.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/%v/metafields.json", resourceType),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewSingleErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result []models.Metafield
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.Metafield](decoder)
return models.NewApiResponse(result, resp), err
}
// ListMetafieldsInput represents the input of the ListMetafields endpoint.
type ListMetafieldsInput struct {
// the resource type to which the metafields belong
ResourceType models.ResourceType
// filter by the name of the metafield
Name *string
// Result records are organized in pages. By default, the first page of results is displayed. The page parameter specifies a page number of results to fetch. You can start navigating through the pages to consume the results. You do this by passing in a page parameter. Retrieve the next page by adding ?page=2 to the query string. If there are no results to return, then an empty result set will be returned.
// Use in query `page=1`.
Page *int
// This parameter indicates how many records to fetch in each request. Default value is 20. The maximum allowed values is 200; any per_page value over 200 will be changed to 200.
// Use in query `per_page=200`.
PerPage *int
// Controls the order in which results are returned.
// Use in query `direction=asc`.
Direction *models.SortingDirection
}
// ListMetafields takes context, resourceType, name, page, perPage, direction as parameters and
// returns an models.ApiResponse with models.ListMetafieldsResponse data and
// an error if there was an issue with the request or response.
// This endpoint lists metafields associated with a site. The metafield description and usage is contained in the response.
func (c *CustomFieldsController) ListMetafields(
ctx context.Context,
input ListMetafieldsInput) (
models.ApiResponse[models.ListMetafieldsResponse],
error) {
req := c.prepareRequest(
ctx,
"GET",
fmt.Sprintf("/%v/metafields.json", input.ResourceType),
)
req.Authenticate(NewAuth("BasicAuth"))
if input.Name != nil {
req.QueryParam("name", *input.Name)
}
if input.Page != nil {
req.QueryParam("page", *input.Page)
}
if input.PerPage != nil {
req.QueryParam("per_page", *input.PerPage)
}
if input.Direction != nil {
req.QueryParam("direction", *input.Direction)
}
var result models.ListMetafieldsResponse
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.ListMetafieldsResponse](decoder)
return models.NewApiResponse(result, resp), err
}
// UpdateMetafield takes context, resourceType, body as parameters and
// returns an models.ApiResponse with []models.Metafield data and
// an error if there was an issue with the request or response.
// Use the following method to update metafields for your Site. Metafields can be populated with metadata after the fact.
func (c *CustomFieldsController) UpdateMetafield(
ctx context.Context,
resourceType models.ResourceType,
body *models.UpdateMetafieldsRequest) (
models.ApiResponse[[]models.Metafield],
error) {
req := c.prepareRequest(
ctx,
"PUT",
fmt.Sprintf("/%v/metafields.json", resourceType),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewSingleErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result []models.Metafield
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.Metafield](decoder)
return models.NewApiResponse(result, resp), err
}
// DeleteMetafield takes context, resourceType, name as parameters and
// returns an *Response and
// an error if there was an issue with the request or response.
// Use the following method to delete a metafield. This will remove the metafield from the Site.
// Additionally, this will remove the metafield and associated metadata with all Subscriptions on the Site.
func (c *CustomFieldsController) DeleteMetafield(
ctx context.Context,
resourceType models.ResourceType,
name *string) (
*http.Response,
error) {
req := c.prepareRequest(
ctx,
"DELETE",
fmt.Sprintf("/%v/metafields.json", resourceType),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
})
if name != nil {
req.QueryParam("name", *name)
}
httpCtx, err := req.Call()
if err != nil {
return httpCtx.Response, err
}
return httpCtx.Response, err
}
// CreateMetadata takes context, resourceType, resourceId, body as parameters and
// returns an models.ApiResponse with []models.Metadata data and
// an error if there was an issue with the request or response.
// ## Custom Fields: Metadata Intro
// **Advanced Billing refers to Custom Fields in the API documentation as metafields and metadata.** Within the Advanced Billing UI, metadata and metafields are grouped together under the umbrella of "Custom Fields." All of our UI-based documentation that references custom fields will not cite the terminology metafields or metadata.
// + **Metafield is the custom field**
// + **Metadata is the data populating the custom field.**
// Advanced Billing Metafields are used to add meaningful attributes to subscription and customer resources. Full documentation on how to create Custom Fields in the Advanced Billing UI can be located [here](https://maxio.zendesk.com/hc/en-us/articles/24266164865677-Custom-Fields-Overview). For additional documentation on how to record data within custom fields, please see our subscription-based documentation [here.](https://maxio.zendesk.com/hc/en-us/articles/24251701302925-Subscription-Summary-Custom-Fields-Tab)
// Metadata is associated to a customer or subscription, and corresponds to a Metafield. When creating a new metadata object for a given record, **if the metafield is not present it will be created**.
// ## Metadata limits
// Metadata values are limited to 2kB in size. Additonally, there are limits on the number of unique metafields available per resource.
// ## Create Metadata
// This method will create a metafield for the site on the fly if it does not already exist, and populate the metadata value.
// ### Subscription or Customer Resource
// Please pay special attention to the resource you use when creating metadata.
func (c *CustomFieldsController) CreateMetadata(
ctx context.Context,
resourceType models.ResourceType,
resourceId int,
body *models.CreateMetadataRequest) (
models.ApiResponse[[]models.Metadata],
error) {
req := c.prepareRequest(
ctx,
"POST",
fmt.Sprintf("/%v/%v/metadata.json", resourceType, resourceId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewSingleErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result []models.Metadata
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.Metadata](decoder)
return models.NewApiResponse(result, resp), err
}
// ListMetadataInput represents the input of the ListMetadata endpoint.
type ListMetadataInput struct {
// the resource type to which the metafields belong
ResourceType models.ResourceType
// The Advanced Billing id of the customer or the subscription for which the metadata applies
ResourceId int
// Result records are organized in pages. By default, the first page of results is displayed. The page parameter specifies a page number of results to fetch. You can start navigating through the pages to consume the results. You do this by passing in a page parameter. Retrieve the next page by adding ?page=2 to the query string. If there are no results to return, then an empty result set will be returned.
// Use in query `page=1`.
Page *int
// This parameter indicates how many records to fetch in each request. Default value is 20. The maximum allowed values is 200; any per_page value over 200 will be changed to 200.
// Use in query `per_page=200`.
PerPage *int
}
// ListMetadata takes context, resourceType, resourceId, page, perPage as parameters and
// returns an models.ApiResponse with models.PaginatedMetadata data and
// an error if there was an issue with the request or response.
// This request will list all of the metadata belonging to a particular resource (ie. subscription, customer) that is specified.
// ## Metadata Data
// This endpoint will also display the current stats of your metadata to use as a tool for pagination.
func (c *CustomFieldsController) ListMetadata(
ctx context.Context,
input ListMetadataInput) (
models.ApiResponse[models.PaginatedMetadata],
error) {
req := c.prepareRequest(
ctx,
"GET",
fmt.Sprintf("/%v/%v/metadata.json", input.ResourceType, input.ResourceId),
)
req.Authenticate(NewAuth("BasicAuth"))
if input.Page != nil {
req.QueryParam("page", *input.Page)
}
if input.PerPage != nil {
req.QueryParam("per_page", *input.PerPage)
}
var result models.PaginatedMetadata
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.PaginatedMetadata](decoder)
return models.NewApiResponse(result, resp), err
}
// UpdateMetadata takes context, resourceType, resourceId, body as parameters and
// returns an models.ApiResponse with []models.Metadata data and
// an error if there was an issue with the request or response.
// This method allows you to update the existing metadata associated with a subscription or customer.
func (c *CustomFieldsController) UpdateMetadata(
ctx context.Context,
resourceType models.ResourceType,
resourceId int,
body *models.UpdateMetadataRequest) (
models.ApiResponse[[]models.Metadata],
error) {
req := c.prepareRequest(
ctx,
"PUT",
fmt.Sprintf("/%v/%v/metadata.json", resourceType, resourceId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"422": {TemplatedMessage: "HTTP Response Not OK. Status code: {$statusCode}. Response: '{$response.body}'.", Unmarshaller: errors.NewSingleErrorResponse},
})
req.Header("Content-Type", "application/json")
if body != nil {
req.Json(body)
}
var result []models.Metadata
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[[]models.Metadata](decoder)
return models.NewApiResponse(result, resp), err
}
// DeleteMetadata takes context, resourceType, resourceId, name, names as parameters and
// returns an *Response and
// an error if there was an issue with the request or response.
// This method removes the metadata from the subscriber/customer cited.
// ## Query String Usage
// For instance if you wanted to delete the metadata for customer 99 named weight you would request:
// ```
// https://acme.chargify.com/customers/99/metadata.json?name=weight
// ```
// If you want to delete multiple metadata fields for a customer 99 named: `weight` and `age` you wrould request:
// ```
// https://acme.chargify.com/customers/99/metadata.json?names[]=weight&names[]=age
// ```
// ## Successful Response
// For a success, there will be a code `200` and the plain text response `true`.
// ## Unsuccessful Response
// When a failed response is encountered, you will receive a `404` response and the plain text response of `true`.
func (c *CustomFieldsController) DeleteMetadata(
ctx context.Context,
resourceType models.ResourceType,
resourceId int,
name *string,
names []string) (
*http.Response,
error) {
req := c.prepareRequest(
ctx,
"DELETE",
fmt.Sprintf("/%v/%v/metadata.json", resourceType, resourceId),
)
req.Authenticate(NewAuth("BasicAuth"))
req.AppendErrors(map[string]https.ErrorBuilder[error]{
"404": {TemplatedMessage: "Not Found:'{$response.body}'"},
})
if name != nil {
req.QueryParamWithArraySerializationOption("name", *name, https.UnIndexed)
}
if names != nil {
req.QueryParamWithArraySerializationOption("names", names, https.UnIndexed)
}
httpCtx, err := req.Call()
if err != nil {
return httpCtx.Response, err
}
return httpCtx.Response, err
}
// ListMetadataForResourceTypeInput represents the input of the ListMetadataForResourceType endpoint.
type ListMetadataForResourceTypeInput struct {
// the resource type to which the metafields belong
ResourceType models.ResourceType
// Result records are organized in pages. By default, the first page of results is displayed. The page parameter specifies a page number of results to fetch. You can start navigating through the pages to consume the results. You do this by passing in a page parameter. Retrieve the next page by adding ?page=2 to the query string. If there are no results to return, then an empty result set will be returned.
// Use in query `page=1`.
Page *int
// This parameter indicates how many records to fetch in each request. Default value is 20. The maximum allowed values is 200; any per_page value over 200 will be changed to 200.
// Use in query `per_page=200`.
PerPage *int
// The type of filter you would like to apply to your search.
DateField *models.BasicDateField
// The start date (format YYYY-MM-DD) with which to filter the date_field. Returns metadata with a timestamp at or after midnight (12:00:00 AM) in your site’s time zone on the date specified.
StartDate *time.Time
// The end date (format YYYY-MM-DD) with which to filter the date_field. Returns metadata with a timestamp up to and including 11:59:59PM in your site’s time zone on the date specified.
EndDate *time.Time
// The start date and time (format YYYY-MM-DD HH:MM:SS) with which to filter the date_field. Returns metadata with a timestamp at or after exact time provided in query. You can specify timezone in query - otherwise your site's time zone will be used. If provided, this parameter will be used instead of start_date.
StartDatetime *time.Time
// The end date and time (format YYYY-MM-DD HH:MM:SS) with which to filter the date_field. Returns metadata with a timestamp at or before exact time provided in query. You can specify timezone in query - otherwise your site's time zone will be used. If provided, this parameter will be used instead of end_date.
EndDatetime *time.Time
// Allow to fetch deleted metadata.
WithDeleted *bool
// Allow to fetch metadata for multiple records based on provided ids. Use in query: `resource_ids[]=122&resource_ids[]=123&resource_ids[]=124`.
ResourceIds []int
// Controls the order in which results are returned.
// Use in query `direction=asc`.
Direction *models.SortingDirection
}
// ListMetadataForResourceType takes context, resourceType, page, perPage, dateField, startDate, endDate, startDatetime, endDatetime, withDeleted, resourceIds, direction as parameters and
// returns an models.ApiResponse with models.PaginatedMetadata data and
// an error if there was an issue with the request or response.
// This method will provide you information on usage of metadata across your selected resource (ie. subscriptions, customers)
// ## Metadata Data
// This endpoint will also display the current stats of your metadata to use as a tool for pagination.
// ### Metadata for multiple records
// `https://acme.chargify.com/subscriptions/metadata.json?resource_ids[]=1&resource_ids[]=2`
// ## Read Metadata for a Site
// This endpoint will list the number of pages of metadata information that are contained within a site.
func (c *CustomFieldsController) ListMetadataForResourceType(
ctx context.Context,
input ListMetadataForResourceTypeInput) (
models.ApiResponse[models.PaginatedMetadata],
error) {
req := c.prepareRequest(
ctx,
"GET",
fmt.Sprintf("/%v/metadata.json", input.ResourceType),
)
req.Authenticate(NewAuth("BasicAuth"))
if input.Page != nil {
req.QueryParamWithArraySerializationOption("page", *input.Page, https.UnIndexed)
}
if input.PerPage != nil {
req.QueryParamWithArraySerializationOption("per_page", *input.PerPage, https.UnIndexed)
}
if input.DateField != nil {
req.QueryParamWithArraySerializationOption("date_field", *input.DateField, https.UnIndexed)
}
if input.StartDate != nil {
req.QueryParam("start_date", input.StartDate.Format(models.DEFAULT_DATE))
}
if input.EndDate != nil {
req.QueryParam("end_date", input.EndDate.Format(models.DEFAULT_DATE))
}
if input.StartDatetime != nil {
req.QueryParam("start_datetime", input.StartDatetime.Format(time.RFC3339))
}
if input.EndDatetime != nil {
req.QueryParam("end_datetime", input.EndDatetime.Format(time.RFC3339))
}
if input.WithDeleted != nil {
req.QueryParamWithArraySerializationOption("with_deleted", *input.WithDeleted, https.UnIndexed)
}
if input.ResourceIds != nil {
req.QueryParamWithArraySerializationOption("resource_ids", input.ResourceIds, https.UnIndexed)
}
if input.Direction != nil {
req.QueryParamWithArraySerializationOption("direction", *input.Direction, https.UnIndexed)
}
var result models.PaginatedMetadata
decoder, resp, err := req.CallAsJson()
if err != nil {
return models.NewApiResponse(result, resp), err
}
result, err = utilities.DecodeResults[models.PaginatedMetadata](decoder)
return models.NewApiResponse(result, resp), err
}