Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
m1so committed Jul 24, 2017
0 parents commit a36a465
Show file tree
Hide file tree
Showing 163 changed files with 19,443 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
composer.lock
.env.test
.idea
vendor
gen

21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Michal Baumgartner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# LeagueWrap
Simple yet powerful PHP wrapper for [Riot Games API](https://developer.riotgames.com/) (formerly [LeaguePHP/LeagueWrap](https://github.com/LeaguePHP/LeagueWrap) and [paquettg/leaguewrap](https://github.com/paquettg/leaguewrap)).

The library has been fully rewritten to support v3 of the League of Legends API and adapted to use modern PHP standards such as PSR-4, PSR-6 and PSR-7. Guzzle has been removed in favor of [PHP-HTTP](http://docs.php-http.org/en/latest/), while the caching and rate-limiting backends now fully support PSR-6 through [PHP Cache](http://www.php-cache.com/en/latest/).

- [Features](#features)
- [Example](#example)
- [Rate limiting and Caching](#rate-limiting-and-caching)
- [Extras](#extras)
- [Additional headers](#additional-headers)
- [Gotchas](#gotchas)
- [Batch request calls / caching](#batch-request-calls-caching)
- [Todo](#todo)

## Features

* Simple API design
* leverages PHP's magic methods and provides phpdoc annotations
* implements `ArrayAccess` and "dot notation" to quickly grab data
* Rate limiting supports app and method limits
* PSR-6 compatible caching with multiple backend adapters and framework integrations
* Batch calls - every method can accept an array of identifiers to generate multiple asynchronous requests

## Example

```php
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
```

## Rate limiting
Any PSR-6 compatible cache pool can be used for rate limiting backend. If no pool is supplied, the filesystem is used. For list of available pools see [php-cache docs](http://www.php-cache.com/en/latest/#cache-pool-implementations).

```php
// 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();
```

## Extras

### Additional headers
The underlying PSR response contains additional headers such as:
* `X-Region` - region string, can be used with `Region::create($value)`
* `X-Endpoint` - endpoint name, can be used with `BaseApi::$endpointDefinitions[$endpointName]`

## Gotchas

### Batch request calls / caching
Consider the following example:
```php
$api->addCache($cachePool)->match()->byIds([$id1, $id2, ..., $idN, $id1]);
```
it is possible that `N+1` requests will be made instead of `N` (where the second call with `$id1` would be cached) because Guzzle can start multiple requests "concurrently", therefore the second call would be fired before the first response could be received and cached.

This scenario is not that significant in most cases as requesting multiple same resources should be avoided in the application logic.

## Todo

* Finish writing annotations for phpdoc
* Custom cache TTL for individual endpoints
* Sync rate limits and usage from responses
* Clean up batch responses sorting
* Dto support?
* Should the filesystem cache be used by default or use void?

---
PHP LeagueWrap API isn't endorsed by LeagueWrap Games and doesn't reflect the views or opinions of LeagueWrap Games or anyone officially involved in producing or managing League of Legends. League of Legends and LeagueWrap Games are trademarks or registered trademarks of LeagueWrap Games, Inc. League of Legends © LeagueWrap Games, Inc.
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "m1so/leaguewrap",
"description": "Riot Games API client",
"license": "MIT",
"keywords": ["leaguewrap", "lol", "league", "riot", "rito", "games", "legends", "api"],
"authors": [
{
"name": "Michal Baumgartner",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"LeagueWrap\\": "src/LeagueWrap"
}
},
"autoload-dev": {
"psr-4": {
"LeagueWrap\\Tests\\": "tests"
}
},
"require": {
"php": "^5.6 || ^7.0",
"psr/http-message": "^1.0",
"cache/cache": "^1.0",
"php-http/httplug": "^1.1",
"php-http/discovery": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/client-common": "^1.3",
"php-http/cache-plugin": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "^6.1",
"brianium/paratest": "dev-master",
"vlucas/phpdotenv": "^2.4.0",
"php-http/guzzle6-adapter": "^1.0",
"php-http/mock-client": "^1.0"
},
"scripts": {
"test-int": "paratest -p8 -f --testsuite Integration --bootstrap tests/bootstrap.php --colors tests"
}
}
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true">
<testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>

<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit a36a465

Please sign in to comment.