Skip to content

Commit d17e1d1

Browse files
committed
Add async concurrency support.
1 parent d18415b commit d17e1d1

File tree

4 files changed

+80
-19
lines changed

4 files changed

+80
-19
lines changed

cli

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ $arg1 = isset($argv[2]) ? trim($argv[2]) : false;
88

99
// setup xivapi
1010
$api = new \XIVAPI\XIVAPI();
11+
$api->environment->key('');
1112

12-
// set env key
13-
$api->environment->key('my_api_key');
13+
// using concurrent
14+
use GuzzleHttp\Promise;
1415

15-
$items = $api->search
16-
->indexes(['item'])
17-
->filter('ItemSearchCategory.ID', 10, \XIVAPI\Api\SearchFilters::EQUAL_TO)
18-
->limit(500)
19-
->sort('LevelItem', 'desc')
20-
->columns(['ID','Name'])
21-
->results();
16+
$promises = [
17+
'item1' => $api->async()->content->Item()->one(1675),
18+
'item2' => $api->async()->content->Item()->one(5),
19+
'item3' => $api->async()->content->Item()->one(2546),
20+
];
2221

23-
print_r($items);
22+
$results = Promise\settle($promises)->wait();
23+
$items = $api->unwrap($results);
24+
25+
print_r( $items->item1->Name . PHP_EOL);
26+
print_r( $items->item2->Name . PHP_EOL);
27+
print_r( $items->item3->Name . PHP_EOL);

src/XIVAPI/Api/Content.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public function servers(): array
469469
return Guzzle::get('/servers');
470470
}
471471

472-
public function serversByDataCenter(): array
472+
public function serversByDataCenter(): \stdClass
473473
{
474474
return Guzzle::get('/servers/dc');
475475
}

src/XIVAPI/Guzzle/Guzzle.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,58 @@ class Guzzle
1212
const ENDPOINT_DEV = 'https://xivapi.local';
1313
const TIMEOUT = 10.0;
1414
const VERIFY = false;
15-
15+
16+
/** @var Client */
17+
private static $client = null;
18+
/** @var bool */
19+
private static $async = false;
20+
/** @var array */
1621
private static $options = [];
22+
23+
/**
24+
* Set the Guzzle client
25+
*/
26+
private static function setClient()
27+
{
28+
if (self::$client === null) {
29+
self::$client = new Client([
30+
'base_uri' => self::ENDPOINT_PROD,
31+
'timeout' => self::TIMEOUT,
32+
'verify' => self::VERIFY,
33+
]);
34+
}
35+
}
1736

1837
public static function query($method, $apiEndpoint, $options = [])
1938
{
39+
self::setClient();
40+
41+
// set XIVAPI key
2042
if ($key = getenv(Environment::XIVAPI_KEY)) {
2143
$options[RequestOptions::QUERY]['key'] = $key;
2244
}
2345

46+
// set request queries
2447
foreach (self::$options as $query => $value) {
2548
$value = is_array($value) ? implode(',', $value) : $value;
2649
$options[RequestOptions::QUERY][$query] = $value;
2750
}
28-
29-
$client = new Client([
30-
'base_uri' => self::ENDPOINT_PROD,
31-
'timeout' => self::TIMEOUT,
32-
'verify' => self::VERIFY,
33-
]);
51+
52+
// allow async
53+
if (self::$async) {
54+
return self::$client->requestAsync($method, $apiEndpoint, $options);
55+
}
3456

3557
return \GuzzleHttp\json_decode(
36-
$client->request($method, $apiEndpoint, $options)->getBody()
58+
self::$client->request($method, $apiEndpoint, $options)->getBody()
3759
);
3860
}
3961

62+
public static function setAsync()
63+
{
64+
self::$async = true;
65+
}
66+
4067
public static function resetQuery()
4168
{
4269
self::$options = [];

src/XIVAPI/XIVAPI.php

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

33
namespace XIVAPI;
44

5+
use GuzzleHttp\Psr7\Response;
56
use XIVAPI\Api\Character;
67
use XIVAPI\Api\FreeCompany;
78
use XIVAPI\Api\Linkshell;
@@ -50,6 +51,35 @@ public function __construct()
5051
$this->market = new Market();
5152
$this->patchlist = new PatchList();
5253
}
54+
55+
public function reset(): XIVAPI
56+
{
57+
Guzzle::resetQuery();
58+
return $this;
59+
}
60+
61+
public function async(): XIVAPI
62+
{
63+
Guzzle::setAsync();
64+
return $this;
65+
}
66+
67+
public function unwrap($results): \stdClass
68+
{
69+
$unwrapped = (Object)[];
70+
foreach ($results as $key => $response) {
71+
/** @var Response $response */
72+
$response = $response['value'] ?? false;
73+
74+
if ($response) {
75+
$response = \GuzzleHttp\json_decode($response->getBody());
76+
}
77+
78+
$unwrapped->{$key} = $response;
79+
}
80+
81+
return $unwrapped;
82+
}
5383

5484
public function queries($queries): XIVAPI
5585
{

0 commit comments

Comments
 (0)