1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\Wit; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use GuzzleHttp\RequestOptions; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Tgallice\Wit\Exception\BadResponseException; |
10
|
|
|
|
11
|
|
|
class Client |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* API base uri |
15
|
|
|
*/ |
16
|
|
|
const API_BASE_URI = 'https://api.wit.ai/'; |
17
|
|
|
|
18
|
|
|
/* |
19
|
|
|
* API Version |
20
|
|
|
*/ |
21
|
|
|
const API_VERSION = '20160330'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Request default timeout |
25
|
|
|
*/ |
26
|
|
|
const DEFAULT_TIMEOUT = 5; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var HTTP client |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
private $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ResponseInterface|null |
40
|
|
|
*/ |
41
|
|
|
private $lastResponse; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string Wit app token |
45
|
|
|
*/ |
46
|
|
|
private $accessToken; |
47
|
|
|
|
48
|
7 |
|
public function __construct($accessToken, $httpClient = null, LoggerInterface $logger = null) |
49
|
|
|
{ |
50
|
7 |
|
$this->accessToken = $accessToken; |
51
|
7 |
|
$this->client = $httpClient ?: new HttpClient([ |
52
|
1 |
|
'base_uri' => self::API_BASE_URI, |
53
|
1 |
|
'timeout' => self::DEFAULT_TIMEOUT, |
54
|
1 |
|
'connect_timeout' => self::DEFAULT_TIMEOUT, |
55
|
1 |
|
]); |
56
|
7 |
|
$this->logger = $logger; |
57
|
7 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $method |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param array $query |
63
|
|
|
* @param mixed $content |
64
|
|
|
* |
65
|
|
|
* @return ResponseInterface |
66
|
|
|
*/ |
67
|
5 |
|
public function send($method, $uri, array $query = [], $content = null) |
68
|
|
|
{ |
69
|
|
|
$options = [ |
70
|
5 |
|
RequestOptions::QUERY => $query, |
71
|
5 |
|
RequestOptions::HEADERS => $this->getDefaultHeaders() |
72
|
5 |
|
]; |
73
|
|
|
|
74
|
5 |
|
if (!empty($content)) { |
75
|
1 |
|
$type = is_array($content) || $content instanceof \JsonSerializable ? RequestOptions::JSON : RequestOptions::BODY; |
76
|
1 |
|
$options[$type] = $content; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
5 |
|
$this->lastResponse = $this->client->request($method, $uri, $options); |
80
|
|
|
|
81
|
5 |
|
$this->validateResponse($this->lastResponse); |
82
|
|
|
|
83
|
4 |
|
return $this->lastResponse; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the last response from the API |
88
|
|
|
* |
89
|
|
|
* @return null|ResponseInterface |
90
|
|
|
*/ |
91
|
1 |
|
public function getLastResponse() |
92
|
|
|
{ |
93
|
1 |
|
return $this->lastResponse; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function getClient() |
97
|
|
|
{ |
98
|
1 |
|
return $this->client; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the defaults headers like the Authorization field |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
5 |
|
protected function getDefaultHeaders() |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
5 |
|
'Authorization' => 'Bearer '.$this->accessToken, |
110
|
|
|
// Used the accept field is needed to fix the API version and avoid BC break from the API |
111
|
5 |
|
'Accept' => 'application/vnd.wit.'.self::API_VERSION.'+json', |
112
|
5 |
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param ResponseInterface $response |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
* |
120
|
|
|
* @throws BadResponseException |
121
|
|
|
*/ |
122
|
5 |
|
protected function validateResponse(ResponseInterface $response) |
123
|
|
|
{ |
124
|
5 |
|
if ($response->getStatusCode() !== 200) { |
125
|
1 |
|
$message = empty($response->getReasonPhrase()) ? 'Bad response status code' : $response->getReasonPhrase(); |
126
|
|
|
|
127
|
1 |
|
throw new BadResponseException($message, $response); |
128
|
|
|
} |
129
|
|
|
|
130
|
4 |
|
return true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|