Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekz committed Nov 6, 2020
1 parent f875a54 commit 5c54b80
Show file tree
Hide file tree
Showing 42 changed files with 16,336 additions and 5 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Test

on: [push, pull_request, workflow_dispatch]

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [ 7.4 ]
dependency-version: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}

steps:
- name: Checkout code
uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v1
with:
php-version: ${{ matrix.php }}
extensions: mbstring, sqlite, pdo_sqlite, iconv
coverage: none

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Install dependencies
run: |
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
composer.phar
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
/.idea
/.phpunit.cache
composer.lock
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
# VALORANTPHP
PSR-18 compliant package for the VALORANT API by Riot

![Test](https://github.com/teamreflex/VALORANTPHP/workflows/Test/badge.svg?branch=master)
[![Latest Version](https://img.shields.io/packagist/v/team-reflex/valorant-php.svg)](https://packagist.org/packages/team-reflex/valorant-php)
[![Downloads](https://img.shields.io/packagist/dt/team-reflex/valorant-php.svg)](https://packagist.org/packages/team-reflex/valorant-php)

PSR-18 compliant package for the VALORANT API by Riot.

## Note
The match endpoints have been written blind, as I don't have access to the VALORANT API yet.

The only resource I've been able to use is some [sample JSON](https://gist.github.com/RiotTuxedo/34e1af353d9d340619cbbfa4579fc81c) from [Riot Tuxedo](https://github.com/RiotTuxedo) in the Riot Dev Discord server.

Every DTO from the Match resource has been generated based on this JSON, so it may not be accurate, nor have every type set correctly.

## Installation
Requires PHP 7.4 as it takes advantage of its type support.

Install via composer:

```bash
composer require team-reflex/valorant-php
```

## Usage
As the package is PSR-18 compliant, it does not come with an HTTP client by default.

You can use a client such as Guzzle, and pass an instance of it when instantiating:

```bash
$http = new GuzzleHttp\Client();
$valorant = new Valorant($http, 'api_key_here', AccountRegion::AMERICAS(), MatchRegion::AMERICA());
```
Now you're ready to make requests:
```bash
$player = $valorant->fetchAccountByRiot('Kairu#1481');
```
## Contact
- [@Reflexgg](http://twitter.com/Reflexgg)
- [@Kairuxo](http://twitter.com/Kairuxo)
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "team-reflex/valorant-php",
"description": "PSR-18 compliant package for the VALORANT API by Riot.",
"require": {
"ext-json": "*",
"php": "^7.4",
"psr/http-client": "^1.0",
"illuminate/collections": "~8.0",
"spatie/data-transfer-object": "^2.5",
"myclabs/php-enum": "^1.7"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
"guzzlehttp/guzzle": "^7.2"
},
"license": "MIT",
"authors": [
{
"name": "Kyle Ward",
"email": "kairu@team-reflex.com"
}
],
"autoload": {
"psr-4": {
"Reflex\\Valorant\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
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/9.4/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
169 changes: 169 additions & 0 deletions src/ClientWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Reflex\Valorant;

use JsonException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Reflex\Valorant\Exceptions\AuthorizationException;
use Reflex\Valorant\Exceptions\BadRequestException;
use Reflex\Valorant\Exceptions\NotFoundException;
use Reflex\Valorant\Exceptions\RateLimitException;
use Reflex\Valorant\Exceptions\ServerException;
use Reflex\Valorant\Exceptions\UnexpectedErrorException;
use Reflex\Valorant\Regions\AccountRegion;
use Reflex\Valorant\Regions\MatchRegion;

class ClientWrapper
{
protected ClientInterface $client;
protected string $key;
protected AccountRegion $accountRegion;
protected MatchRegion $matchRegion;

/**
* ClientWrapper constructor.
* @param ClientInterface $client
* @param string $key
* @param AccountRegion $accountRegion
* @param MatchRegion $matchRegion
*/
public function __construct(ClientInterface $client, string $key, AccountRegion $accountRegion, MatchRegion $matchRegion)
{
$this->client = $client;
$this->key = $key;
$this->accountRegion = $accountRegion;
$this->matchRegion = $matchRegion;
}

/**
* Make a request to Riot via the HTTP client.
* @param string $method
* @param string $uri
* @param array $content
* @param bool $isAccount
* @return array
* @throws AuthorizationException
* @throws BadRequestException
* @throws JsonException
* @throws NotFoundException
* @throws RateLimitException
* @throws ServerException
* @throws UnexpectedErrorException
*/
public function request(string $method, string $uri, array $content = [], bool $isAccount = false): array
{
$region = $isAccount ? $this->accountRegion->getValue() : $this->matchRegion->getValue();
$base_uri = "https://{$region}.api.riotgames.com/{$uri}";

$response = $this->client->request($method, $base_uri, [
'query' => [],
'form_params' => $content,
'headers' => ['X-Riot-Token' => $this->getKey()],
'http_errors' => false,
'verify' => false,
]);

return $this->handleErrors($response);
}

/**
* Handles the response and throws errors accordingly.
* @param ResponseInterface $response
* @return array
* @throws AuthorizationException
* @throws BadRequestException
* @throws JsonException
* @throws NotFoundException
* @throws RateLimitException
* @throws ServerException
* @throws UnexpectedErrorException
*/
protected function handleErrors(ResponseInterface $response): array
{
$code = $response->getStatusCode();
switch ($code) {
case 200:
return json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
break;
case 400:
throw new BadRequestException('Request format was incorrect or malformed.');
break;
case 401:
throw new AuthorizationException('Unauthorized (Invalid API key or insufficient permissions)');
break;
case 404:
throw new NotFoundException('Data not found.');
break;
case 429:
throw new RateLimitException('Rate limit exceeded.');
break;
case 500:
case 502:
case 503:
case 504:
throw new ServerException("Something went wrong on Riot's end. ({$code})");
break;
default:
throw new UnexpectedErrorException($response->getBody());
break;
}
}

/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}

/**
* @param string $key
*/
public function setKey(string $key): void
{
$this->key = $key;
}

/**
* @return AccountRegion
*/
public function getAccountRegion(): AccountRegion
{
return $this->accountRegion;
}

/**
* @param AccountRegion $accountRegion
*/
public function setAccountRegion(AccountRegion $accountRegion): void
{
$this->accountRegion = $accountRegion;
}

/**
* @return MatchRegion
*/
public function getMatchRegion(): MatchRegion
{
return $this->matchRegion;
}

/**
* @param MatchRegion $matchRegion
*/
public function setMatchRegion(MatchRegion $matchRegion): void
{
$this->matchRegion = $matchRegion;
}

/**
* Get the underlying client.
* @return ClientInterface
*/
public function getClient(): ClientInterface
{
return $this->client;
}
}
16 changes: 16 additions & 0 deletions src/DTO/Ability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Reflex\Valorant\DTO;

use Spatie\DataTransferObject\DataTransferObject;

class Ability extends DataTransferObject
{
public $grenadeEffects;

public $ability1Effects;

public $ability2Effects;

public $ultimateEffects;
}
16 changes: 16 additions & 0 deletions src/DTO/AbilityCasts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Reflex\Valorant\DTO;

use Spatie\DataTransferObject\DataTransferObject;

class AbilityCasts extends DataTransferObject
{
public int $grenadeCasts;

public int $ability1Casts;

public int $ability2Casts;

public int $ultimateCasts;
}
22 changes: 22 additions & 0 deletions src/DTO/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Reflex\Valorant\DTO;

use Spatie\DataTransferObject\DataTransferObject;

class Account extends DataTransferObject
{
public string $puuid;
public string $gameName;
public string $tagLine;

/**
* Concatenate the two for the full ID.
*
* @return string
*/
public function getRiotId(): string
{
return "{$this->gameName}#{$this->tagLine}";
}
}
12 changes: 12 additions & 0 deletions src/DTO/ActiveShard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Reflex\Valorant\DTO;

use Spatie\DataTransferObject\DataTransferObject;

class ActiveShard extends DataTransferObject
{
public string $puuid;
public string $game;
public string $activeShard;
}
Loading

0 comments on commit 5c54b80

Please sign in to comment.