-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.go
190 lines (165 loc) · 5.1 KB
/
server.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
package paapi5
import (
"context"
"net/http"
"net/url"
"github.com/goark/fetch"
)
const (
defaultScheme = "https"
defaultAccept = "application/json, text/javascript"
defaultContentType = "application/json; charset=UTF-8"
defaultHMACAlgorithm = "AWS4-HMAC-SHA256"
defaultServiceName = "ProductAdvertisingAPI"
defaultContentEncoding = "amz-1.0"
defaultAWS4Request = "aws4_request"
)
//Server type is a implementation of PA-API service.
type Server struct {
scheme string
marketplace Marketplace
language string
}
//ServerOptFunc type is self-referential function type for New functions. (functional options pattern)
type ServerOptFunc func(*Server)
//New function returns an Server instance with options.
func New(opts ...ServerOptFunc) *Server {
server := &Server{scheme: defaultScheme, marketplace: DefaultMarketplace, language: ""}
for _, opt := range opts {
opt(server)
}
return server
}
//WithMarketplace function returns ServerOptFunc function value.
//This function is used in New functions that represents Marketplace data.
func WithMarketplace(marketplace Marketplace) ServerOptFunc {
return func(s *Server) {
if s != nil {
s.marketplace = marketplace
}
}
}
//WithLanguage function returns ServerOptFunc function value.
//This function is used in New functions that represents Accept-Language parameter.
func WithLanguage(language string) ServerOptFunc {
return func(s *Server) {
if s != nil {
s.language = language
}
}
}
//URL method returns url of service server information for PA-API v5.
func (s *Server) URL(path string) *url.URL {
if s == nil {
s = New()
}
return &url.URL{Scheme: s.scheme, Host: s.HostName(), Path: path}
}
//Marketplace method returns marketplace name for PA-API v5.
func (s *Server) Marketplace() string {
if s == nil {
s = New()
}
return s.marketplace.String()
}
//HostName method returns hostname for PA-API v5.
func (s *Server) HostName() string {
if s == nil {
s = New()
}
return s.marketplace.HostName()
}
//Region method returns region name for PA-API v5
func (s *Server) Region() string {
if s == nil {
s = New()
}
return s.marketplace.Region()
}
//Accept method returns Accept parameter for PA-API v5
func (s *Server) Accept() string {
return defaultAccept
}
//AcceptLanguage method returns Accept-Language parameter for PA-API v5
func (s *Server) AcceptLanguage() string {
if s == nil {
s = New()
}
if len(s.language) > 0 {
return s.language
}
return s.marketplace.Language() //default language
}
//ContentType method returns Content-Type parameter for PA-API v5
func (s *Server) ContentType() string {
return defaultContentType
}
//HMACAlgorithm method returns HMAC-Algorithm parameter for PA-API v5
func (s *Server) HMACAlgorithm() string {
return defaultHMACAlgorithm
}
//ServiceName method returns ServiceName parameter for PA-API v5
func (s *Server) ServiceName() string {
return defaultServiceName
}
//AWS4Request method returns AWS4Request parameter for PA-API v5
func (s *Server) AWS4Request() string {
return defaultAWS4Request
}
//ContentEncoding method returns Content-Encoding parameter for PA-API v5
func (s *Server) ContentEncoding() string {
return defaultContentEncoding
}
//ClientOptFunc type is self-referential function type for Server.CreateClient method. (functional options pattern)
type ClientOptFunc func(*client)
//CreateClient method returns an Client instance with associate-tag, access-key, secret-key, and other options.
func (s *Server) CreateClient(associateTag, accessKey, secretKey string, opts ...ClientOptFunc) Client {
if s == nil {
s = New()
}
cli := &client{
server: s,
client: nil,
partnerTag: associateTag,
accessKey: accessKey,
secretKey: secretKey,
}
for _, opt := range opts {
opt(cli)
}
if cli.client == nil {
cli.client = fetch.New()
}
return cli
}
//WithContext is dummy function. Because this function is deprecated.
func WithContext(ctx context.Context) ClientOptFunc {
return func(c *client) {}
}
//WithHttpClient function returns ClientOptFunc function value.
//This function is used in Server.CreateClient method that represents http.Client.
func WithHttpClient(hc *http.Client) ClientOptFunc {
return func(c *client) {
if c != nil {
c.client = fetch.New(fetch.WithHTTPClient(hc))
}
}
}
//DefaultClient function returns an default Client instance with associate-tag, access-key, and secret-key parameters.
func DefaultClient(associateTag, accessKey, secretKey string) Client {
return New().CreateClient(associateTag, accessKey, secretKey)
}
/* Copyright 2019-2021 Spiegel and contributors
*
* 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.
*/