-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
204 lines (180 loc) · 6.04 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
package paapi5
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"strings"
"time"
"github.com/goark/errs"
"github.com/goark/fetch"
)
const (
defaultPartnerType = "Associates"
)
// 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 http.Client for Aozora API Server
type client struct {
server *Server
client fetch.Client
partnerTag string
accessKey string
secretKey string
}
// Marketplace returns name of Marketplace parameter for PA-API v5
func (c *client) Marketplace() string {
return c.server.Marketplace()
}
// PartnerTag returns PartnerTag parameter for PA-API v5
func (c *client) PartnerTag() string {
return c.partnerTag
}
// PartnerType returns PartnerType parameter for PA-API v5
func (c *client) PartnerType() string {
return defaultPartnerType
}
// Request method returns response data (JSON format) by PA-APIv5.
func (c *client) Request(q Query) ([]byte, error) {
return c.RequestContext(context.Background(), q)
}
// RequestContext method returns response data (JSON format) by PA-APIv5. (with context.Context)
func (c *client) RequestContext(ctx context.Context, q Query) ([]byte, error) {
payload, err := q.Payload()
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("Operation", q.Operation().String()))
}
b, err := c.post(ctx, q.Operation(), payload)
if err != nil {
return nil, errs.Wrap(err, errs.WithContext("Operation", q.Operation().String()), errs.WithContext("payload", string(payload)))
}
return b, nil
}
func (c *client) post(ctx context.Context, cmd Operation, payload []byte) ([]byte, error) {
dt := NewTimeStamp(time.Now())
u := c.server.URL(cmd.Path())
hds := newHeaders(c.server, cmd, dt)
sig := c.signiture(c.signedString(hds, payload), hds)
resp, err := c.client.PostWithContext(
ctx,
u,
bytes.NewReader(payload),
fetch.WithRequestHeaderSet("Accept", c.server.Accept()),
fetch.WithRequestHeaderSet("Accept-Language", c.server.AcceptLanguage()),
fetch.WithRequestHeaderSet("Content-Type", c.server.ContentType()),
fetch.WithRequestHeaderSet("Content-Encoding", hds.get("Content-Encoding")),
fetch.WithRequestHeaderSet("Host", hds.get("Host")),
fetch.WithRequestHeaderSet("X-Amz-Date", hds.get("X-Amz-Date")),
fetch.WithRequestHeaderSet("X-Amz-Target", hds.get("X-Amz-Target")),
fetch.WithRequestHeaderSet("Authorization", c.authorization(sig, hds)),
)
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
}
func (c *client) authorization(sig string, hds *headers) string {
buf := bytes.Buffer{}
buf.WriteString(c.server.HMACAlgorithm())
buf.WriteString(" Credential=")
buf.WriteString(strings.Join([]string{c.accessKey, hds.dt.StringDate(), c.server.Region(), c.server.ServiceName(), c.server.AWS4Request()}, "/"))
buf.WriteString(",SignedHeaders=")
buf.WriteString(hds.list())
buf.WriteString(",Signature=")
buf.WriteString(sig)
return buf.String()
}
func (c *client) signiture(signed string, hds *headers) string {
dateKey := hmacSHA256([]byte("AWS4"+c.secretKey), []byte(hds.dt.StringDate()))
regionKey := hmacSHA256(dateKey, []byte(c.server.Region()))
serviceKey := hmacSHA256(regionKey, []byte(c.server.ServiceName()))
requestKey := hmacSHA256(serviceKey, []byte(c.server.AWS4Request()))
return hex.EncodeToString(hmacSHA256(requestKey, []byte(signed)))
}
func (c *client) signedString(hds *headers, payload []byte) string {
return strings.Join(
[]string{
c.server.HMACAlgorithm(),
hds.dt.String(),
strings.Join([]string{hds.dt.StringDate(), c.server.Region(), c.server.ServiceName(), c.server.AWS4Request()}, "/"),
hashedString([]byte(c.canonicalRequest(hds, payload))),
},
"\n",
)
}
func (c *client) canonicalRequest(hds *headers, payload []byte) string {
request := []string{"POST", hds.cmd.Path(), "", hds.values(), "", hds.list(), hashedString(payload)}
return strings.Join(request, "\n")
}
func hmacSHA256(key, data []byte) []byte {
hasher := hmac.New(sha256.New, key)
_, err := hasher.Write(data)
if err != nil {
return []byte{}
}
return hasher.Sum(nil)
}
func hashedString(data []byte) string {
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:])
}
type headers struct {
cmd Operation
dt TimeStamp
headers []string
valueList map[string]string
}
func newHeaders(svr *Server, cmd Operation, dt TimeStamp) *headers {
hds := &headers{cmd: cmd, dt: dt, headers: []string{"content-encoding", "host", "x-amz-date", "x-amz-target"}, valueList: map[string]string{}}
hds.valueList["content-encoding"] = svr.ContentEncoding()
hds.valueList["host"] = svr.HostName()
hds.valueList["x-amz-date"] = dt.String()
hds.valueList["x-amz-target"] = cmd.Target()
return hds
}
func (h *headers) get(name string) string {
if s, ok := h.valueList[strings.ToLower(name)]; ok {
return s
}
return ""
}
func (h *headers) list() string {
return strings.Join(h.headers, ";")
}
func (h *headers) values() string {
list := []string{}
for _, name := range h.headers {
list = append(list, name+":"+h.get(name))
}
return strings.Join(list, "\n")
}
/* 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.
*/