1 | <?php |
||
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) |
|
58 | |||
59 | /** |
||
60 | * @param string $uri |
||
61 | * @param array $params |
||
62 | * |
||
63 | * @return ResponseInterface |
||
64 | */ |
||
65 | 1 | public function post($uri, array $params = []) |
|
69 | |||
70 | /** |
||
71 | * @param string $uri |
||
72 | * @param array $params |
||
73 | * |
||
74 | * @return ResponseInterface |
||
75 | */ |
||
76 | 3 | public function get($uri, array $params = []) |
|
80 | |||
81 | /** |
||
82 | * @param string $uri |
||
83 | * @param array $params |
||
84 | * |
||
85 | * @return ResponseInterface |
||
86 | */ |
||
87 | 1 | public function put($uri, array $params = []) |
|
91 | |||
92 | /** |
||
93 | * @param string $uri |
||
94 | * |
||
95 | * @return ResponseInterface |
||
96 | */ |
||
97 | 1 | public function delete($uri) |
|
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 = []) |
|
121 | |||
122 | /** |
||
123 | * Get the last response from the API |
||
124 | * |
||
125 | * @return null|ResponseInterface |
||
126 | */ |
||
127 | 1 | public function getLastResponse() |
|
131 | |||
132 | 1 | public function getHttpClient() |
|
136 | |||
137 | /** |
||
138 | * Get the defaults headers like the Authorization field |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 14 | private function getDefaultHeaders() |
|
150 | |||
151 | /** |
||
152 | * @param ResponseInterface $response |
||
153 | * |
||
154 | * @throws BadResponseException |
||
155 | */ |
||
156 | 14 | private function validateResponse(ResponseInterface $response) |
|
164 | |||
165 | /** |
||
166 | * @return HttpClient |
||
167 | */ |
||
168 | 1 | private function defaultHttpClient() |
|
172 | |||
173 | /** |
||
174 | * @param $method |
||
175 | * |
||
176 | * @throw \InvalidArgumentException |
||
177 | */ |
||
178 | 15 | private function validateMethod($method) |
|
184 | } |
||
185 |