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
|
|
|
|
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 API_VERSION = '20160330'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Request default timeout |
24
|
|
|
*/ |
25
|
|
|
const DEFAULT_TIMEOUT = 5; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var HTTP client |
29
|
|
|
*/ |
30
|
|
|
private $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LoggerInterface |
34
|
|
|
*/ |
35
|
|
|
private $logger; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ResponseInterface|null |
39
|
|
|
*/ |
40
|
|
|
private $lastResponse; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string Wit app token |
44
|
|
|
*/ |
45
|
|
|
private $accessToken; |
46
|
|
|
|
47
|
6 |
|
public function __construct($accessToken, $httpClient = null, LoggerInterface $logger = null) |
48
|
|
|
{ |
49
|
6 |
|
$this->accessToken = $accessToken; |
50
|
6 |
|
$this->client = $httpClient ?: new HttpClient([ |
51
|
1 |
|
'base_uri' => self::API_BASE_URI, |
52
|
1 |
|
'timeout' => self::DEFAULT_TIMEOUT, |
53
|
1 |
|
'connect_timeout' => self::DEFAULT_TIMEOUT, |
54
|
1 |
|
]); |
55
|
6 |
|
$this->logger = $logger; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $method |
60
|
|
|
* @param string $uri |
61
|
|
|
* @param array $query |
62
|
|
|
* @param mixed $content |
63
|
|
|
* |
64
|
|
|
* @return ResponseInterface |
65
|
|
|
*/ |
66
|
4 |
|
public function send($method, $uri, array $query = [], $content = null) |
67
|
|
|
{ |
68
|
|
|
$options = [ |
69
|
4 |
|
RequestOptions::QUERY => $query, |
70
|
4 |
|
RequestOptions::HEADERS => $this->getDefaultHeaders() |
71
|
4 |
|
]; |
72
|
|
|
|
73
|
4 |
|
if (!empty($content)) { |
74
|
1 |
|
$type = is_array($content) || $content instanceof \JsonSerializable ? RequestOptions::JSON : RequestOptions::BODY; |
75
|
1 |
|
$options[$type] = $content; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
4 |
|
$this->lastResponse = $this->client->request($method, $uri, $options); |
79
|
|
|
|
80
|
4 |
|
return $this->lastResponse; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the last response from the API |
85
|
|
|
* |
86
|
|
|
* @return null|ResponseInterface |
87
|
|
|
*/ |
88
|
1 |
|
public function getLastResponse() |
89
|
|
|
{ |
90
|
1 |
|
return $this->lastResponse; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function getClient() |
94
|
|
|
{ |
95
|
1 |
|
return $this->client; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the defaults headers like the Authorization field |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
4 |
|
protected function getDefaultHeaders() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
4 |
|
'Authorization' => 'Bearer '.$this->accessToken, |
107
|
|
|
// Used the accept field is needed to fix the API version and avoid BC break from the API |
108
|
4 |
|
'Accept' => 'application/vnd.wit.'.self::API_VERSION.'+json', |
109
|
4 |
|
'Content-Type' => 'application/json', |
110
|
4 |
|
]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|