-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
169 lines (152 loc) · 5.2 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
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
package finding
import (
"github.com/go-resty/resty/v2"
"time"
)
// Service represents Ebay Finding API service
type Service struct {
version string
endpoint string
globalID string
securityAppName string
timeout time.Duration
pageLimit int
}
// NewService creates new Ebay Finding API service
// API version according to EbayFindingAPIVersion (1.13.0)
// Default endpoint: EbayEndpointProduction (https://svcs.ebay.com/services/search/FindingService/v1)
// Default GlobalID: GlobalIDEbayUS (EBAY-US)
// Default Page Limit: DefaultItemsPerPage (100)
// Default timeout for requests: 10 seconds
func NewService(securityAppName string) *Service {
s := &Service{
version: EbayFindingAPIVersion,
securityAppName: securityAppName,
timeout: 10 * time.Second,
}
s.WithEndpoint(EbayEndpointProduction)
s.WithGlobalID(GlobalIDEbayUS)
s.WithPageLimit(DefaultItemsPerPage)
return s
}
// WithEndpoint changes endpoint for service
// You can add your own endpoint (for tests purposes)
func (s *Service) WithEndpoint(endpoint string) *Service {
s.endpoint = endpoint
return s
}
// WithGlobalID changes site for search
func (s *Service) WithGlobalID(globalID GlobalID) *Service {
s.globalID = string(globalID)
return s
}
// WithTimeout changes default timeout for search requests
func (s *Service) WithTimeout(timeout time.Duration) *Service {
s.timeout = timeout
return s
}
// WithPageLimit changes limit of items for any child req
func (s *Service) WithPageLimit(limit int) *Service {
if limit < 1 {
limit = 1
} else {
if limit > 100 {
limit = 100
}
}
s.pageLimit = limit
return s
}
// creates new http client (resty)
func (s *Service) newHTTPClient() *resty.Client {
return resty.New().
SetHeader("X-EBAY-SOA-SERVICE-VERSION", s.version).
//SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationFindItemsAdvanced)).
SetHeader("X-EBAY-SOA-SECURITY-APPNAME", s.securityAppName).
SetHeader("X-EBAY-SOA-REQUEST-DATA-FORMAT", EbayRequestDataFormat).
SetHeader("X-EBAY-SOA-RESPONSE-DATA-FORMAT", EbayResponseDataFormat).
SetHeader("X-EBAY-SOA-GLOBAL-ID", s.globalID).
SetTimeout(s.timeout)
}
// NewAdvancedRequest creates new AdvancedRequest
func (s *Service) NewAdvancedRequest() *AdvancedRequest {
req := AdvancedRequest{}
req.Initialize()
//req.Client = resty.New().
// SetHeader("X-EBAY-SOA-SERVICE-VERSION", s.version).
// SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationFindItemsAdvanced)).
// SetHeader("X-EBAY-SOA-SECURITY-APPNAME", s.securityAppName).
// SetHeader("X-EBAY-SOA-REQUEST-DATA-FORMAT", EbayRequestDataFormat).
// SetHeader("X-EBAY-SOA-RESPONSE-DATA-FORMAT", EbayResponseDataFormat).
// SetHeader("X-EBAY-SOA-GLOBAL-ID", s.globalID).
// SetTimeout(s.timeout)
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationFindItemsAdvanced))
req.URL = s.endpoint
req.WithPageLimit(s.pageLimit)
return &req
}
// NewByCategoryRequest creates new ByCategoryRequest
func (s *Service) NewByCategoryRequest() *ByCategoryRequest {
req := ByCategoryRequest{}
req.Initialize()
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationFindItemsByCategory))
req.URL = s.endpoint
req.WithPageLimit(s.pageLimit)
return &req
}
// NewByKeywordsRequest creates new ByKeywordsRequest
func (s *Service) NewByKeywordsRequest() *ByKeywordsRequest {
req := ByKeywordsRequest{}
req.Initialize()
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationFindItemsByKeywords))
req.URL = s.endpoint
req.WithPageLimit(s.pageLimit)
return &req
}
// NewByProductRequest creates new ByProductRequest
func (s *Service) NewByProductRequest() *ByProductRequest {
req := ByProductRequest{}
req.Initialize()
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationFindItemsByProduct))
req.URL = s.endpoint
req.WithPageLimit(s.pageLimit)
return &req
}
// NewInEbayStoresRequest creates new InEbayStoresRequest
func (s *Service) NewInEbayStoresRequest() *InEbayStoresRequest {
req := InEbayStoresRequest{}
req.Initialize()
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationFindItemsIneBayStores))
req.URL = s.endpoint
req.WithPageLimit(s.pageLimit)
return &req
}
// NewGetHistogramsRequest creates new GetHistogramsRequest
func (s *Service) NewGetHistogramsRequest() *GetHistogramsRequest {
req := GetHistogramsRequest{}
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationGetHistograms))
req.URL = s.endpoint
return &req
}
// NewGetKeywordsRecommendationRequest creates new GetKeywordsRecommendationRequest
func (s *Service) NewGetKeywordsRecommendationRequest() *GetKeywordsRecommendationRequest {
req := GetKeywordsRecommendationRequest{}
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationGetSearchKeywordsRecommendation))
req.URL = s.endpoint
return &req
}
// NewGetVersionRequest creates new GetVersionRequest
func (s *Service) NewGetVersionRequest() *GetVersionRequest {
req := GetVersionRequest{}
req.Client = s.newHTTPClient().
SetHeader("X-EBAY-SOA-OPERATION-NAME", string(OperationGetVersion))
req.URL = s.endpoint
return &req
}