-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
264 lines (213 loc) · 7.71 KB
/
client.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
package amazonpa
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
// ItemLookupQuery describes the allowed parameters for a ItemLookup request
type ItemLookupQuery struct {
Condition string
IDType string
IncludeReviewsSummary string
ItemIDs []string
MerchantID string
RelatedItemPage string
RelationshipType string
SearchIndex string
TruncateReviewsAt string
VariationPage string
ResponseGroups []string
}
// ItemSearchQuery describes the allowed parameters for a ItemSearch request
type ItemSearchQuery struct {
Actor string
Artist string
AudienceRatings []string
Author string
Availability string
Brand string
BrowseNode string
Composer string
Condition string
Conductor string
Director string
IncludeReviewsSummary string
ItemPage string
Keywords string
Manufacturer string
MaximumPrice string
MerchantID string
MinimumPrice string
MinPercentageOff string
Orchestra string
Power string
Publisher string
RelatedItemPage string
RelationshipType string
SearchIndex string
Sort string
Title string
TruncateReviewsAt string
VariationPage string
ResponseGroups []string
}
type BrowseNodeLookupQuery struct {
BrowseNodeID string
ResponseGroups []string
}
// Client provides the functions to interact with the API
type Client struct {
config Config
httpClient *http.Client
}
// NewClient returns a new Client
func NewClient(config Config) *Client {
c := Client{config: config, httpClient: http.DefaultClient}
return &c
}
// SetHTTPClient allows to set a custom *http.Client on the API client
func (client *Client) SetHTTPClient(h *http.Client) {
client.httpClient = h
}
// NewRequest returns a request with basic parameters
func (client Client) NewRequest(operation string) *Request {
request := Request{}
if client.config.Secure {
request.scheme = "https"
} else {
request.scheme = "http"
}
request.endpoint = Endpoints[client.config.Region]
request.endpointURI = EndpointURI
request.parameters = map[string]string{
"Service": "AWSECommerceService",
"AWSAccessKeyId": client.config.AccessKey,
"AssociateTag": client.config.AssociateTag,
"Version": "2013-08-01",
"Operation": operation,
"Timestamp": time.Now().Format(time.RFC3339),
}
return &request
}
// SignRequest produces the signature for the given query string
func (client Client) SignRequest(request *Request) {
signable := fmt.Sprintf("GET\n%s\n%s\n%s", request.endpoint, request.endpointURI, request.QueryString())
hasher := hmac.New(sha256.New, []byte(client.config.AccessSecret))
hasher.Write([]byte(signable))
request.signature = base64.StdEncoding.EncodeToString(hasher.Sum(nil))
}
// ProcessRequest takes a request and queries the API
func (client Client) ProcessRequest(request *Request) ([]byte, error) {
// Sign the request
client.SignRequest(request)
// Execute the HTTP request
var httpResponse *http.Response
var err error
var contents []byte
requestURL, err := request.SignedURL()
if err != nil {
return nil, errors.New("amazonpa: cannot get the signed request URL")
}
httpResponse, err = client.httpClient.Get(requestURL)
if err != nil {
return nil, errors.New("amazonpa: error processing the http request")
}
contents, err = ioutil.ReadAll(httpResponse.Body)
httpResponse.Body.Close()
if err != nil {
return nil, errors.New("amazonpa: error while reading the server response")
}
return contents, nil
}
// ItemLookup performs an ItemLookup request
func (client Client) ItemLookup(query ItemLookupQuery) (*ItemLookupResponse, error) {
request := client.NewRequest("ItemLookup")
request.SetParameter("Condition", query.Condition)
request.SetParameter("IdType", query.IDType)
request.SetParameter("IncludeReviewsSummary", query.IncludeReviewsSummary)
request.SetParameter("ItemId", strings.Join(query.ItemIDs, ","))
request.SetParameter("MerchantId", query.MerchantID)
request.SetParameter("RelatedItemPage", query.RelatedItemPage)
request.SetParameter("RelationshipType", query.RelationshipType)
request.SetParameter("SearchIndex", query.SearchIndex)
request.SetParameter("TruncateReviewsAt", query.TruncateReviewsAt)
request.SetParameter("VariationPage", query.VariationPage)
request.SetParameter("ResponseGroup", strings.Join(query.ResponseGroups, ","))
xmlData, err := client.ProcessRequest(request)
if err != nil {
return nil, err
}
var response ItemLookupResponse
xml.Unmarshal(xmlData, &response)
if response.Items.Request.IsValid != true {
return &response, errors.New("amazonpa: request is invalid")
}
return &response, nil
}
// ItemLookup performs an ItemLookup request
func (client Client) ItemSearch(query ItemSearchQuery) (*ItemSearchResponse, error) {
request := client.NewRequest("ItemSearch")
request.SetParameter("Actor", query.Actor)
request.SetParameter("Artist", query.Artist)
request.SetParameter("AudienceRating", strings.Join(query.AudienceRatings, ","))
request.SetParameter("Author", query.Author)
request.SetParameter("Availability", query.Availability)
request.SetParameter("Brand", query.Brand)
request.SetParameter("BrowseNode", query.BrowseNode)
request.SetParameter("Composer", query.Composer)
request.SetParameter("Condition", query.Condition)
request.SetParameter("Conductor", query.Conductor)
request.SetParameter("Director", query.Director)
request.SetParameter("IncludeReviewsSummary", query.IncludeReviewsSummary)
request.SetParameter("ItemPage", query.ItemPage)
request.SetParameter("Keywords", query.Keywords)
request.SetParameter("Manufacturer", query.Manufacturer)
request.SetParameter("MaximumPrice", query.MaximumPrice)
request.SetParameter("MerchantId", query.MerchantID)
request.SetParameter("MinimumPrice", query.MinimumPrice)
request.SetParameter("MinPercentageOff", query.MinPercentageOff)
request.SetParameter("Orchestra", query.Orchestra)
request.SetParameter("Power", query.Power)
request.SetParameter("Publisher", query.Publisher)
request.SetParameter("RelatedItemPage", query.RelatedItemPage)
request.SetParameter("RelationshipType", query.RelationshipType)
request.SetParameter("SearchIndex", query.SearchIndex)
request.SetParameter("Sort", query.Sort)
request.SetParameter("Title", query.Title)
request.SetParameter("TruncateReviewsAt", query.TruncateReviewsAt)
request.SetParameter("VariationPage", query.VariationPage)
request.SetParameter("ResponseGroup", strings.Join(query.ResponseGroups, ","))
xmlData, err := client.ProcessRequest(request)
if err != nil {
return nil, err
}
var response ItemSearchResponse
xml.Unmarshal(xmlData, &response)
if response.Items.Request.IsValid != true {
return &response, errors.New("amazonpa: request is invalid")
}
return &response, nil
}
// ItemLookup performs an ItemLookup request
func (client Client) BrowseNodeLookup(query BrowseNodeLookupQuery) (*BrowseNodeLookupResponse, error) {
request := client.NewRequest("BrowseNodeLookup")
request.SetParameter("BrowseNodeId", query.BrowseNodeID)
request.SetParameter("ResponseGroup", strings.Join(query.ResponseGroups, ","))
xmlData, err := client.ProcessRequest(request)
if err != nil {
return nil, err
}
var response BrowseNodeLookupResponse
xml.Unmarshal(xmlData, &response)
if response.BrowseNodes.Request.IsValid != true {
return &response, errors.New("amazonpa: request is invalid")
}
return &response, nil
}