Skip to content

Commit

Permalink
Merge pull request #21 from scottcharlesworth/v4-update
Browse files Browse the repository at this point in the history
IGDB API Version 4
  • Loading branch information
marcreichel committed Oct 7, 2020
2 parents 63ff7a1 + 6e4f59d commit 067f9f6
Show file tree
Hide file tree
Showing 32 changed files with 70 additions and 307 deletions.
12 changes: 5 additions & 7 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

return [
/*
* This is the API Token you got from https://api.igdb.com
* These are the credentials you got from https://dev.twitch.tv/console/apps
*/
'api_token' => env('IGDB_TOKEN', ''),
'credentials' => [
'client_id' => env('TWITCH_CLIENT_ID', ''),
'client_secret' => env('TWITCH_CLIENT_SECRET', ''),
],

/*
* This package caches queries automatically (for 1 hour per default).
Expand All @@ -18,9 +21,4 @@
* This is the per-page limit for your tier.
*/
'per_page_limit' => 500,

/*
* This is the offset limit for your tier.
*/
'offset_limit' => 5000,
];
15 changes: 1 addition & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ $games = Game::where('first_release_date', '>=', 1546297200)->get();

Here's a list of all available Models:

- Achievement
- AchievementIcon
- AgeRating
- AgeRatingContentDescription
- AlternativeName
Expand All @@ -81,7 +79,6 @@ Here's a list of all available Models:
- CompanyWebsite
- Cover
- ExternalGame
- Feed
- Franchise
- Game
- GameEngine
Expand All @@ -95,28 +92,18 @@ Here's a list of all available Models:
- InvolvedCompany
- Keyword
- MultiplayerMode
- Page
- PageBackground
- PageLogo
- PageWebsite
- Platform
- PlatformFamily
- PlatformLogo
- PlatformVersion
- PlatformVersionCompany
- PlatformVersionReleaseDate
- PlatformWebsite
- PlayerPerspective
- ProductFamily
- Pulse
- PulseGroup
- PulseSource
- PulseUrl
- ReleaseDate
- Screenshot
- Search
- Theme
- TimeToBeat
- Title
- Website

### Query Builder
Expand Down
69 changes: 53 additions & 16 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
use Illuminate\Http\Response;
use Illuminate\Pagination\Paginator;
Expand All @@ -14,6 +15,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use MarcReichel\IGDBLaravel\Exceptions\AuthenticationException;
use MarcReichel\IGDBLaravel\Exceptions\MissingEndpointException;
use MarcReichel\IGDBLaravel\Exceptions\ModelNotFoundException;
use MarcReichel\IGDBLaravel\Exceptions\ServiceException;
Expand Down Expand Up @@ -143,10 +145,10 @@ protected function init(): void
private function initClient(): void
{
$this->client = new Client([
'base_uri' => 'https://api-v3.igdb.com/',
'base_uri' => 'https://api.igdb.com/v4/',
'headers' => [
'user-key' => config('igdb.api_token'),
'User-Agent' => 'IGDB-Laravel-Wrapper/0.1-Dev',
'Accept' => 'application/json',
'Client-ID' => config('igdb.credentials.client_id'),
],
]);
}
Expand All @@ -169,6 +171,43 @@ protected function resetCacheLifetime(): void
$this->cacheLifetime = config('igdb.cache_lifetime');
}

/**
* Retrieves an Access Token from Twitch.
*
* @return string
* @throws AuthenticationException
*/
protected function retrieveAccessToken(): string
{
$accessTokenCacheKey = 'igdb_cache.access_token';

if ($accessToken = Cache::get($accessTokenCacheKey, false)) {
return $accessToken;
}

try {
$guzzleClient = new Client();
$response = $guzzleClient->post(
'https://id.twitch.tv/oauth2/token?'
.'client_id='.config('igdb.credentials.client_id')
.'&client_secret='.config('igdb.credentials.client_secret')
.'&grant_type=client_credentials'
)->getBody();
} catch (GuzzleException $exception) {
$response = '';
}

$response = json_decode($response, true);

if (isset($response['access_token']) && $response['expires_in']) {
Cache::put($accessTokenCacheKey, (string) $response['access_token'], (int) $response['expires_in']);

return (string) $response['access_token'];
} else {
throw new AuthenticationException('Access Token could not be retrieved from Twitch.');
}
}

/**
* Set the "limit" value of the query.
*
Expand Down Expand Up @@ -205,13 +244,7 @@ public function take(int $limit): self
*/
public function offset(int $offset): self
{
$tierMaximum = max(0, config('igdb.offset_limit', 150));
if ($tierMaximum === 0) {
$this->query->put('offset', $offset);
} else {
$offset = min($offset, config('igdb.offset_limit', 150));
$this->query->put('offset', $offset);
}
$this->query->put('offset', $offset);

return $this;
}
Expand Down Expand Up @@ -1323,7 +1356,7 @@ protected function getQuery()
public function endpoint(string $endpoint)
{
if ($this->class === null) {
$this->endpoint = Str::start($endpoint, '/');
$this->endpoint = $endpoint;
}

return $this;
Expand Down Expand Up @@ -1356,8 +1389,7 @@ protected function setEndpoint($model)

$class = class_basename($this->class);

$this->endpoint = ($model->privateEndpoint ? '/private' : '') . Str::start(Str::snake(Str::plural($class)),
'/');
$this->endpoint = Str::snake(Str::plural($class));
}
} catch (\ReflectionException $e) {
}
Expand Down Expand Up @@ -1392,10 +1424,12 @@ private function castDate($date)
* Execute the query.
*
* @return mixed|string
* @throws \MarcReichel\IGDBLaravel\Exceptions\MissingEndpointException
* @throws \MarcReichel\IGDBLaravel\Exceptions\MissingEndpointException|AuthenticationException
*/
public function get()
{
$accessToken = $this->retrieveAccessToken();

if ($this->endpoint) {

$cacheKey = 'igdb_cache.' . md5($this->endpoint . $this->getQuery());
Expand All @@ -1405,10 +1439,13 @@ public function get()
}

$data = Cache::remember($cacheKey, $this->cacheLifetime,
function () {
function () use ($accessToken) {
try {
return collect(json_decode($this->client->get($this->endpoint,
return collect(json_decode($this->client->post($this->endpoint,
[
'headers' => [
'Authorization' => 'Bearer '.$accessToken,
],
'body' => $this->getQuery(),
])->getBody()));
} catch (\Exception $exception) {
Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/AuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace MarcReichel\IGDBLaravel\Exceptions;

use Exception;

class AuthenticationException extends Exception
{
}
9 changes: 0 additions & 9 deletions src/Models/Achievement.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Models/AchievementIcon.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Models/Credit.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Models/Feed.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Models/FeedFollow.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Models/Follow.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Models/ListEntry.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/Models/ListModel.php

This file was deleted.

4 changes: 1 addition & 3 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Model implements ArrayAccess, Arrayable, Jsonable
HasRelationships;

public $identifier;
public $privateEndpoint = false;

protected $endpoint;
public $builder;
Expand Down Expand Up @@ -321,8 +320,7 @@ protected function setEndpoint()
if (!$this->endpoint) {
$class = class_basename(get_class($this));

$this->endpoint = ($this->privateEndpoint ? '/private' : '') .
Str::start(Str::snake(Str::plural($class)), '/');
$this->endpoint = Str::snake(Str::plural($class));
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/Models/Page.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Models/PageBackground.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Models/PageLogo.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Models/PageWebsite.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Models/Person.php

This file was deleted.

Loading

0 comments on commit 067f9f6

Please sign in to comment.