m1so/leaguewrap

Riot Games API客户端

v1.0.1 2017-10-19 15:18 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:20:52 UTC


README

一个简单而强大的PHP包装器,用于Riot Games API(之前为LeaguePHP/LeagueWrappaquettg/leaguewrap)。

该库已完全重写以支持《英雄联盟》API的v3版本,并已适应使用现代PHP标准,如PSR-4、PSR-6和PSR-7。已移除Guzzle,转而使用PHP-HTTP,同时缓存和速率限制后端现在完全支持通过PHP Cache的PSR-6。

注意,HTTP客户端是可插拔的,必须在安装库时进行选择。例如,使用Guzzle与LeagueWrap一起使用将看起来像

composer require \
    php-http/guzzle6-adapter \
    php-http/message \
    guzzlehttp/psr7 \
    m1so/leaguewrap

有关更多信息,请参阅库用户部分。

特性

  • 简单的API设计
    • 利用PHP的魔术方法和提供phpdoc注释
    • 实现ArrayAccess和"点表示法"以快速获取数据
  • 速率限制支持应用程序和方法限制
  • PSR-6兼容的缓存,具有多个后端适配器和框架集成
  • 批量调用 - 每个方法都可以接受一个标识符数组以生成多个异步请求

示例

use LeagueWrap\Client as RiotApi;

$api = new RiotApi('RGAPI-xxx', 'euw');

// The following calls produce the same results
$api->match()->getById($id);
$api->match()->byId($id);

// Support for batch calls (requires multiple async requests)
$api->match()->getById([$gameId, $anotherGameId]); // using array as an argument
$api->match()->byIds([$gameId, $anotherGameId]); // dedicated method on the Match API Class

// Simple data access
$myGame = $api->match()->byId($id);
$myGame['gameId'] === $id;
$myGame['teams.0.teamId'] === 100; // "dot" notation support for nested elements

速率限制

可以使用任何PSR-6兼容的缓存池作为速率限制后端。如果没有提供池,则使用文件系统。有关可用池的列表,请参阅php-cache文档

// Redis backend with Predis client
// @see https://github.com/php-cache/predis-adapter
$client = new \Predis\Client('tcp:/127.0.0.1:6379');
$pool = new \Cache\Adapter\Predis\PredisCachePool($client);

$api->setAppLimits([
    '10' => 100, // 100 requests per 10 seconds
], $pool);

// When using Laravel
$store = app(\Illuminate\Cache\Repository::class)->getStore();
$pool = new \Cache\Adapter\Illuminate\IlluminateCachePool($store);

$api->setLimits([
    '10' => 100, // 100 requests per 10 seconds
], $pool);

/*
 * Caching is very similar, infact the same pool can be shared with the rate-limiter.
 */
$api->addCache($pool);
$api->removeCache();

附加功能

额外头信息

底层的PSR响应包含额外的头信息,例如

  • X-Region - 区域字符串,可用于Region::create($value)
  • X-Endpoint - 端点名称,可用于BaseApi::$endpointDefinitions[$endpointName]

注意事项

批量请求调用/缓存

考虑以下示例

$api->addCache($cachePool)->match()->byIds([$id1, $id2, ..., $idN, $id1]);

可能会发出N+1个请求而不是N个(其中第二个调用带有$id1将进行缓存),因为Guzzle可以同时启动多个请求,因此第二个调用将在第一个响应收到和缓存之前被触发。

在大多数情况下,这种场景并不重要,因为应避免在应用程序逻辑中请求多个相同的资源。

待办事项

  • 完成为phpdoc编写注释
  • 为单个端点自定义缓存TTL
  • 从响应中同步速率限制和使用情况
  • 清理批量响应排序
  • 是否支持Dto?
  • 是否默认使用文件系统缓存或使用空值?

PHP LeagueWrap API未获得Riot Games的支持,并且不代表Riot Games或参与《英雄联盟》的生产或管理的任何官方人士的观点或意见。《英雄联盟》和Riot Games是Riot Games Inc.的商标或注册商标。《英雄联盟》© Riot Games Inc.