1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IBM\Watson\Common\Api; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use IBM\Watson\Common\RequestBuilderInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* AbstractApi is used to create and send HTTP requests. |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractApi implements ApiInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Http\Client\HttpClient |
15
|
|
|
*/ |
16
|
|
|
private $httpClient; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var RequestBuilderInterface |
20
|
|
|
*/ |
21
|
|
|
private $requestBuilder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param \Http\Client\HttpClient $httpClient HTTP client to send requests. |
25
|
|
|
* @param \IBM\Watson\Common\RequestBuilderInterface $requestBuilder Request builder to create requests. |
26
|
|
|
*/ |
27
|
|
|
public function __construct( |
28
|
|
|
HttpClient $httpClient, |
29
|
|
|
RequestBuilderInterface $requestBuilder |
30
|
|
|
) { |
31
|
|
|
$this->httpClient = $httpClient; |
32
|
|
|
$this->requestBuilder = $requestBuilder; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create and send GET request. |
37
|
|
|
* |
38
|
|
|
* @param string|UriInterface $uri API URI. |
39
|
|
|
* @param array $params Query parameters. |
40
|
|
|
* @param array $headers Query headers. |
41
|
|
|
* |
42
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
43
|
|
|
* |
44
|
|
|
* @throws \Http\Client\Exception |
45
|
|
|
*/ |
46
|
|
|
protected function get($uri, array $params = [], array $headers = []) |
47
|
|
|
{ |
48
|
|
|
return $this->httpClient->sendRequest( |
49
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_GET, $uri, $params, $headers) |
|
|
|
|
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create and send HEAD request. |
55
|
|
|
* |
56
|
|
|
* @param string|UriInterface $uri API URI. |
57
|
|
|
* @param array $headers Query headers. |
58
|
|
|
* |
59
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
60
|
|
|
* @throws \Http\Client\Exception |
61
|
|
|
*/ |
62
|
|
|
protected function head($uri, array $headers = []) |
63
|
|
|
{ |
64
|
|
|
return $this->httpClient->sendRequest( |
65
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_HEAD, $uri, $headers, null) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create and send TRACE request. |
71
|
|
|
* |
72
|
|
|
* @param string|UriInterface $uri API URI. |
73
|
|
|
* @param array $headers Query headers. |
74
|
|
|
* |
75
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
76
|
|
|
* @throws \Http\Client\Exception |
77
|
|
|
*/ |
78
|
|
|
protected function trace($uri, array $headers = []) |
79
|
|
|
{ |
80
|
|
|
return $this->httpClient->sendRequest( |
81
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_TRACE, $uri, $headers, null) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create and send POST request. |
87
|
|
|
* |
88
|
|
|
* @param string|UriInterface $uri API URI. |
89
|
|
|
* @param array $params Query parameters. |
90
|
|
|
* @param array $headers Query headers. |
91
|
|
|
* |
92
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
93
|
|
|
* |
94
|
|
|
* @throws \Http\Client\Exception |
95
|
|
|
*/ |
96
|
|
|
protected function post($uri, array $params = [], array $headers = []) |
97
|
|
|
{ |
98
|
|
|
return $this->postRaw($uri, \http_build_query($params), $headers); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create and send POST request. |
103
|
|
|
* |
104
|
|
|
* @param string|UriInterface $uri API URI. |
105
|
|
|
* @param resource|string|StreamInterface|null $body Request body. |
106
|
|
|
* @param array $headers Query headers. |
107
|
|
|
* |
108
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
109
|
|
|
* |
110
|
|
|
* @throws \Http\Client\Exception |
111
|
|
|
*/ |
112
|
|
|
protected function postRaw($uri, $body, array $headers = []) |
113
|
|
|
{ |
114
|
|
|
return $this->httpClient->sendRequest( |
115
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_POST, $uri, $headers, $body) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Create and send PUT request. |
121
|
|
|
* |
122
|
|
|
* @param string|UriInterface $uri API URI. |
123
|
|
|
* @param array $params Query parameters. |
124
|
|
|
* @param array $headers Query headers. |
125
|
|
|
* |
126
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
127
|
|
|
* |
128
|
|
|
* @throws \Http\Client\Exception |
129
|
|
|
*/ |
130
|
|
|
protected function put($uri, array $params = [], array $headers = []) |
131
|
|
|
{ |
132
|
|
|
return $this->httpClient->sendRequest( |
133
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_PUT, $uri, $params, $headers) |
|
|
|
|
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create and send PATCH request. |
139
|
|
|
* |
140
|
|
|
* @param string|UriInterface $uri API URI. |
141
|
|
|
* @param resource|string|StreamInterface|null $body Request body. |
142
|
|
|
* @param array $headers Query headers. |
143
|
|
|
* |
144
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
145
|
|
|
* @throws \Http\Client\Exception |
146
|
|
|
*/ |
147
|
|
|
protected function patch($uri, $body, array $headers = []) |
148
|
|
|
{ |
149
|
|
|
return $this->httpClient->sendRequest( |
150
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_PATCH, $uri, $headers, $body) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create and send DELETE request. |
156
|
|
|
* |
157
|
|
|
* @param string|UriInterface $uri API URI. |
158
|
|
|
* @param array $params Query parameters. |
159
|
|
|
* @param array $headers Query headers. |
160
|
|
|
* |
161
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
162
|
|
|
* @throws \Http\Client\Exception |
163
|
|
|
*/ |
164
|
|
|
protected function delete($uri, array $params = [], array $headers = []) |
165
|
|
|
{ |
166
|
|
|
return $this->httpClient->sendRequest( |
167
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_DELETE, $uri, $params, $headers) |
|
|
|
|
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Create and send OPTIONS request. |
173
|
|
|
* |
174
|
|
|
* @param string|UriInterface $uri API URI. |
175
|
|
|
* @param array $params Query parameters. |
176
|
|
|
* @param array $headers Query headers. |
177
|
|
|
* |
178
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
179
|
|
|
* @throws \Http\Client\Exception |
180
|
|
|
*/ |
181
|
|
|
protected function options($uri, array $params = [], array $headers = []) |
182
|
|
|
{ |
183
|
|
|
return $this->httpClient->sendRequest( |
184
|
|
|
$this->requestBuilder->create(static::HTTP_METHOD_OPTIONS, $uri, $params, $headers) |
|
|
|
|
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: