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
|
|
|
|
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 Request implements RequestInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* The requestscheme |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $requestScheme = "http://webservices.amazon.%s/onca/xml?%s"; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ConfigurationInterface |
44
|
|
|
*/ |
45
|
|
|
private $configuration; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ClientInterface |
49
|
|
|
*/ |
50
|
|
|
private $client; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize instance |
54
|
|
|
* |
55
|
|
|
* @param ClientInterface $client |
56
|
|
|
*/ |
57
|
|
|
public function __construct(ClientInterface $client) |
58
|
|
|
{ |
59
|
|
|
$this->client = $client; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function setConfiguration(ConfigurationInterface $configuration) |
66
|
|
|
{ |
67
|
|
|
$this->configuration = $configuration; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function perform(OperationInterface $operation) |
74
|
|
|
{ |
75
|
|
|
$preparedRequestParams = $this->prepareRequestParams($operation); |
76
|
|
|
$queryString = $this->buildQueryString($preparedRequestParams); |
77
|
|
|
|
78
|
|
|
$uri = sprintf($this->requestScheme, $this->configuration->getCountry(), $queryString); |
79
|
|
|
$request = new \GuzzleHttp\Psr7\Request('GET', $uri, [ |
80
|
|
|
'User-Agent' => 'ApaiIO [' . ApaiIO::VERSION . ']' |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$result = $this->client->send($request); |
84
|
|
|
|
85
|
|
|
return $result->getBody()->getContents(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Prepares the parameters for the request |
90
|
|
|
* |
91
|
|
|
* @param OperationInterface $operation |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function prepareRequestParams(OperationInterface $operation) |
96
|
|
|
{ |
97
|
|
|
$baseRequestParams = [ |
98
|
|
|
'Service' => 'AWSECommerceService', |
99
|
|
|
'AWSAccessKeyId' => $this->configuration->getAccessKey(), |
100
|
|
|
'AssociateTag' => $this->configuration->getAssociateTag(), |
101
|
|
|
'Operation' => $operation->getName(), |
102
|
|
|
'Version' => '2011-08-01', |
103
|
|
|
'Timestamp' => Util::getTimeStamp() |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$operationParams = $operation->getOperationParameter(); |
107
|
|
|
|
108
|
|
|
foreach ($operationParams as $key => $value) { |
109
|
|
|
if (true === is_array($value)) { |
110
|
|
|
$operationParams[$key] = implode(',', $value); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$fullParameterList = array_merge($baseRequestParams, $operationParams); |
115
|
|
|
ksort($fullParameterList); |
116
|
|
|
|
117
|
|
|
return $fullParameterList; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Builds the final querystring including the signature |
122
|
|
|
* |
123
|
|
|
* @param array $params |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function buildQueryString(array $params) |
128
|
|
|
{ |
129
|
|
|
$parameterList = []; |
130
|
|
|
foreach ($params as $key => $value) { |
131
|
|
|
$parameterList[] = sprintf('%s=%s', $key, rawurlencode($value)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$parameterList[] = 'Signature=' . rawurlencode($this->buildSignature($parameterList)); |
135
|
|
|
|
136
|
|
|
return implode("&", $parameterList); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Calculates the signature for the request |
141
|
|
|
* |
142
|
|
|
* @param array $params |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
protected function buildSignature(array $params) |
147
|
|
|
{ |
148
|
|
|
$template = "GET\nwebservices.amazon.%s\n/onca/xml\n%s"; |
149
|
|
|
|
150
|
|
|
return Util::buildSignature( |
151
|
|
|
sprintf( |
152
|
|
|
$template, |
153
|
|
|
$this->configuration->getCountry(), |
154
|
|
|
implode('&', $params) |
155
|
|
|
), |
156
|
|
|
$this->configuration->getSecretKey() |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|