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; |
19
|
|
|
|
20
|
|
|
use ApaiIO\ApaiIO; |
21
|
|
|
use ApaiIO\Configuration\ConfigurationInterface; |
22
|
|
|
use ApaiIO\Operations\OperationInterface; |
23
|
|
|
use Http\Message\MessageFactory; |
24
|
|
|
use Http\Client\HttpClient; |
25
|
|
|
use Http\Message\UriFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Basic implementation of the rest request |
29
|
|
|
* |
30
|
|
|
* @see http://docs.aws.amazon.com/AWSECommerceService/2011-08-01/DG/AnatomyOfaRESTRequest.html |
31
|
|
|
* @author Jan Eichhorn <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class HttpRequest implements RequestInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* The requestscheme |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $requestTemplate = "//webservices.amazon.%s/onca/xml?%s"; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The scheme for the uri. E.g. http or https. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $scheme = 'http'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var HttpClient |
51
|
|
|
*/ |
52
|
|
|
private $client; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var MessageFactory |
56
|
|
|
*/ |
57
|
|
|
private $messageFactory; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var UriFactory |
61
|
|
|
*/ |
62
|
|
|
private $uriFactory; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Initialize instance |
66
|
|
|
* |
67
|
|
|
* @param HttpClient $client |
68
|
|
|
* @param MessageFactory $messageFactory |
69
|
|
|
* @param UriFactory $uriFactory |
70
|
|
|
*/ |
71
|
4 |
|
public function __construct(HttpClient $client, MessageFactory $messageFactory, UriFactory $uriFactory) |
72
|
|
|
{ |
73
|
4 |
|
$this->client = $client; |
74
|
4 |
|
$this->messageFactory = $messageFactory; |
75
|
4 |
|
$this->uriFactory = $uriFactory; |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
2 |
|
public function perform(OperationInterface $operation, ConfigurationInterface $configuration) |
82
|
|
|
{ |
83
|
2 |
|
$preparedRequestParams = $this->prepareRequestParams($operation, $configuration); |
84
|
2 |
|
$queryString = $this->buildQueryString($preparedRequestParams, $configuration); |
85
|
|
|
|
86
|
2 |
|
$uri = $this->uriFactory->createUri( |
87
|
2 |
|
sprintf($this->requestTemplate, $configuration->getCountry(), $queryString) |
88
|
|
|
); |
89
|
2 |
|
$request = $this->messageFactory->createRequest('GET', $uri->withScheme($this->scheme), [ |
90
|
|
|
'User-Agent' => 'ApaiIO [' . ApaiIO::VERSION . ']' |
91
|
2 |
|
]); |
92
|
2 |
|
$result = $this->client->sendRequest($request); |
93
|
|
|
|
94
|
2 |
|
return $result->getBody()->getContents(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets the scheme. |
99
|
|
|
* |
100
|
|
|
* @param string $scheme |
101
|
|
|
*/ |
102
|
2 |
|
public function setScheme($scheme) |
103
|
|
|
{ |
104
|
2 |
|
if (!in_array($scheme = strtolower($scheme), ['http', 'https'])) { |
105
|
1 |
|
throw new \InvalidArgumentException('The scheme can only be http or https.'); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$this->scheme = $scheme; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Prepares the parameters for the request |
113
|
|
|
* |
114
|
|
|
* @param OperationInterface $operation |
115
|
|
|
* @param ConfigurationInterface $configuration |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
2 |
|
protected function prepareRequestParams(OperationInterface $operation, ConfigurationInterface $configuration) |
120
|
|
|
{ |
121
|
|
|
$baseRequestParams = [ |
122
|
2 |
|
'Service' => 'AWSECommerceService', |
123
|
2 |
|
'AWSAccessKeyId' => $configuration->getAccessKey(), |
124
|
2 |
|
'AssociateTag' => $configuration->getAssociateTag(), |
125
|
2 |
|
'Operation' => $operation->getName(), |
126
|
2 |
|
'Version' => '2013-08-01', |
127
|
2 |
|
'Timestamp' => Util::getTimeStamp() |
128
|
|
|
]; |
129
|
|
|
|
130
|
2 |
|
$operationParams = $operation->getOperationParameter(); |
131
|
|
|
|
132
|
2 |
|
foreach ($operationParams as $key => $value) { |
133
|
2 |
|
if (true === is_array($value)) { |
134
|
2 |
|
$operationParams[$key] = implode(',', $value); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
$fullParameterList = array_merge($baseRequestParams, $operationParams); |
139
|
2 |
|
ksort($fullParameterList); |
140
|
|
|
|
141
|
2 |
|
return $fullParameterList; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Builds the final querystring including the signature |
146
|
|
|
* |
147
|
|
|
* @param array $params |
148
|
|
|
* @param ConfigurationInterface $configuration |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
2 |
|
protected function buildQueryString(array $params, ConfigurationInterface $configuration) |
153
|
|
|
{ |
154
|
2 |
|
$parameterList = []; |
155
|
2 |
|
foreach ($params as $key => $value) { |
156
|
2 |
|
$parameterList[] = sprintf('%s=%s', $key, rawurlencode($value)); |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
$parameterList[] = 'Signature=' . rawurlencode( |
160
|
2 |
|
$this->buildSignature($parameterList, $configuration->getCountry(), $configuration->getSecretKey()) |
161
|
|
|
); |
162
|
|
|
|
163
|
2 |
|
return implode("&", $parameterList); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Calculates the signature for the request |
168
|
|
|
* |
169
|
|
|
* @param array $params |
170
|
|
|
* @param string $country |
171
|
|
|
* @param string $secret |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
2 |
|
protected function buildSignature(array $params, $country, $secret) |
176
|
|
|
{ |
177
|
2 |
|
return Util::buildSignature( |
178
|
|
|
sprintf( |
179
|
2 |
|
"GET\nwebservices.amazon.%s\n/onca/xml\n%s", |
180
|
|
|
$country, |
181
|
2 |
|
implode('&', $params) |
182
|
|
|
), |
183
|
|
|
$secret |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|