This repository was archived by the owner on Jan 6, 2025. It is now read-only.
forked from DDRBoxman/go-amazon-product-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamazonproduct.go
192 lines (160 loc) · 4.98 KB
/
amazonproduct.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
//Package amazonproduct provides methods for interacting with the Amazon Product Advertising API
package amazonproduct
import (
"errors"
"fmt"
"strconv"
"strings"
)
/*
ItemLookup takes a product ID (ASIN) and returns the result
*/
func (api AmazonProductAPI) ItemLookup(ItemId string) (string, error) {
params := map[string]string{
"ItemId": ItemId,
"ResponseGroup": "Images,ItemAttributes,Small,EditorialReview",
}
return api.genSignAndFetch("ItemLookup", params)
}
/*
ItemLookupWithResponseGroup takes a product ID (ASIN) and a ResponseGroup and returns the result
*/
func (api AmazonProductAPI) ItemLookupWithResponseGroup(ItemId string, ResponseGroup string) (string, error) {
params := map[string]string{
"ItemId": ItemId,
"ResponseGroup": ResponseGroup,
}
return api.genSignAndFetch("ItemLookup", params)
}
/*
ItemLookupWithParams takes the params for ItemLookup and returns the result
*/
func (api AmazonProductAPI) ItemLookupWithParams(params map[string]string) (string, error) {
_, present := params["ItemId"]
if !present {
return "", errors.New("ItemId property is required in the params map")
}
return api.genSignAndFetch("ItemLookup", params)
}
/*
MultipleItemLookup takes an array of product IDs (ASIN) and returns the result
*/
func (api AmazonProductAPI) MultipleItemLookup(ItemIds []string) (string, error) {
params := map[string]string{
"ItemId": strings.Join(ItemIds, ","),
"ResponseGroup": "Images,ItemAttributes,Small,EditorialReview",
}
return api.genSignAndFetch("ItemLookup", params)
}
/*
MultipleItemLookupWithResponseGroup takes an array of product IDs (ASIN) as well as a ResponseGroup and returns the result
*/
func (api AmazonProductAPI) MultipleItemLookupWithResponseGroup(ItemIds []string, ResponseGroup string) (string, error) {
params := map[string]string{
"ItemId": strings.Join(ItemIds, ","),
"ResponseGroup": ResponseGroup,
}
return api.genSignAndFetch("ItemLookup", params)
}
/*
ItemSearchByKeyword takes a string containing keywords and returns the search results
*/
func (api AmazonProductAPI) ItemSearchByKeyword(Keywords string, page int) (string, error) {
params := map[string]string{
"Keywords": Keywords,
"ResponseGroup": "Images,ItemAttributes,Small,EditorialReview",
"ItemPage": strconv.FormatInt(int64(page), 10),
}
return api.ItemSearch("All", params)
}
func (api AmazonProductAPI) ItemSearchByKeywordWithResponseGroup(Keywords string, ResponseGroup string) (string, error) {
params := map[string]string{
"Keywords": Keywords,
"ResponseGroup": ResponseGroup,
}
return api.ItemSearch("All", params)
}
func (api AmazonProductAPI) ItemSearch(SearchIndex string, Parameters map[string]string) (string, error) {
Parameters["SearchIndex"] = SearchIndex
return api.genSignAndFetch("ItemSearch", Parameters)
}
/*
CartCreate takes a map containing ASINs and quantities. Up to 10 items are allowed
*/
func (api AmazonProductAPI) CartCreate(items map[string]int) (string, error) {
params := make(map[string]string)
i := 1
for k, v := range items {
if i < 11 {
key := fmt.Sprintf("Item.%d.ASIN", i)
params[key] = string(k)
key = fmt.Sprintf("Item.%d.Quantity", i)
params[key] = strconv.Itoa(v)
i++
} else {
break
}
}
return api.genSignAndFetch("CartCreate", params)
}
/*
CartAdd takes a map containing ASINs and quantities and adds them to the given cart.
Up to 10 items are allowed
*/
func (api AmazonProductAPI) CartAdd(items map[string]int, cartid, HMAC string) (string, error) {
params := map[string]string{
"CartId": cartid,
"HMAC": HMAC,
}
i := 1
for k, v := range items {
if i < 11 {
key := fmt.Sprintf("Item.%d.ASIN", i)
params[key] = string(k)
key = fmt.Sprintf("Item.%d.Quantity", i)
params[key] = strconv.Itoa(v)
i++
} else {
break
}
}
return api.genSignAndFetch("CartAdd", params)
}
/*
CartClear takes a CartId and HMAC that were returned when generating a cart
It then removes the contents of the cart
*/
func (api AmazonProductAPI) CartClear(CartId, HMAC string) (string, error) {
params := map[string]string{
"CartId": CartId,
"HMAC": HMAC,
}
return api.genSignAndFetch("CartClear", params)
}
/*
Cart get takes a CartID and HMAC that were returned when generating a cart
Returns the contents of the specified cart
*/
func (api AmazonProductAPI) CartGet(CartId, HMAC string) (string, error) {
params := map[string]string{
"CartId": CartId,
"HMAC": HMAC,
}
return api.genSignAndFetch("CartGet", params)
}
/*
BrowseNodeLookup takes a BrowseNodeId and returns the result.
*/
func (api AmazonProductAPI) BrowseNodeLookup(nodeId string) (string, error) {
params := map[string]string{
"BrowseNodeId": nodeId,
}
return api.genSignAndFetch("BrowseNodeLookup", params)
}
func (api AmazonProductAPI) BrowseNodeLookupWithResponseGroup(nodeId string, responseGroup string) (string, error) {
params := map[string]string{
"BrowseNodeId": nodeId,
"ResponseGroup": responseGroup,
}
return api.genSignAndFetch("BrowseNodeLookup", params)
}