|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tgallice\Wit; |
|
4
|
|
|
|
|
5
|
|
|
use Tgallice\Wit\HttpClient\GuzzleHttpClient; |
|
6
|
|
|
use Tgallice\Wit\HttpClient\HttpClient; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Tgallice\Wit\Exception\BadResponseException; |
|
9
|
|
|
|
|
10
|
|
|
class Client |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* API base uri |
|
14
|
|
|
*/ |
|
15
|
|
|
const API_BASE_URI = 'https://api.wit.ai/'; |
|
16
|
|
|
|
|
17
|
|
|
/* |
|
18
|
|
|
* API Version |
|
19
|
|
|
*/ |
|
20
|
|
|
const DEFAULT_API_VERSION = '20160330'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Request default timeout |
|
24
|
|
|
*/ |
|
25
|
|
|
const DEFAULT_TIMEOUT = 5; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
public static $allowedMethod = ['POST', 'GET', 'PUT', 'DELETE']; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string Wit app token |
|
34
|
|
|
*/ |
|
35
|
|
|
private $accessToken; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $apiVersion; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var HttpClient client |
|
44
|
|
|
*/ |
|
45
|
|
|
private $client; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ResponseInterface|null |
|
49
|
|
|
*/ |
|
50
|
|
|
private $lastResponse; |
|
51
|
|
|
|
|
52
|
17 |
|
public function __construct($accessToken, HttpClient $httpClient = null, $apiVersion = self::DEFAULT_API_VERSION) |
|
53
|
|
|
{ |
|
54
|
17 |
|
$this->accessToken = $accessToken; |
|
55
|
17 |
|
$this->apiVersion = $apiVersion; |
|
56
|
17 |
|
$this->client = $httpClient ?: $this->defaultHttpClient(); |
|
57
|
17 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $uri |
|
61
|
|
|
* @param array $params |
|
62
|
|
|
* |
|
63
|
|
|
* @return ResponseInterface |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function post($uri, array $params = []) |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->send('POST', $uri, $params); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $uri |
|
72
|
|
|
* @param array $params |
|
73
|
|
|
* |
|
74
|
|
|
* @return ResponseInterface |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function get($uri, array $params = []) |
|
77
|
|
|
{ |
|
78
|
3 |
|
return $this->send('GET', $uri, null, $params); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $uri |
|
83
|
|
|
* @param array $params |
|
84
|
|
|
* |
|
85
|
|
|
* @return ResponseInterface |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function put($uri, array $params = []) |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->send('PUT', $uri, $params); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $uri |
|
94
|
|
|
* |
|
95
|
|
|
* @return ResponseInterface |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function delete($uri) |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->send('DELETE', $uri); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $method |
|
104
|
|
|
* @param string $uri |
|
105
|
|
|
* @param array $queryParams |
|
|
|
|
|
|
106
|
|
|
* @param mixed $postParams |
|
|
|
|
|
|
107
|
|
|
* |
|
108
|
|
|
* @return ResponseInterface |
|
109
|
|
|
*/ |
|
110
|
15 |
|
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []) |
|
111
|
|
|
{ |
|
112
|
15 |
|
$this->validateMethod($method); |
|
113
|
14 |
|
$headers = array_merge($this->getDefaultHeaders(), $headers); |
|
114
|
14 |
|
$this->lastResponse = $this->client->send($method, $uri, $body, $query, $headers, $options); |
|
115
|
14 |
|
$this->validateResponse($this->lastResponse); |
|
116
|
|
|
|
|
117
|
13 |
|
return $this->lastResponse; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the last response from the API |
|
122
|
|
|
* |
|
123
|
|
|
* @return null|ResponseInterface |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function getLastResponse() |
|
126
|
|
|
{ |
|
127
|
1 |
|
return $this->lastResponse; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
public function getHttpClient() |
|
131
|
|
|
{ |
|
132
|
1 |
|
return $this->client; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get the defaults headers like the Authorization field |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
14 |
|
private function getDefaultHeaders() |
|
141
|
|
|
{ |
|
142
|
|
|
return [ |
|
143
|
14 |
|
'Authorization' => 'Bearer '.$this->accessToken, |
|
144
|
|
|
// Used the accept field is needed to fix the API version and avoid BC break from the API |
|
145
|
14 |
|
'Accept' => 'application/vnd.wit.'.$this->apiVersion.'+json', |
|
146
|
14 |
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param ResponseInterface $response |
|
151
|
|
|
* |
|
152
|
|
|
* @throws BadResponseException |
|
153
|
|
|
*/ |
|
154
|
14 |
|
private function validateResponse(ResponseInterface $response) |
|
155
|
|
|
{ |
|
156
|
14 |
|
if ($response->getStatusCode() !== 200) { |
|
157
|
1 |
|
$message = empty($response->getReasonPhrase()) ? 'Bad response status code' : $response->getReasonPhrase(); |
|
158
|
|
|
|
|
159
|
1 |
|
throw new BadResponseException($message, $response); |
|
160
|
|
|
} |
|
161
|
13 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return HttpClient |
|
165
|
|
|
*/ |
|
166
|
1 |
|
private function defaultHttpClient() |
|
167
|
|
|
{ |
|
168
|
1 |
|
return new GuzzleHttpClient(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param $method |
|
173
|
|
|
* |
|
174
|
|
|
* @throw \InvalidArgumentException |
|
175
|
|
|
*/ |
|
176
|
15 |
|
private function validateMethod($method) |
|
177
|
|
|
{ |
|
178
|
15 |
|
if (!in_array(strtoupper($method), self::$allowedMethod)) { |
|
179
|
1 |
|
throw new \InvalidArgumentException(sprintf('"%s" is not in the allowed methods "%s"', $method, implode(', ', self::$allowedMethod))); |
|
180
|
|
|
} |
|
181
|
14 |
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.