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 = '20160526'; |
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 mixed $body |
106
|
|
|
* @param array $query |
107
|
|
|
* @param array $headers |
108
|
|
|
* @param array $options |
109
|
|
|
* |
110
|
|
|
* @return ResponseInterface |
111
|
|
|
*/ |
112
|
15 |
|
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []) |
113
|
|
|
{ |
114
|
15 |
|
$this->validateMethod($method); |
115
|
14 |
|
$headers = array_merge($this->getDefaultHeaders(), $headers); |
116
|
14 |
|
$this->lastResponse = $this->client->send($method, $uri, $body, $query, $headers, $options); |
117
|
14 |
|
$this->validateResponse($this->lastResponse); |
118
|
|
|
|
119
|
13 |
|
return $this->lastResponse; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the last response from the API |
124
|
|
|
* |
125
|
|
|
* @return null|ResponseInterface |
126
|
|
|
*/ |
127
|
1 |
|
public function getLastResponse() |
128
|
|
|
{ |
129
|
1 |
|
return $this->lastResponse; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
public function getHttpClient() |
133
|
|
|
{ |
134
|
1 |
|
return $this->client; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the defaults headers like the Authorization field |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
14 |
|
private function getDefaultHeaders() |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
14 |
|
'Authorization' => 'Bearer '.$this->accessToken, |
146
|
|
|
// Used the accept field is needed to fix the API version and avoid BC break from the API |
147
|
14 |
|
'Accept' => 'application/vnd.wit.'.$this->apiVersion.'+json', |
148
|
14 |
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param ResponseInterface $response |
153
|
|
|
* |
154
|
|
|
* @throws BadResponseException |
155
|
|
|
*/ |
156
|
14 |
|
private function validateResponse(ResponseInterface $response) |
157
|
|
|
{ |
158
|
14 |
|
if ($response->getStatusCode() !== 200) { |
159
|
1 |
|
$message = empty($response->getReasonPhrase()) ? 'Bad response status code' : $response->getReasonPhrase(); |
160
|
|
|
|
161
|
1 |
|
throw new BadResponseException($message, $response); |
162
|
|
|
} |
163
|
13 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return HttpClient |
167
|
|
|
*/ |
168
|
1 |
|
private function defaultHttpClient() |
169
|
|
|
{ |
170
|
1 |
|
return new GuzzleHttpClient(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param $method |
175
|
|
|
* |
176
|
|
|
* @throw \InvalidArgumentException |
177
|
|
|
*/ |
178
|
15 |
|
private function validateMethod($method) |
179
|
|
|
{ |
180
|
15 |
|
if (!in_array(strtoupper($method), self::$allowedMethod)) { |
181
|
1 |
|
throw new \InvalidArgumentException(sprintf('"%s" is not in the allowed methods "%s"', $method, implode(', ', self::$allowedMethod))); |
182
|
|
|
} |
183
|
14 |
|
} |
184
|
|
|
} |
185
|
|
|
|