-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.go
120 lines (100 loc) · 2.8 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
package gopaapi5
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/utekaravinash/gopaapi5/api"
)
var (
// ErrEmptyAccessKey is thrown when access key is empty
ErrEmptyAccessKey = errors.New("Empty access key")
// ErrEmptySecretKey is thrown when secret key is empty
ErrEmptySecretKey = errors.New("Empty secret key")
// ErrEmptyAssociateTag is thrown when associate tag is empty
ErrEmptyAssociateTag = errors.New("Empty associate tag")
// ErrInvalidLocale is thrown when locale is invalid
ErrInvalidLocale = errors.New("Invalid locale")
// ErrNilHttpClient is thrown when locale is invalid
ErrNilHttpClient = errors.New("Nil http client")
)
// Client stores AccessKey, SecretKey, and, AssociateTag; and exposes GetBrowseNodes, GetItems, GetVariations, and SearchItems operations.
type Client struct {
AccessKey string
SecretKey string
AssociateTag string
Locale api.Locale
partnerType string
service string
host string
region string
marketplace string
httpClient *http.Client
testing bool
}
// NewClient accepts Access Key, Secrete Key, Associate Tag, Locale and returns a new client
func NewClient(accessKey, secretKey, associateTag string, locale api.Locale) (*Client, error) {
if accessKey == "" {
return nil, ErrEmptyAccessKey
}
if secretKey == "" {
return nil, ErrEmptySecretKey
}
if associateTag == "" {
return nil, ErrEmptyAssociateTag
}
if !locale.IsValid() {
return nil, ErrInvalidLocale
}
client := &Client{
AccessKey: accessKey,
SecretKey: secretKey,
AssociateTag: associateTag,
partnerType: "Associates",
service: "ProductAdvertisingAPIv1",
host: locale.Host(),
region: locale.Region(),
marketplace: locale.Marketplace(),
httpClient: &http.Client{},
testing: false,
}
return client, nil
}
// SetHttpClient allows you to change the default http client used by gopaapi5 client
func (c *Client) SetHttpClient(httpClient *http.Client) error {
if httpClient == nil {
return ErrNilHttpClient
}
c.httpClient = httpClient
return nil
}
// send sends a http request to Amazon Product Advertising service and returns response or error
func (c *Client) send(req *request, v interface{}) error {
// Construct http request
err := req.build()
if err != nil {
return err
}
// Sign http request by adding Authorization header
err = req.sign()
if err != nil {
return err
}
// Send http request and receive response or return error if any
resp, err := c.httpClient.Do(req.httpReq)
if err != nil {
return err
}
defer resp.Body.Close()
// Read response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// Unmarshal response body to operation specific response struct
err = json.Unmarshal(body, v)
if err != nil {
return err
}
return nil
}