5
5
use ApiAi \HttpClient \HttpClient ;
6
6
use ApiAi \HttpClient \GuzzleHttpClient ;
7
7
use ApiAi \Exception \BadResponseException ;
8
+ use GuzzleHttp \Promise \PromiseInterface ;
9
+ use function GuzzleHttp \Promise \rejection_for ;
8
10
use Psr \Http \Message \ResponseInterface ;
9
11
10
12
/**
@@ -145,6 +147,17 @@ public function get($uri, array $params = [])
145
147
return $ this ->send ('GET ' , $ uri , null , $ params );
146
148
}
147
149
150
+ /**
151
+ * @param string $url
152
+ * @param array $params
153
+ *
154
+ * @return PromiseInterface
155
+ */
156
+ public function getAsync ($ url , array $ params = [])
157
+ {
158
+ return $ this ->sendAsync ('GET ' , $ uri , null , $ params );
159
+ }
160
+
148
161
/**
149
162
* @param string $uri
150
163
* @param array $params
@@ -156,6 +169,17 @@ public function post($uri, array $params = [])
156
169
return $ this ->send ('POST ' , $ uri , $ params );
157
170
}
158
171
172
+ /**
173
+ * @param string $uri
174
+ * @param array $params
175
+ *
176
+ * @return PromiseInterface
177
+ */
178
+ public function postAsync ($ uri , array $ params = [])
179
+ {
180
+ return $ this ->sendAsync ('POST ' , $ uri , $ params );
181
+ }
182
+
159
183
/**
160
184
* @param string $method
161
185
* @param string $uri
@@ -180,6 +204,38 @@ public function send($method, $uri, $body = null, array $query = [], array $head
180
204
return $ this ->lastResponse ;
181
205
}
182
206
207
+ /**
208
+ * @param string $method
209
+ * @param string $uri
210
+ * @param mixed $body
211
+ * @param array $query
212
+ * @param array $headers
213
+ * @param array $options
214
+ *
215
+ * @return PromiseInterface
216
+ */
217
+ public function sendAsync ($ method , $ uri , $ body = null , array $ query = [], array $ headers = [], array $ options = [])
218
+ {
219
+ try {
220
+ $ this ->validateMethod ($ method );
221
+ } catch (\InvalidArgumentException $ e ) {
222
+ return rejection_for ($ e );
223
+ }
224
+
225
+ $ query = array_merge ($ this ->getDefaultQuery (), $ query );
226
+ $ headers = array_merge ($ this ->getDefaultHeaders (), $ headers );
227
+
228
+ return $ this ->client ->sendAsync ($ method , $ uri , $ body , $ query , $ headers , $ options )->then (
229
+ function (ResponseInterface $ response ) {
230
+ $ this ->lastResponse = $ response ;
231
+
232
+ $ this ->validateResponse ($ this ->lastResponse );
233
+
234
+ return $ this ->lastResponse ;
235
+ }
236
+ );
237
+ }
238
+
183
239
/**
184
240
* @param string $method
185
241
* @throw \InvalidArgumentException
0 commit comments