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