Skip to content

Commit d0cbdf1

Browse files
authored
Merge pull request #8 from CthulhuDen/async
Add asynchronous API
2 parents eb2349c + 9ca9972 commit d0cbdf1

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,46 @@ try {
6969
}
7070
```
7171

72+
## Usage
73+
74+
Using the low level asynchronous api:
75+
76+
```php
77+
require_once __DIR__.'/vendor/autoload.php';
78+
79+
use ApiAi\Client;
80+
use ApiAi\Model\Query;
81+
use ApiAi\Method\QueryApi;
82+
use GuzzleHttp\HandlerStack;
83+
use React\EventLoop\Factory;
84+
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;
85+
86+
$loop = Factory::create();
87+
$reactGuzzle = new \GuzzleHttp\Client([
88+
'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT,
89+
'timeout' => Client::DEFAULT_TIMEOUT,
90+
'connect_timeout' => Client::DEFAULT_TIMEOUT,
91+
'handler' => HandlerStack::create(new HttpClientAdapter($loop))
92+
]);
93+
94+
$client = new Client('bc0a6d712bba4b3c8063a9c7ff0fa4ea', new ApiAi\HttpClient\GuzzleHttpClient($reactGuzzle));
95+
$queryApi = new QueryApi($client);
96+
97+
$queryApi->extractMeaningAsync('Hello', [
98+
'sessionId' => '123456789',
99+
'lang' => 'en'
100+
])->then(
101+
function ($meaning) {
102+
$response = new Query($meaning);
103+
},
104+
function ($error) {
105+
echo $error;
106+
}
107+
);
108+
109+
$loop->run();
110+
```
111+
72112
## Dialog
73113

74114
The `Dialog` class provides an easy way to use the `query` api and execute automatically the chaining steps :

src/Client.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use ApiAi\HttpClient\HttpClient;
66
use ApiAi\HttpClient\GuzzleHttpClient;
77
use ApiAi\Exception\BadResponseException;
8+
use GuzzleHttp\Promise\PromiseInterface;
9+
use function GuzzleHttp\Promise\rejection_for;
810
use Psr\Http\Message\ResponseInterface;
911

1012
/**
@@ -145,6 +147,17 @@ public function get($uri, array $params = [])
145147
return $this->send('GET', $uri, null, $params);
146148
}
147149

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+
148161
/**
149162
* @param string $uri
150163
* @param array $params
@@ -156,6 +169,17 @@ public function post($uri, array $params = [])
156169
return $this->send('POST', $uri, $params);
157170
}
158171

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+
159183
/**
160184
* @param string $method
161185
* @param string $uri
@@ -180,6 +204,38 @@ public function send($method, $uri, $body = null, array $query = [], array $head
180204
return $this->lastResponse;
181205
}
182206

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+
183239
/**
184240
* @param string $method
185241
* @throw \InvalidArgumentException

src/HttpClient/GuzzleHttpClient.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ public function __construct(ClientInterface $guzzleClient = null)
3737
* @inheritdoc
3838
*/
3939
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = [])
40+
{
41+
$options = $this->prepareOptions($body, $query, $headers, $options);
42+
return $this->guzzleClient->request($method, $uri, $options);
43+
}
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function sendAsync($method, $uri, $body = null, array $query = [], array $headers = [], array $options = [])
49+
{
50+
$options = $this->prepareOptions($body, $query, $headers, $options);
51+
return $this->guzzleClient->requestAsync($method, $uri, $options);
52+
}
53+
54+
/**
55+
* @param mixed $body
56+
* @param array $query
57+
* @param array $headers
58+
* @param array $options
59+
*
60+
* @return array
61+
*/
62+
private function prepareOptions($body, array $query, array $headers, array $options)
4063
{
4164
$options = array_merge($options, [
4265
RequestOptions::QUERY => $query,
@@ -49,7 +72,6 @@ public function send($method, $uri, $body = null, array $query = [], array $head
4972
$options[RequestOptions::BODY] = $body;
5073
}
5174

52-
return $this->guzzleClient->request($method, $uri, $options);
75+
return $options;
5376
}
54-
5577
}

src/HttpClient/HttpClient.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiAi\HttpClient;
44

5+
use GuzzleHttp\Promise\PromiseInterface;
56
use Psr\Http\Message\ResponseInterface;
67

78
/**
@@ -22,4 +23,16 @@ interface HttpClient
2223
* @return ResponseInterface
2324
*/
2425
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []);
26+
27+
/**
28+
* @param string $method
29+
* @param string $uri
30+
* @param mixed $body
31+
* @param array $query
32+
* @param array $headers
33+
* @param array $options
34+
*
35+
* @return PromiseInterface
36+
*/
37+
public function sendAsync($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []);
2538
}

src/Method/QueryApi.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use ApiAi\Client;
66
use ApiAi\ResponseHandler;
7+
use GuzzleHttp\Promise\PromiseInterface;
8+
use Psr\Http\Message\ResponseInterface;
79

810
/**
911
* Class QueryApi
@@ -47,4 +49,19 @@ public function extractMeaning($query, $extraParams = [])
4749
return $this->decodeResponse($response);
4850
}
4951

52+
/**
53+
* @param $query
54+
* @param array $extraParams
55+
*
56+
* @return PromiseInterface
57+
*/
58+
public function extractMeaningAsync($query, $extraParams = [])
59+
{
60+
$query = array_merge($extraParams, [
61+
'lang' => $this->client->getApiLanguage(),
62+
'query' => $query,
63+
]);
64+
65+
return $this->client->postAsync('query', $query)->then([$this, 'decodeResponse']);
66+
}
5067
}

0 commit comments

Comments
 (0)