Repository navigation
Expand file tree
/
Copy pathclient_layer.go
More file actions
953 lines (864 loc) · 34.8 KB
/
Copy pathclient_layer.go
File metadata and controls
953 lines (864 loc) · 34.8 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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
package controlresell
import (
"bytes"
"context"
"errors"
"io"
"iter"
"net/http"
"os"
"time"
)
// Version is the version of this SDK.
const Version = "1.0.0"
// DefaultBaseURL is the production address of the API.
const DefaultBaseURL = "https://api.controlresell.com"
// APIKeyVariable is the environment variable read when no API key is given.
const APIKeyVariable = "CONTROLRESELL_API_KEY"
type options struct {
apiKey string
baseURL string
retry RetryPolicy
http *http.Client
}
// Option configures a Client.
type Option func(*options)
// WithAPIKey sets the API key; absent, the CONTROLRESELL_API_KEY environment variable is read.
func WithAPIKey(key string) Option { return func(o *options) { o.apiKey = key } }
// WithBaseURL sets the API's address (staging, a test environment).
func WithBaseURL(url string) Option { return func(o *options) { o.baseURL = url } }
// WithRetryPolicy sets how calls are retried.
func WithRetryPolicy(policy RetryPolicy) Option { return func(o *options) { o.retry = policy } }
// WithHTTPClient sets the http.Client to call through (proxy, timeouts...); its transport gets the retries.
func WithHTTPClient(client *http.Client) Option { return func(o *options) { o.http = client } }
// Client is a client of the ControlResell public API, for one API key. It holds no global state: create one per API
// key. Calls are retried on a rate limit (after Retry-After), on a gateway or availability error (502, 503, 504), on
// a 409 idempotency_in_progress and on a network error; a POST always carries the same Idempotency-Key across its
// retries. Any other error is returned as a typed error (*APIError and its per-status types).
//
// client, err := controlresell.NewClient(controlresell.WithAPIKey("cr_live_..."))
// me, err := client.Me.Get(ctx)
// for item, err := range client.Items.All(ctx, controlresell.ItemListParams{}) { ... }
type Client struct {
// Me is the seller the API key acts for.
Me *MeService
// Accounts are the seller's marketplace accounts.
Accounts *AccountsService
// Items are the items of the seller's stock.
Items *ItemsService
// Conversations are the conversations with buyers.
Conversations *ConversationsService
// Orders are the seller's sales and purchases.
Orders *OrdersService
// Customers are the other parties of the seller's sales and purchases.
Customers *CustomersService
// Files are the photos of the seller's library: register one, send its bytes, read it.
Files *FilesService
// Favorites are the buyers who put the seller's listings in their favorites.
Favorites *FavoritesService
// Catalogs are the identifiers an item is written with: categories, brands, sizes, colors...
Catalogs *CatalogsService
// Webhooks are the seller's webhook endpoints, their deliveries and the recent events.
Webhooks *WebhooksService
// API is the underlying generated client, configured with the key and the retries.
API *APIClient
}
// NewClient creates a client.
func NewClient(opts ...Option) (*Client, error) {
o := options{baseURL: DefaultBaseURL, retry: DefaultRetryPolicy()}
for _, opt := range opts {
opt(&o)
}
if o.apiKey == "" {
o.apiKey = os.Getenv(APIKeyVariable)
}
if o.apiKey == "" {
return nil, errors.New("controlresell: no API key: use WithAPIKey or set " + APIKeyVariable)
}
httpClient := &http.Client{Timeout: 60 * time.Second}
if o.http != nil {
copied := *o.http
httpClient = &copied
}
base := httpClient.Transport
if base == nil {
base = http.DefaultTransport
}
httpClient.Transport = &retryTransport{base: base, policy: o.retry}
configuration := NewConfiguration()
configuration.Servers = ServerConfigurations{{URL: o.baseURL}}
configuration.UserAgent = "controlresell-go/" + Version
configuration.HTTPClient = httpClient
configuration.AddDefaultHeader("Authorization", "Bearer "+o.apiKey)
api := NewAPIClient(configuration)
return &Client{
Me: &MeService{api},
Accounts: &AccountsService{api},
Items: &ItemsService{api},
Conversations: &ConversationsService{api},
Orders: &OrdersService{api},
Customers: &CustomersService{api},
Files: &FilesService{api},
Favorites: &FavoritesService{api},
Catalogs: &CatalogsService{api},
Webhooks: &WebhooksService{api},
API: api,
}, nil
}
// MeService reads the seller the API key acts for.
type MeService struct{ api *APIClient }
// Get returns the seller.
func (s *MeService) Get(ctx context.Context) (*Me, error) {
value, response, err := s.api.MeAPI.GetMe(ctx).Execute()
return result(value, response, err)
}
// AccountsService reads the seller's marketplace accounts.
type AccountsService struct{ api *APIClient }
// ListParams are the paging parameters of a list: the zero value reads the first page with the default size.
type ListParams struct {
// Limit is the page size (1 to 100); 0 for the default.
Limit int32
// Cursor is the opaque cursor of the page to read; empty for the first one.
Cursor string
}
// List reads one page of accounts.
func (s *AccountsService) List(ctx context.Context, params ListParams) (*AccountPage, error) {
request := s.api.AccountsAPI.ListAccounts(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// Get reads one account by its identifier.
func (s *AccountsService) Get(ctx context.Context, id string) (*Account, error) {
value, response, err := s.api.AccountsAPI.GetAccount(ctx, id).Execute()
return result(value, response, err)
}
// All yields every account, page after page.
func (s *AccountsService) All(ctx context.Context, limit int32) iter.Seq2[Account, error] {
return paginate(func(cursor string) ([]Account, *string, error) {
page, err := s.List(ctx, ListParams{Limit: limit, Cursor: cursor})
if err != nil {
return nil, nil, err
}
return page.Data, page.NextCursor.Get(), nil
})
}
// ItemsService reads the items of the seller's stock.
type ItemsService struct{ api *APIClient }
// ItemListParams are the parameters of an item list.
type ItemListParams struct {
ListParams
// Sold keeps the sold (true) or the unsold (false) items; nil for both.
Sold *bool
// AccountID keeps the items listed on one marketplace account; empty for all.
AccountID string
}
// List reads one page of items.
func (s *ItemsService) List(ctx context.Context, params ItemListParams) (*ItemPage, error) {
request := s.api.ItemsAPI.ListItems(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.Sold != nil {
request = request.Sold(*params.Sold)
}
if params.AccountID != "" {
request = request.AccountId(params.AccountID)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// Get reads one item by its identifier.
func (s *ItemsService) Get(ctx context.Context, id int64) (*Item, error) {
value, response, err := s.api.ItemsAPI.GetItem(ctx, id).Execute()
return result(value, response, err)
}
// Listings reads where an item is listed on the seller's marketplace accounts.
func (s *ItemsService) Listings(ctx context.Context, id int64) (*ListingPage, error) {
value, response, err := s.api.ItemsAPI.ListItemListings(ctx, id).Execute()
return result(value, response, err)
}
// Create adds an item to the stock, without publishing it. opts may set the Idempotency-Key: to retry a Create yourself
// (after a timeout, say), pass the same WithIdempotencyKey, or a second item is created.
func (s *ItemsService) Create(ctx context.Context, body ItemCreateRequest, opts ...WriteOption) (*Item, error) {
value, response, err := s.api.ItemsAPI.CreateItem(ctx).ItemCreateRequest(body).IdempotencyKey(keyOf(opts)).Execute()
return result(value, response, err)
}
// Update changes the given fields of an item (a PATCH: the fields left out are kept).
func (s *ItemsService) Update(ctx context.Context, id int64, body ItemUpdateRequest) (*Item, error) {
value, response, err := s.api.ItemsAPI.UpdateItem(ctx, id).ItemUpdateRequest(body).Execute()
return result(value, response, err)
}
// Delete removes an item from the stock; an item still online is refused (409 item_still_listed).
func (s *ItemsService) Delete(ctx context.Context, id int64) error {
response, err := s.api.ItemsAPI.DeleteItem(ctx, id).Execute()
if err != nil {
return convert(response, err)
}
return nil
}
// Publish schedules the publication of an item on the given accounts. opts may set the Idempotency-Key.
func (s *ItemsService) Publish(ctx context.Context, id int64, body PublishRequest, opts ...WriteOption) (*ScheduledAction, error) {
value, response, err := s.api.ItemsAPI.PublishItem(ctx, id).PublishRequest(body).IdempotencyKey(keyOf(opts)).Execute()
return result(value, response, err)
}
// PriceDrop schedules a price drop of the item's listings. opts may set the Idempotency-Key.
func (s *ItemsService) PriceDrop(ctx context.Context, id int64, body PriceDropRequest, opts ...WriteOption) (*ScheduledAction, error) {
value, response, err := s.api.ItemsAPI.DropItemPrice(ctx, id).PriceDropRequest(body).IdempotencyKey(keyOf(opts)).Execute()
return result(value, response, err)
}
// Republish schedules the republication of an item (the zero RepublishRequest means every account it is listed on).
// opts may set the Idempotency-Key.
func (s *ItemsService) Republish(ctx context.Context, id int64, body RepublishRequest, opts ...WriteOption) (*ScheduledAction, error) {
value, response, err := s.api.ItemsAPI.RepublishItem(ctx, id).RepublishRequest(body).IdempotencyKey(keyOf(opts)).Execute()
return result(value, response, err)
}
// All yields every item, page after page, with the same filters (params.Cursor is ignored).
func (s *ItemsService) All(ctx context.Context, params ItemListParams) iter.Seq2[Item, error] {
return paginate(func(cursor string) ([]Item, *string, error) {
page := params
page.Cursor = cursor
result, err := s.List(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// ConversationsService reads the conversations with buyers.
type ConversationsService struct{ api *APIClient }
// ConversationListParams are the parameters of a conversation list.
type ConversationListParams struct {
ListParams
// Unread keeps the unread (true) or the read (false) conversations; nil for both.
Unread *bool
// AccountID keeps the conversations of one marketplace account; empty for all.
AccountID string
// From keeps the conversations whose last message is at or after this instant (inclusive); zero for no bound.
From time.Time
// To keeps the conversations whose last message is before this instant (exclusive); zero for no bound.
To time.Time
// BuyerID keeps the conversations with one buyer, by their identifier on the marketplace; empty for all.
BuyerID string
// ItemID keeps the conversations about one item, through its transactions; 0 for all.
ItemID int64
// OfferStatus keeps the conversations with at least one offer at this status (PENDING, ACCEPTED, REJECTED or CANCELED); empty for all.
OfferStatus string
}
// List reads one page of conversations.
func (s *ConversationsService) List(ctx context.Context, params ConversationListParams) (*ConversationPage, error) {
request := s.api.ConversationsAPI.ListConversations(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.Unread != nil {
request = request.Unread(*params.Unread)
}
if params.AccountID != "" {
request = request.AccountId(params.AccountID)
}
if !params.From.IsZero() {
request = request.From(params.From)
}
if !params.To.IsZero() {
request = request.To(params.To)
}
if params.BuyerID != "" {
request = request.BuyerId(params.BuyerID)
}
if params.ItemID != 0 {
request = request.ItemId(params.ItemID)
}
if params.OfferStatus != "" {
request = request.OfferStatus(params.OfferStatus)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// Get reads one conversation by its identifier.
func (s *ConversationsService) Get(ctx context.Context, id string) (*Conversation, error) {
value, response, err := s.api.ConversationsAPI.GetConversation(ctx, id).Execute()
return result(value, response, err)
}
// MessageListParams are the parameters of a message list.
type MessageListParams struct {
ListParams
// From keeps the messages created at or after this instant (inclusive); zero for no bound.
From time.Time
// To keeps the messages created before this instant (exclusive); zero for no bound.
To time.Time
}
// Messages reads one page of the messages of a conversation, oldest first.
func (s *ConversationsService) Messages(ctx context.Context, id string, params MessageListParams) (*MessagePage, error) {
request := s.api.ConversationsAPI.ListConversationMessages(ctx, id)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if !params.From.IsZero() {
request = request.From(params.From)
}
if !params.To.IsZero() {
request = request.To(params.To)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// SendMessage sends a message to the buyer. opts may set the Idempotency-Key.
func (s *ConversationsService) SendMessage(ctx context.Context, id string, body SendMessageRequest, opts ...WriteOption) (*Message, error) {
value, response, err := s.api.ConversationsAPI.SendMessage(ctx, id).SendMessageRequest(body).IdempotencyKey(keyOf(opts)).Execute()
return result(value, response, err)
}
// SendOffer sends the buyer an offer. opts may set the Idempotency-Key.
func (s *ConversationsService) SendOffer(ctx context.Context, id string, body SendOfferRequest, opts ...WriteOption) (*Message, error) {
value, response, err := s.api.ConversationsAPI.SendOffer(ctx, id).SendOfferRequest(body).IdempotencyKey(keyOf(opts)).Execute()
return result(value, response, err)
}
// All yields every conversation, page after page, with the same filters (params.Cursor is ignored).
func (s *ConversationsService) All(ctx context.Context, params ConversationListParams) iter.Seq2[Conversation, error] {
return paginate(func(cursor string) ([]Conversation, *string, error) {
page := params
page.Cursor = cursor
result, err := s.List(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// AllMessages yields every message of a conversation, page after page, with the same period (params.Cursor is ignored).
func (s *ConversationsService) AllMessages(ctx context.Context, id string, params MessageListParams) iter.Seq2[Message, error] {
return paginate(func(cursor string) ([]Message, *string, error) {
query := params
query.Cursor = cursor
page, err := s.Messages(ctx, id, query)
if err != nil {
return nil, nil, err
}
return page.Data, page.NextCursor.Get(), nil
})
}
// OrdersService reads the seller's sales and purchases.
type OrdersService struct{ api *APIClient }
// OrderListParams are the parameters of an order list.
type OrderListParams struct {
ListParams
// Bucket is "to_send" (the sales to ship) or "to_pickup" (the purchases waiting at a pickup point); empty for all.
Bucket string
// Platform keeps the orders of the accounts on one marketplace ("VINTED", "SHOPIFY"...); empty for all.
Platform string
// AccountID keeps the orders of one marketplace account; empty for all.
AccountID string
// From keeps the orders dated at or after this instant (inclusive); zero for no bound.
From time.Time
// To keeps the orders dated before this instant (exclusive); zero for no bound.
To time.Time
}
// List reads one page of orders, the most recent first.
func (s *OrdersService) List(ctx context.Context, params OrderListParams) (*OrderPage, error) {
request := s.api.OrdersAPI.ListOrders(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.Bucket != "" {
request = request.Bucket(params.Bucket)
}
if params.Platform != "" {
request = request.Platform(params.Platform)
}
if params.AccountID != "" {
request = request.AccountId(params.AccountID)
}
if !params.From.IsZero() {
request = request.From(params.From)
}
if !params.To.IsZero() {
request = request.To(params.To)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// Get reads one order by its identifier.
func (s *OrdersService) Get(ctx context.Context, id string) (*Order, error) {
value, response, err := s.api.OrdersAPI.GetOrder(ctx, id).Execute()
return result(value, response, err)
}
// Labels reads the shipping labels of an order.
func (s *OrdersService) Labels(ctx context.Context, id string) (*OrderLabelPage, error) {
value, response, err := s.api.OrdersAPI.ListOrderLabels(ctx, id).Execute()
return result(value, response, err)
}
// LabelsToShip reads one page of the shipping labels still to ship over all the orders, the oldest order first; each
// label names its order (OrderId).
func (s *OrdersService) LabelsToShip(ctx context.Context, params ListParams) (*OrderLabelPage, error) {
request := s.api.OrdersAPI.ListLabelsToShip(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllLabelsToShip yields every label still to ship, page after page (params.Cursor is ignored).
func (s *OrdersService) AllLabelsToShip(ctx context.Context, params ListParams) iter.Seq2[OrderLabel, error] {
return paginate(func(cursor string) ([]OrderLabel, *string, error) {
page := params
page.Cursor = cursor
result, err := s.LabelsToShip(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// All yields every order, page after page, with the same filters (params.Cursor is ignored).
func (s *OrdersService) All(ctx context.Context, params OrderListParams) iter.Seq2[Order, error] {
return paginate(func(cursor string) ([]Order, *string, error) {
page := params
page.Cursor = cursor
result, err := s.List(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// CustomersService reads the other parties of the seller's sales and purchases (needs the customers:read scope).
type CustomersService struct{ api *APIClient }
// CustomerListParams are the parameters of a customer list.
type CustomerListParams struct {
ListParams
// AccountID keeps the customers of an order on one marketplace account; empty for all.
AccountID string
// From keeps the customers created at or after this instant (inclusive); zero for no bound.
From time.Time
// To keeps the customers created before this instant (exclusive); zero for no bound.
To time.Time
}
// List reads one page of customers, the most recently created first.
func (s *CustomersService) List(ctx context.Context, params CustomerListParams) (*CustomerPage, error) {
request := s.api.CustomersAPI.ListCustomers(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.AccountID != "" {
request = request.AccountId(params.AccountID)
}
if !params.From.IsZero() {
request = request.From(params.From)
}
if !params.To.IsZero() {
request = request.To(params.To)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// Get reads one customer by its identifier.
func (s *CustomersService) Get(ctx context.Context, id string) (*Customer, error) {
value, response, err := s.api.CustomersAPI.GetCustomer(ctx, id).Execute()
return result(value, response, err)
}
// All yields every customer, page after page, with the same filters (params.Cursor is ignored).
func (s *CustomersService) All(ctx context.Context, params CustomerListParams) iter.Seq2[Customer, error] {
return paginate(func(cursor string) ([]Customer, *string, error) {
page := params
page.Cursor = cursor
result, err := s.List(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// CatalogsService reads the catalog an item is written with (needs the items:read scope).
type CatalogsService struct{ api *APIClient }
// CatalogListParams are the parameters of a catalog list.
type CatalogListParams struct {
ListParams
// CategoryID keeps what applies to one category (sizes, colors, conditions, package sizes, materials); 0 for all.
CategoryID int64
// ParentID keeps the children of one category (categories only); 0 for all.
ParentID int64
// Platform keeps the categories of one marketplace (categories only); empty for all.
Platform string
// Q keeps the brands whose name starts with it (brands only); empty for all.
Q string
}
// Categories reads one page of categories, flat, by identifier.
func (s *CatalogsService) Categories(ctx context.Context, params CatalogListParams) (*CatalogCategoryPage, error) {
request := s.api.CatalogsAPI.ListCatalogCategories(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.ParentID > 0 {
request = request.ParentId(params.ParentID)
}
if params.Platform != "" {
request = request.Platform(params.Platform)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllCategories yields all the categories, page after page, with the same filters (params.Cursor is ignored).
func (s *CatalogsService) AllCategories(ctx context.Context, params CatalogListParams) iter.Seq2[CatalogCategory, error] {
return paginate(func(cursor string) ([]CatalogCategory, *string, error) {
page := params
page.Cursor = cursor
result, err := s.Categories(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// Category reads one category by its identifier.
func (s *CatalogsService) Category(ctx context.Context, id int64) (*CatalogCategory, error) {
value, response, err := s.api.CatalogsAPI.GetCatalogCategory(ctx, id).Execute()
return result(value, response, err)
}
// CategoryFields reads what an item of a category is filled with: its size groups, enumerated and text fields, measurements.
func (s *CatalogsService) CategoryFields(ctx context.Context, id int64) (*CatalogCategoryFields, error) {
value, response, err := s.api.CatalogsAPI.GetCatalogCategoryFields(ctx, id).Execute()
return result(value, response, err)
}
// Brands reads one page of brands, by identifier or, with params.Q, by name.
func (s *CatalogsService) Brands(ctx context.Context, params CatalogListParams) (*CatalogBrandPage, error) {
request := s.api.CatalogsAPI.ListCatalogBrands(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.Q != "" {
request = request.Q(params.Q)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllBrands yields all the brands, page after page, with the same filters (params.Cursor is ignored).
func (s *CatalogsService) AllBrands(ctx context.Context, params CatalogListParams) iter.Seq2[CatalogBrand, error] {
return paginate(func(cursor string) ([]CatalogBrand, *string, error) {
page := params
page.Cursor = cursor
result, err := s.Brands(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// Sizes reads one page of sizes, each with its size group; params.CategoryID keeps those of one category.
func (s *CatalogsService) Sizes(ctx context.Context, params CatalogListParams) (*CatalogSizePage, error) {
request := s.api.CatalogsAPI.ListCatalogSizes(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.CategoryID > 0 {
request = request.CategoryId(params.CategoryID)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllSizes yields all the sizes, page after page, with the same filters (params.Cursor is ignored).
func (s *CatalogsService) AllSizes(ctx context.Context, params CatalogListParams) iter.Seq2[CatalogSize, error] {
return paginate(func(cursor string) ([]CatalogSize, *string, error) {
page := params
page.Cursor = cursor
result, err := s.Sizes(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// Colors reads one page of colors; params.CategoryID keeps those of one category.
func (s *CatalogsService) Colors(ctx context.Context, params CatalogListParams) (*CatalogValuePage, error) {
request := s.api.CatalogsAPI.ListCatalogColors(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.CategoryID > 0 {
request = request.CategoryId(params.CategoryID)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllColors yields all the colors, page after page, with the same filters (params.Cursor is ignored).
func (s *CatalogsService) AllColors(ctx context.Context, params CatalogListParams) iter.Seq2[CatalogValue, error] {
return paginate(func(cursor string) ([]CatalogValue, *string, error) {
page := params
page.Cursor = cursor
result, err := s.Colors(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// Conditions reads one page of conditions; params.CategoryID keeps those of one category.
func (s *CatalogsService) Conditions(ctx context.Context, params CatalogListParams) (*CatalogValuePage, error) {
request := s.api.CatalogsAPI.ListCatalogConditions(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.CategoryID > 0 {
request = request.CategoryId(params.CategoryID)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllConditions yields all the conditions, page after page, with the same filters (params.Cursor is ignored).
func (s *CatalogsService) AllConditions(ctx context.Context, params CatalogListParams) iter.Seq2[CatalogValue, error] {
return paginate(func(cursor string) ([]CatalogValue, *string, error) {
page := params
page.Cursor = cursor
result, err := s.Conditions(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// PackageSizes reads one page of package sizes; params.CategoryID keeps those of one category.
func (s *CatalogsService) PackageSizes(ctx context.Context, params CatalogListParams) (*CatalogValuePage, error) {
request := s.api.CatalogsAPI.ListCatalogPackageSizes(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.CategoryID > 0 {
request = request.CategoryId(params.CategoryID)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllPackageSizes yields all the package sizes, page after page, with the same filters (params.Cursor is ignored).
func (s *CatalogsService) AllPackageSizes(ctx context.Context, params CatalogListParams) iter.Seq2[CatalogValue, error] {
return paginate(func(cursor string) ([]CatalogValue, *string, error) {
page := params
page.Cursor = cursor
result, err := s.PackageSizes(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// Materials reads one page of materials; params.CategoryID keeps those of one category.
func (s *CatalogsService) Materials(ctx context.Context, params CatalogListParams) (*CatalogValuePage, error) {
request := s.api.CatalogsAPI.ListCatalogMaterials(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.CategoryID > 0 {
request = request.CategoryId(params.CategoryID)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllMaterials yields all the materials, page after page, with the same filters (params.Cursor is ignored).
func (s *CatalogsService) AllMaterials(ctx context.Context, params CatalogListParams) iter.Seq2[CatalogValue, error] {
return paginate(func(cursor string) ([]CatalogValue, *string, error) {
page := params
page.Cursor = cursor
result, err := s.Materials(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// WriteOption configures a write call.
type WriteOption func(*writeOptions)
type writeOptions struct{ idempotencyKey string }
// WithIdempotencyKey sets the Idempotency-Key of a write (a random one by default). The key is the same at every retry.
func WithIdempotencyKey(key string) WriteOption {
return func(o *writeOptions) { o.idempotencyKey = key }
}
// keyOf returns the Idempotency-Key of a write: the caller's, else a random one chosen once, before the retries.
func keyOf(opts []WriteOption) string {
var o writeOptions
for _, opt := range opts {
opt(&o)
}
if o.idempotencyKey != "" {
return o.idempotencyKey
}
return newKey()
}
func result[T any](value *T, response *http.Response, err error) (*T, error) {
if err != nil {
return nil, convert(response, err)
}
return value, nil
}
// FilesService registers the photos of the seller's library (needs items:write, items:read to read one). Register a file,
// send its bytes to the returned address, then put its identifier in PhotoIds of an item.
type FilesService struct{ api *APIClient }
// Register records a file (jpeg, png, webp, heic or heif, at most 20 MiB) and returns its identifier and the address
// where to PUT its bytes within 30 minutes. opts may set the Idempotency-Key.
func (s *FilesService) Register(ctx context.Context, body FileUploadRequest, opts ...WriteOption) (*FileUpload, error) {
value, response, err := s.api.FilesAPI.UploadFile(ctx).FileUploadRequest(body).IdempotencyKey(keyOf(opts)).Execute()
return result(value, response, err)
}
// Get reads one file of the library, with a signed link to read it; its Status is PENDING until its bytes are stored.
func (s *FilesService) Get(ctx context.Context, id string) (*File, error) {
value, response, err := s.api.FilesAPI.GetFile(ctx, id).Execute()
return result(value, response, err)
}
// Upload registers a file and sends its bytes (an HTTP PUT with the same Content-Type and no Authorization header),
// and returns the registration: put its Id in PhotoIds. The PUT is retried like any call; a refused one is a typed
// error (the file stays PENDING).
func (s *FilesService) Upload(ctx context.Context, filename, contentType string, data []byte, opts ...WriteOption) (*FileUpload, error) {
upload, err := s.Register(ctx, *NewFileUploadRequest(filename, contentType, int64(len(data))), opts...)
if err != nil {
return nil, err
}
if upload.UploadUrl == "" {
return nil, errors.New("controlresell: the API returned no upload address for file " + upload.Id)
}
request, err := http.NewRequestWithContext(ctx, http.MethodPut, upload.UploadUrl, bytes.NewReader(data))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", contentType)
request.Header.Set("User-Agent", s.api.GetConfig().UserAgent)
response, err := s.api.GetConfig().HTTPClient.Do(request)
if err != nil {
return nil, convert(response, err)
}
defer response.Body.Close()
if response.StatusCode/100 != 2 {
body, _ := io.ReadAll(io.LimitReader(response.Body, 4096))
return nil, errorOf(response.StatusCode, body, response.Header)
}
_, _ = io.Copy(io.Discard, response.Body)
return upload, nil
}
// FavoritesService reads the buyers who put the seller's listings in their favorites (needs the items:read scope).
type FavoritesService struct{ api *APIClient }
// FavoriteListParams are the parameters of a favorite list.
type FavoriteListParams struct {
ListParams
// AccountID keeps the favorites of the listings of one marketplace account; empty for all.
AccountID string
// ItemID keeps the favorites of the listings of one item; 0 for all.
ItemID int64
// From keeps the favorites created at or after this instant (inclusive); zero for no bound.
From time.Time
// To keeps the favorites created before this instant (exclusive); zero for no bound.
To time.Time
}
// List reads one page of favorites, the most recent first.
func (s *FavoritesService) List(ctx context.Context, params FavoriteListParams) (*FavoritePage, error) {
request := s.api.FavoritesAPI.ListFavorites(ctx)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if params.AccountID != "" {
request = request.AccountId(params.AccountID)
}
if params.ItemID != 0 {
request = request.ItemId(params.ItemID)
}
if !params.From.IsZero() {
request = request.From(params.From)
}
if !params.To.IsZero() {
request = request.To(params.To)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// All yields every favorite, page after page, with the same filters (params.Cursor is ignored).
func (s *FavoritesService) All(ctx context.Context, params FavoriteListParams) iter.Seq2[Favorite, error] {
return paginate(func(cursor string) ([]Favorite, *string, error) {
page := params
page.Cursor = cursor
result, err := s.List(ctx, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}
// ItemFavoriteListParams are the parameters of the favorite list of one item.
type ItemFavoriteListParams struct {
ListParams
// From keeps the favorites created at or after this instant (inclusive); zero for no bound.
From time.Time
// To keeps the favorites created before this instant (exclusive); zero for no bound.
To time.Time
}
// ListItem reads one page of the favorites of an item, on every account it is listed on, the most recent first.
func (s *FavoritesService) ListItem(ctx context.Context, id int64, params ItemFavoriteListParams) (*FavoritePage, error) {
request := s.api.FavoritesAPI.ListItemFavorites(ctx, id)
if params.Limit > 0 {
request = request.Limit(params.Limit)
}
if params.Cursor != "" {
request = request.Cursor(params.Cursor)
}
if !params.From.IsZero() {
request = request.From(params.From)
}
if !params.To.IsZero() {
request = request.To(params.To)
}
value, response, err := request.Execute()
return result(value, response, err)
}
// AllItem yields every favorite of an item, page after page, with the same period (params.Cursor is ignored).
func (s *FavoritesService) AllItem(ctx context.Context, id int64, params ItemFavoriteListParams) iter.Seq2[Favorite, error] {
return paginate(func(cursor string) ([]Favorite, *string, error) {
page := params
page.Cursor = cursor
result, err := s.ListItem(ctx, id, page)
if err != nil {
return nil, nil, err
}
return result.Data, result.NextCursor.Get(), nil
})
}