|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright 2016 Jan Eichhorn <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace ApaiIO\Request\Rest; |
|
19
|
|
|
|
|
20
|
|
|
use ApaiIO\ApaiIO; |
|
21
|
|
|
use ApaiIO\Configuration\ConfigurationInterface; |
|
22
|
|
|
use ApaiIO\Operations\OperationInterface; |
|
23
|
|
|
use ApaiIO\Request\RequestInterface; |
|
24
|
|
|
use ApaiIO\Request\Util; |
|
25
|
|
|
use GuzzleHttp\ClientInterface; |
|
26
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Basic implementation of the rest request |
|
30
|
|
|
* |
|
31
|
|
|
* @see http://docs.aws.amazon.com/AWSECommerceService/2011-08-01/DG/AnatomyOfaRESTRequest.html |
|
32
|
|
|
* @author Jan Eichhorn <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class Request implements RequestInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* The requestscheme |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $requestTemplate = "//webservices.amazon.%s/onca/xml?%s"; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The scheme for the uri. E.g. http or https. |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $scheme = 'http'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var ClientInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $client; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Initialize instance |
|
57
|
|
|
* |
|
58
|
|
|
* @param ClientInterface $client |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(ClientInterface $client) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->client = $client; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function perform(OperationInterface $operation, ConfigurationInterface $configuration) |
|
69
|
|
|
{ |
|
70
|
|
|
$preparedRequestParams = $this->prepareRequestParams($operation, $configuration); |
|
71
|
|
|
$queryString = $this->buildQueryString($preparedRequestParams, $configuration); |
|
72
|
|
|
|
|
73
|
|
|
$uri = new Uri(sprintf($this->requestTemplate, $configuration->getCountry(), $queryString)); |
|
74
|
|
|
$request = new \GuzzleHttp\Psr7\Request('GET', $uri->withScheme($this->scheme), [ |
|
75
|
|
|
'User-Agent' => 'ApaiIO [' . ApaiIO::VERSION . ']' |
|
76
|
|
|
]); |
|
77
|
|
|
$result = $this->client->send($request); |
|
78
|
|
|
|
|
79
|
|
|
return $result->getBody()->getContents(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Sets the scheme. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $scheme |
|
86
|
|
|
*/ |
|
87
|
|
|
public function setScheme($scheme) |
|
88
|
|
|
{ |
|
89
|
|
|
if (!in_array($scheme = strtolower($scheme), ['http', 'https'])) { |
|
90
|
|
|
throw new \InvalidArgumentException('The scheme can only be http or https.'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->scheme = $scheme; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Prepares the parameters for the request |
|
98
|
|
|
* |
|
99
|
|
|
* @param OperationInterface $operation |
|
100
|
|
|
* @param ConfigurationInterface $configuration |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function prepareRequestParams(OperationInterface $operation, ConfigurationInterface $configuration) |
|
105
|
|
|
{ |
|
106
|
|
|
$baseRequestParams = [ |
|
107
|
|
|
'Service' => 'AWSECommerceService', |
|
108
|
|
|
'AWSAccessKeyId' => $configuration->getAccessKey(), |
|
109
|
|
|
'AssociateTag' => $configuration->getAssociateTag(), |
|
110
|
|
|
'Operation' => $operation->getName(), |
|
111
|
|
|
'Version' => '2011-08-01', |
|
112
|
|
|
'Timestamp' => Util::getTimeStamp() |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
$operationParams = $operation->getOperationParameter(); |
|
116
|
|
|
|
|
117
|
|
|
foreach ($operationParams as $key => $value) { |
|
118
|
|
|
if (true === is_array($value)) { |
|
119
|
|
|
$operationParams[$key] = implode(',', $value); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$fullParameterList = array_merge($baseRequestParams, $operationParams); |
|
124
|
|
|
ksort($fullParameterList); |
|
125
|
|
|
|
|
126
|
|
|
return $fullParameterList; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Builds the final querystring including the signature |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $params |
|
133
|
|
|
* @param ConfigurationInterface $configuration |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function buildQueryString(array $params, ConfigurationInterface $configuration) |
|
138
|
|
|
{ |
|
139
|
|
|
$parameterList = []; |
|
140
|
|
|
foreach ($params as $key => $value) { |
|
141
|
|
|
$parameterList[] = sprintf('%s=%s', $key, rawurlencode($value)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$parameterList[] = 'Signature=' . rawurlencode( |
|
145
|
|
|
$this->buildSignature($parameterList, $configuration->getCountry(), $configuration->getSecretKey()) |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
return implode("&", $parameterList); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Calculates the signature for the request |
|
153
|
|
|
* |
|
154
|
|
|
* @param array $params |
|
155
|
|
|
* @param string $country |
|
156
|
|
|
* @param string $secret |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function buildSignature(array $params, $country, $secret) |
|
161
|
|
|
{ |
|
162
|
|
|
return Util::buildSignature( |
|
163
|
|
|
sprintf( |
|
164
|
|
|
"GET\nwebservices.amazon.%s\n/onca/xml\n%s", |
|
165
|
|
|
$country, |
|
166
|
|
|
implode('&', $params) |
|
167
|
|
|
), |
|
168
|
|
|
$secret |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|