-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
106 lines (95 loc) · 3.09 KB
/
service.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
package browse
import (
"github.com/go-resty/resty/v2"
"strconv"
"strings"
"time"
)
type Service struct {
marketplace string
appToken string
timeout time.Duration
limit int
isSandbox bool
}
// NewService creates new Service.
// It requires already created Application Token.
// You can use this repo to create token https://github.com/hotafrika/ebay-common
// More info here https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html
func NewService(appToken string) *Service {
s := Service{appToken: appToken}
s.WithTimeout(10 * time.Second)
s.WithMarketplace(MarketplaceUSA)
s.WithLimit(50)
return &s
}
// WithTimeout sets timeout for HTTP client.
// The same timeout will be for all child requests.
func (s *Service) WithTimeout(timeout time.Duration) *Service {
s.timeout = timeout
return s
}
// WithMarketplace sets marketplace header for HTTP client.
// The same header will be for all child requests.
func (s *Service) WithMarketplace(marketplace EbayMarketplace) *Service {
s.marketplace = string(marketplace)
return s
}
// WithApplicationToken sets application token (Authorization) header for HTTP client.
// The same header will be for all child requests.
func (s *Service) WithApplicationToken(token string) *Service {
s.appToken = token
return s
}
// WithSandbox tells to Service to create Sandbox requests.
func (s *Service) WithSandbox(isSandbox bool) *Service {
s.isSandbox = isSandbox
return s
}
// WithLimit tells to Service to create requests with specified limit per page.
// Default: 50
func (s *Service) WithLimit(limit int) *Service {
s.limit = limit
return s
}
// NewItemSummarySearchRequest creates new ItemSummarySearchRequest
func (s *Service) NewItemSummarySearchRequest() *ItemSummarySearchRequest {
r := &ItemSummarySearchRequest{}
r.url = URLProductionItemSummarySearch
if s.isSandbox {
r.url = URLSandboxItemSummarySearch
}
r.client = resty.New().
SetTimeout(s.timeout).
SetHeader("X-EBAY-C-MARKETPLACE-ID", s.marketplace).
SetHeader("Authorization", strings.Join([]string{"Bearer", s.appToken}, " "))
r.Limit = strconv.Itoa(s.limit)
r.FilterMap = make(map[string]Filterer)
return r
}
// NewGetItemByLegacyIdRequest creates new GetItemByLegacyIdRequest
func (s *Service) NewGetItemByLegacyIdRequest() *GetItemByLegacyIdRequest {
r := &GetItemByLegacyIdRequest{}
r.url = URLProductionGetItemByLegacyId
if s.isSandbox {
r.url = URLSandboxGetItemByLegacyId
}
r.client = resty.New().
SetTimeout(s.timeout).
SetHeader("X-EBAY-C-MARKETPLACE-ID", s.marketplace).
SetHeader("Authorization", strings.Join([]string{"Bearer", s.appToken}, " "))
return r
}
// NewGetItemsByItemGroupRequest creates new GetItemsByItemGroupRequest
func (s *Service) NewGetItemsByItemGroupRequest() *GetItemsByItemGroupRequest {
r := &GetItemsByItemGroupRequest{}
r.url = URLProductionGetItemsByItemGroup
if s.isSandbox {
r.url = URLSandboxGetItemsByItemGroup
}
r.client = resty.New().
SetTimeout(s.timeout).
SetHeader("X-EBAY-C-MARKETPLACE-ID", s.marketplace).
SetHeader("Authorization", strings.Join([]string{"Bearer", s.appToken}, " "))
return r
}