Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
127 lines (114 loc) · 3.8 KB
/
Copy pathclient.go
File metadata and controls
127 lines (114 loc) · 3.8 KB
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
package paapi5
import (
"bytes"
"context"
"net/http"
"github.com/goark/errs"
"github.com/goark/fetch"
)
const (
defaultPartnerType = "Associates"
// marketplaceHeader is the request header used by the Creators API to
// select the target Amazon marketplace (e.g. www.amazon.co.jp).
marketplaceHeader = "x-marketplace"
)
// Query interface for Client type
type Query interface {
Operation() Operation
Payload() ([]byte, error)
}
// Client interface
type Client interface {
Marketplace() string
PartnerTag() string
PartnerType() string
Request(Query) ([]byte, error)
RequestContext(context.Context, Query) ([]byte, error)
}
// client is the HTTP client used to call the Amazon Creators API.
type client struct {
server *Server
httpClient fetch.Client
tokenHTTPClient *http.Client
partnerTag string
credentialID string
credentialSecret string
version string
authEndpoint string
lwaFlow bool
auth *tokenManager
}
// Marketplace returns the marketplace name (e.g. www.amazon.com).
func (c *client) Marketplace() string {
return c.server.Marketplace()
}
// PartnerTag returns the configured partner (associate) tag.
func (c *client) PartnerTag() string {
return c.partnerTag
}
// PartnerType returns the partner type. The Creators API does not
// transmit this value over the wire; it is retained for backward
// compatibility with code that inspects the client.
func (c *client) PartnerType() string {
return defaultPartnerType
}
// Request issues the supplied query against the Creators API using a
// background context.
func (c *client) Request(q Query) ([]byte, error) {
return c.RequestContext(context.Background(), q)
}
// RequestContext issues the supplied query against the Creators API,
// honouring cancellation on the supplied context.
func (c *client) RequestContext(ctx context.Context, q Query) ([]byte, error) {
if q == nil {
return nil, errs.Wrap(ErrNullPointer, errs.WithContext("reason", "nil query"))
}
op := q.Operation()
payload, err := q.Payload()
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("Operation", op.String()))
}
b, err := c.post(ctx, op, payload)
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("Operation", op.String()), errs.WithContext("payload", string(payload)))
}
return b, nil
}
func (c *client) post(ctx context.Context, cmd Operation, payload []byte) ([]byte, error) {
u := c.server.URL(cmd.Path())
token, err := c.auth.Token(ctx)
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("url", u.String()))
}
resp, err := c.httpClient.PostWithContext(
ctx,
u,
bytes.NewReader(payload),
fetch.WithRequestHeaderSet("Accept", c.server.Accept()),
fetch.WithRequestHeaderSet("Content-Type", c.server.ContentType()),
fetch.WithRequestHeaderSet(marketplaceHeader, c.server.Marketplace()),
fetch.WithRequestHeaderSet("Authorization", authorizationHeader(token, c.version, c.lwaFlow)),
)
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("url", u.String()), errs.WithContext("payload", string(payload)))
}
body, err := resp.DumpBodyAndClose()
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("url", u.String()), errs.WithContext("payload", string(payload)))
}
return body, nil
}
/* Copyright 2019-2021 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/