-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathitem.go
More file actions
283 lines (250 loc) · 12.7 KB
/
item.go
File metadata and controls
283 lines (250 loc) · 12.7 KB
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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package orb
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/orbcorp/orb-go/internal/apijson"
"github.com/orbcorp/orb-go/internal/apiquery"
"github.com/orbcorp/orb-go/internal/param"
"github.com/orbcorp/orb-go/internal/requestconfig"
"github.com/orbcorp/orb-go/option"
"github.com/orbcorp/orb-go/packages/pagination"
)
// The Item resource represents a sellable product or good. Items are associated
// with all line items, billable metrics, and prices and are used for defining
// external sync behavior for invoices and tax calculation purposes.
//
// ItemService contains methods and other services that help with interacting with
// the orb API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewItemService] method instead.
type ItemService struct {
Options []option.RequestOption
}
// NewItemService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewItemService(opts ...option.RequestOption) (r *ItemService) {
r = &ItemService{}
r.Options = opts
return
}
// This endpoint is used to create an [Item](/core-concepts#item).
func (r *ItemService) New(ctx context.Context, body ItemNewParams, opts ...option.RequestOption) (res *Item, err error) {
opts = slices.Concat(r.Options, opts)
path := "items"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// This endpoint can be used to update properties on the Item.
func (r *ItemService) Update(ctx context.Context, itemID string, body ItemUpdateParams, opts ...option.RequestOption) (res *Item, err error) {
opts = slices.Concat(r.Options, opts)
if itemID == "" {
err = errors.New("missing required item_id parameter")
return nil, err
}
path := fmt.Sprintf("items/%s", itemID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, body, &res, opts...)
return res, err
}
// This endpoint returns a list of all Items, ordered in descending order by
// creation time.
func (r *ItemService) List(ctx context.Context, query ItemListParams, opts ...option.RequestOption) (res *pagination.Page[Item], err error) {
var raw *http.Response
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "items"
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// This endpoint returns a list of all Items, ordered in descending order by
// creation time.
func (r *ItemService) ListAutoPaging(ctx context.Context, query ItemListParams, opts ...option.RequestOption) *pagination.PageAutoPager[Item] {
return pagination.NewPageAutoPager(r.List(ctx, query, opts...))
}
// Archive item
func (r *ItemService) Archive(ctx context.Context, itemID string, opts ...option.RequestOption) (res *Item, err error) {
opts = slices.Concat(r.Options, opts)
if itemID == "" {
err = errors.New("missing required item_id parameter")
return nil, err
}
path := fmt.Sprintf("items/%s/archive", itemID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return res, err
}
// This endpoint returns an item identified by its item_id.
func (r *ItemService) Fetch(ctx context.Context, itemID string, opts ...option.RequestOption) (res *Item, err error) {
opts = slices.Concat(r.Options, opts)
if itemID == "" {
err = errors.New("missing required item_id parameter")
return nil, err
}
path := fmt.Sprintf("items/%s", itemID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// The Item resource represents a sellable product or good. Items are associated
// with all line items, billable metrics, and prices and are used for defining
// external sync behavior for invoices and tax calculation purposes.
type Item struct {
// The Orb-assigned unique identifier for the item.
ID string `json:"id" api:"required"`
// The time at which the item was created.
CreatedAt time.Time `json:"created_at" api:"required" format:"date-time"`
// A list of external connections for this item, used to sync with external
// invoicing and tax systems.
ExternalConnections []ItemExternalConnection `json:"external_connections" api:"required"`
// User specified key-value pairs for the resource. If not present, this defaults
// to an empty dictionary. Individual keys can be removed by setting the value to
// `null`, and the entire metadata mapping can be cleared by setting `metadata` to
// `null`.
Metadata map[string]string `json:"metadata" api:"required"`
// The name of the item.
Name string `json:"name" api:"required"`
// The time at which the item was archived. If null, the item is not archived.
ArchivedAt time.Time `json:"archived_at" api:"nullable" format:"date-time"`
JSON itemJSON `json:"-"`
}
// itemJSON contains the JSON metadata for the struct [Item]
type itemJSON struct {
ID apijson.Field
CreatedAt apijson.Field
ExternalConnections apijson.Field
Metadata apijson.Field
Name apijson.Field
ArchivedAt apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Item) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r itemJSON) RawJSON() string {
return r.raw
}
// Represents a connection between an Item and an external system for invoicing or
// tax calculation purposes.
type ItemExternalConnection struct {
// The name of the external system this item is connected to.
ExternalConnectionName ItemExternalConnectionsExternalConnectionName `json:"external_connection_name" api:"required"`
// The identifier of this item in the external system.
ExternalEntityID string `json:"external_entity_id" api:"required"`
JSON itemExternalConnectionJSON `json:"-"`
}
// itemExternalConnectionJSON contains the JSON metadata for the struct
// [ItemExternalConnection]
type itemExternalConnectionJSON struct {
ExternalConnectionName apijson.Field
ExternalEntityID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ItemExternalConnection) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r itemExternalConnectionJSON) RawJSON() string {
return r.raw
}
// The name of the external system this item is connected to.
type ItemExternalConnectionsExternalConnectionName string
const (
ItemExternalConnectionsExternalConnectionNameStripe ItemExternalConnectionsExternalConnectionName = "stripe"
ItemExternalConnectionsExternalConnectionNameQuickbooks ItemExternalConnectionsExternalConnectionName = "quickbooks"
ItemExternalConnectionsExternalConnectionNameBillCom ItemExternalConnectionsExternalConnectionName = "bill.com"
ItemExternalConnectionsExternalConnectionNameNetsuite ItemExternalConnectionsExternalConnectionName = "netsuite"
ItemExternalConnectionsExternalConnectionNameTaxjar ItemExternalConnectionsExternalConnectionName = "taxjar"
ItemExternalConnectionsExternalConnectionNameAvalara ItemExternalConnectionsExternalConnectionName = "avalara"
ItemExternalConnectionsExternalConnectionNameAnrok ItemExternalConnectionsExternalConnectionName = "anrok"
ItemExternalConnectionsExternalConnectionNameNumeral ItemExternalConnectionsExternalConnectionName = "numeral"
ItemExternalConnectionsExternalConnectionNameStripeTax ItemExternalConnectionsExternalConnectionName = "stripe_tax"
)
func (r ItemExternalConnectionsExternalConnectionName) IsKnown() bool {
switch r {
case ItemExternalConnectionsExternalConnectionNameStripe, ItemExternalConnectionsExternalConnectionNameQuickbooks, ItemExternalConnectionsExternalConnectionNameBillCom, ItemExternalConnectionsExternalConnectionNameNetsuite, ItemExternalConnectionsExternalConnectionNameTaxjar, ItemExternalConnectionsExternalConnectionNameAvalara, ItemExternalConnectionsExternalConnectionNameAnrok, ItemExternalConnectionsExternalConnectionNameNumeral, ItemExternalConnectionsExternalConnectionNameStripeTax:
return true
}
return false
}
type ItemNewParams struct {
// The name of the item.
Name param.Field[string] `json:"name" api:"required"`
// User-specified key/value pairs for the resource. Individual keys can be removed
// by setting the value to `null`, and the entire metadata mapping can be cleared
// by setting `metadata` to `null`.
Metadata param.Field[map[string]string] `json:"metadata"`
}
func (r ItemNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ItemUpdateParams struct {
ExternalConnections param.Field[[]ItemUpdateParamsExternalConnection] `json:"external_connections"`
// User-specified key/value pairs for the resource. Individual keys can be removed
// by setting the value to `null`, and the entire metadata mapping can be cleared
// by setting `metadata` to `null`.
Metadata param.Field[map[string]string] `json:"metadata"`
Name param.Field[string] `json:"name"`
}
func (r ItemUpdateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Represents a connection between an Item and an external system for invoicing or
// tax calculation purposes.
type ItemUpdateParamsExternalConnection struct {
// The name of the external system this item is connected to.
ExternalConnectionName param.Field[ItemUpdateParamsExternalConnectionsExternalConnectionName] `json:"external_connection_name" api:"required"`
// The identifier of this item in the external system.
ExternalEntityID param.Field[string] `json:"external_entity_id" api:"required"`
}
func (r ItemUpdateParamsExternalConnection) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The name of the external system this item is connected to.
type ItemUpdateParamsExternalConnectionsExternalConnectionName string
const (
ItemUpdateParamsExternalConnectionsExternalConnectionNameStripe ItemUpdateParamsExternalConnectionsExternalConnectionName = "stripe"
ItemUpdateParamsExternalConnectionsExternalConnectionNameQuickbooks ItemUpdateParamsExternalConnectionsExternalConnectionName = "quickbooks"
ItemUpdateParamsExternalConnectionsExternalConnectionNameBillCom ItemUpdateParamsExternalConnectionsExternalConnectionName = "bill.com"
ItemUpdateParamsExternalConnectionsExternalConnectionNameNetsuite ItemUpdateParamsExternalConnectionsExternalConnectionName = "netsuite"
ItemUpdateParamsExternalConnectionsExternalConnectionNameTaxjar ItemUpdateParamsExternalConnectionsExternalConnectionName = "taxjar"
ItemUpdateParamsExternalConnectionsExternalConnectionNameAvalara ItemUpdateParamsExternalConnectionsExternalConnectionName = "avalara"
ItemUpdateParamsExternalConnectionsExternalConnectionNameAnrok ItemUpdateParamsExternalConnectionsExternalConnectionName = "anrok"
ItemUpdateParamsExternalConnectionsExternalConnectionNameNumeral ItemUpdateParamsExternalConnectionsExternalConnectionName = "numeral"
ItemUpdateParamsExternalConnectionsExternalConnectionNameStripeTax ItemUpdateParamsExternalConnectionsExternalConnectionName = "stripe_tax"
)
func (r ItemUpdateParamsExternalConnectionsExternalConnectionName) IsKnown() bool {
switch r {
case ItemUpdateParamsExternalConnectionsExternalConnectionNameStripe, ItemUpdateParamsExternalConnectionsExternalConnectionNameQuickbooks, ItemUpdateParamsExternalConnectionsExternalConnectionNameBillCom, ItemUpdateParamsExternalConnectionsExternalConnectionNameNetsuite, ItemUpdateParamsExternalConnectionsExternalConnectionNameTaxjar, ItemUpdateParamsExternalConnectionsExternalConnectionNameAvalara, ItemUpdateParamsExternalConnectionsExternalConnectionNameAnrok, ItemUpdateParamsExternalConnectionsExternalConnectionNameNumeral, ItemUpdateParamsExternalConnectionsExternalConnectionNameStripeTax:
return true
}
return false
}
type ItemListParams struct {
// Cursor for pagination. This can be populated by the `next_cursor` value returned
// from the initial request.
Cursor param.Field[string] `query:"cursor"`
// The number of items to fetch. Defaults to 20.
Limit param.Field[int64] `query:"limit"`
}
// URLQuery serializes [ItemListParams]'s query parameters as `url.Values`.
func (r ItemListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}