Skip to content

LeagueAPI: Asynchronous requests

Daniel Dolejška edited this page Dec 16, 2018 · 1 revision

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.

Library initialization

No special library settings required.

Usage

Any request is synchronous by default and returns its result immediately.

Using nextAsync function makes next request asynchronous. This function accepts 3 parameters:

  1. 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 ).

  2. callable, optional, on request failure
    This callback function receives thrown exception as its only parameter, expected signature: function( \Exception $result ).

  3. 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:

  1. 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>