1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract Request |
4
|
|
|
*/ |
5
|
|
|
namespace IBM\Watson\Common\Message; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use IBM\Watson\Common\Exception\InvalidRequestException; |
9
|
|
|
use IBM\Watson\Common\Exception\RuntimeException; |
10
|
|
|
use IBM\Watson\Common\Helper; |
11
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract Request |
16
|
|
|
* |
17
|
|
|
* This abstract class implements RequestInterface and defines a basic |
18
|
|
|
* set of functions that all IBM Watson requests are intended to include. |
19
|
|
|
* |
20
|
|
|
* Requests of this class are usually created using the createRequest |
21
|
|
|
* function of the client and then actioned using methods within this |
22
|
|
|
* class or a class that extends this. |
23
|
|
|
* |
24
|
|
|
* @see RequestInterface |
25
|
|
|
* @see AbstractResponse |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractRequest implements RequestInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The request parameters |
31
|
|
|
* |
32
|
|
|
* @var \Symfony\Component\HttpFoundation\ParameterBag |
33
|
|
|
*/ |
34
|
|
|
protected $parameters; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The request client |
38
|
|
|
* |
39
|
|
|
* @var \GuzzleHttp\ClientInterface |
40
|
|
|
*/ |
41
|
|
|
protected $httpClient; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The HTTP request object |
45
|
|
|
* |
46
|
|
|
* @var \Symfony\Component\HttpFoundation\Request |
47
|
|
|
*/ |
48
|
|
|
protected $httpRequest; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* An associated ResponseInterface |
52
|
|
|
* |
53
|
|
|
* @var ResponseInterface |
54
|
|
|
*/ |
55
|
|
|
protected $response; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a new Request |
59
|
|
|
* |
60
|
|
|
* @param ClientInterface $httpClient A Guzzle client to make API calls with |
61
|
|
|
* @param HttpRequest $httpRequest A Symfony HTTP request object |
62
|
|
|
*/ |
63
|
18 |
|
public function __construct(ClientInterface $httpClient, HttpRequest $httpRequest) |
|
|
|
|
64
|
|
|
{ |
65
|
18 |
|
$this->httpClient = $httpClient; |
66
|
18 |
|
$this->httpRequest = $httpRequest; |
67
|
18 |
|
$this->initialize(); |
68
|
18 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initialize the request with parameters |
72
|
|
|
* |
73
|
|
|
* Any unknown parameters passed will be ignored |
74
|
|
|
* |
75
|
|
|
* @param array $parameters An associative array of parameters |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
* @throws RuntimeException |
79
|
|
|
*/ |
80
|
39 |
|
public function initialize(array $parameters = []) |
81
|
|
|
{ |
82
|
39 |
|
if (null !== $this->response) { |
83
|
3 |
|
throw new RuntimeException('Request cannot be modified after it has been sent!'); |
84
|
|
|
} |
85
|
|
|
|
86
|
39 |
|
$this->parameters = new ParameterBag; |
87
|
|
|
|
88
|
39 |
|
Helper::initialize($this, $parameters); |
89
|
|
|
|
90
|
39 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get all parameters as an associative array |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
9 |
|
public function getParameters() |
99
|
|
|
{ |
100
|
9 |
|
return $this->parameters->all(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get a single parameter |
105
|
|
|
* |
106
|
|
|
* @param string $key The parameter key |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
15 |
|
protected function getParameter($key) |
110
|
|
|
{ |
111
|
15 |
|
return $this->parameters->get($key); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set a single parameter |
116
|
|
|
* |
117
|
|
|
* @param string $key The parameter key |
118
|
|
|
* @param mixed $value The value to set |
119
|
|
|
* @return AbstractRequest Provides a fluent interface |
120
|
|
|
*/ |
121
|
24 |
|
protected function setParameter($key, $value) |
122
|
|
|
{ |
123
|
24 |
|
if (null !== $this->response) { |
124
|
3 |
|
throw new RuntimeException('Request cannot be modified after it has been sent!'); |
125
|
|
|
} |
126
|
|
|
|
127
|
21 |
|
$this->parameters->set($key, $value); |
128
|
|
|
|
129
|
21 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the username parameter |
134
|
|
|
* |
135
|
|
|
* @param string $value Username to set |
136
|
|
|
* @return AbstractRequest |
137
|
|
|
*/ |
138
|
21 |
|
public function setUsername($value) |
139
|
|
|
{ |
140
|
21 |
|
return $this->setParameter('username', $value); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the username parameter |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
6 |
|
public function getUsername() |
149
|
|
|
{ |
150
|
6 |
|
return $this->getParameter('username'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set the password parameter |
155
|
|
|
* |
156
|
|
|
* @param string $value The value to set |
157
|
|
|
* @return AbstractRequest |
158
|
|
|
*/ |
159
|
12 |
|
public function setPassword($value) |
160
|
|
|
{ |
161
|
12 |
|
return $this->setParameter('password', $value); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get password parameter |
166
|
|
|
* |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
3 |
|
public function getPassword() |
170
|
|
|
{ |
171
|
3 |
|
return $this->getParameter('password'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Send the request |
176
|
|
|
* |
177
|
|
|
* @return ResponseInterface |
178
|
|
|
*/ |
179
|
12 |
|
public function send() |
180
|
|
|
{ |
181
|
12 |
|
$data = $this->getData(); |
182
|
|
|
|
183
|
12 |
|
return $this->sendData($data); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Validate the request |
188
|
|
|
* |
189
|
|
|
* This method is called internally by the client to avoid wasting time with an API call |
190
|
|
|
* when the request is clearly invalid |
191
|
|
|
* |
192
|
|
|
* @param string ... a variable length list of required parameters |
193
|
|
|
* @throws InvalidRequestException |
194
|
|
|
*/ |
195
|
6 |
|
public function validate() |
196
|
|
|
{ |
197
|
6 |
|
foreach (func_get_args() as $key) { |
198
|
6 |
|
$value = $this->getParameter($key); |
199
|
6 |
|
if (!isset($value)) { |
200
|
4 |
|
throw new InvalidRequestException("The $key parameter is required"); |
201
|
|
|
} |
202
|
4 |
|
} |
203
|
3 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the associated response |
207
|
|
|
* |
208
|
|
|
* @return ResponseInterface |
209
|
|
|
*/ |
210
|
6 |
|
public function getResponse() |
211
|
|
|
{ |
212
|
6 |
|
if (null === $this->response) { |
213
|
3 |
|
throw new RuntimeException('You must call send() before accessing the Response!'); |
214
|
|
|
} |
215
|
|
|
|
216
|
3 |
|
return $this->response; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|