-
-
Notifications
You must be signed in to change notification settings - Fork 25
LeagueAPI: Asynchronous requests
Version v3.0.0
This feature allows request grouping and their asynchronous sending using Guzzle. After request is sent and its response received, user provided callbacks are invoked with received data.
No special library settings required.
Any request is synchronous by default and returns its result immediately.
Using nextAsync
function makes next request asynchronous. This function accepts 3 parameters:
-
callable
, optional, invoked on request success
This callback function receives the result of used library function as its only parameter, expected signature:function( IApiObject $result )
. -
callable
, optional, on request failure
This callback function receives thrown exception as its only parameter, expected signature:function( \Exception $result )
. -
string
, optional, request group identifier
Requests with same identifier are grouped together and executed simultaneously.
Request group has to be "commited" after you are finished adding asynchronous requests by calling commitAsync
.
This function accepts single parameter:
-
string
, request group identifier
Example:
use RiotAPI\LeagueAPI\LeagueAPI;
use RiotAPI\LeagueAPI\Objects;
// LeagueAPI initialization
$api = new LeagueAPI([...]);
// Function to be called on request success
$onSuccess = function( Objects\SummonerDto $result ) {
echo "$result->name (level $result->level)";
echo "<br>";
};
// Function to be called on request failure
$onFailure = function( $ex ) {
echo "Error occured: {$ex->getMessage()}";
};
// Adds first asynchronous request to group "accounts"
$api->nextAsync($onSuccess, $onFailure, "accounts")->getSummonerByName("I am TheKronnY");
// Adds another requests to group "accounts"
$api->nextAsync($onSuccess, $onFailure, "accounts")->getSummonerByName("Im Professor");
$api->nextAsync($onSuccess, $onFailure, "accounts");
$api->getSummonerByName("TehExuilKujdž");
// Commits asynchronous requests and processes them
$api->commitAsync("accounts");
Outputs:
I am TheKronnY (level 69)<br>
I m Professor (level 106)<br>
TehExuilKujdž (level 154)<br>