Skip to content

Commit

Permalink
Merge pull request #134 from nicklaw5/v6
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandin authored Jul 30, 2021
2 parents d4cab23 + 5bcf412 commit 1846cb2
Show file tree
Hide file tree
Showing 92 changed files with 396 additions and 3,982 deletions.
4 changes: 2 additions & 2 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in('src/NewTwitchApi/')
->in('test/NewTwitchApi/')
->in('src/')
->in('test/')
;

return PhpCsFixer\Config::create()
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ $twitch_client_id = 'TWITCH_CLIENT_ID';
$twitch_client_secret = 'TWITCH_CLIENT_SECRET';
$twitch_scopes = '';

$helixGuzzleClient = new \NewTwitchApi\HelixGuzzleClient($twitch_client_id);
$newTwitchApi = new \NewTwitchApi\NewTwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);
$oauth = $newTwitchApi->getOauthApi();
$helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id);
$twitchApi = new \TwitchApi\TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);
$oauth = $twitchApi->getOauthApi();

try {
$token = $oauth->getAppAccessToken($twitch_scopes ?? '');
Expand All @@ -62,9 +62,9 @@ $twitch_client_id = 'TWITCH_CLIENT_ID';
$twitch_client_secret = 'TWITCH_CLIENT_SECRET';
$twitch_scopes = '';

$helixGuzzleClient = new \NewTwitchApi\HelixGuzzleClient($twitch_client_id);
$newTwitchApi = new \NewTwitchApi\NewTwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);
$oauth = $newTwitchApi->getOauthApi();
$helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id);
$twitchApi = new \TwitchApi\TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);
$oauth = $twitchApi->getOauthApi();

// Get the code from URI
$code = $_GET['code'];
Expand Down Expand Up @@ -104,9 +104,9 @@ $twitch_client_secret = 'TWITCH_CLIENT_SECRET';
$twitch_scopes = '';
$user_refresh_token = 'REFRESH_TOKEN';

$helixGuzzleClient = new \NewTwitchApi\HelixGuzzleClient($twitch_client_id);
$newTwitchApi = new \NewTwitchApi\NewTwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);
$oauth = $newTwitchApi->getOauthApi();
$helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id);
$twitchApi = new \TwitchApi\TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);
$oauth = $twitchApi->getOauthApi();

try {
$token = $oauth->getAppAccessToken($twitch_scopes ?? '');
Expand All @@ -124,11 +124,11 @@ try {

### Usage of the API Classes

Everything stems from the `NewTwitchApi` class. However, if you want to individually instantiate `UsersApi`, `OauthApi`, etc. you are free to do so.
Everything stems from the `TwitchApi` class. However, if you want to individually instantiate `UsersApi`, `OauthApi`, etc. you are free to do so.

The API calls generally return an object implementing `ResponseInterface`. Since you are getting the full `Response` object, you'll need to handle its contents, e.g. by decoding then into an object with `json_decode()`. This library does not assume this is what you want to do, so it does not do this for you automatically. This library simply acts as a middleman between your code and Twitch, providing you with the raw responses the Twitch API returns.

The individual API classes that can be called from `NewTwitchApi` correspond to the [Twitch API documentation](https://dev.twitch.tv/docs/api/). The rest of the API classes are based on the resources listed [here](https://dev.twitch.tv/docs/api/reference/). The methods in the classes generally correspond to the endpoints for each resource. The naming convention was chosen to try and match the Twitch documentation. Each primary endpoint method (not convenience or helper methods) should have an `@link` annotation with a URL to that endpoint's specific documentation.
The individual API classes that can be called from `TwitchApi` correspond to the [Twitch API documentation](https://dev.twitch.tv/docs/api/). The rest of the API classes are based on the resources listed [here](https://dev.twitch.tv/docs/api/reference/). The methods in the classes generally correspond to the endpoints for each resource. The naming convention was chosen to try and match the Twitch documentation. Each primary endpoint method (not convenience or helper methods) should have an `@link` annotation with a URL to that endpoint's specific documentation.

Here is a sample of retrieving a users table from their access token:

Expand All @@ -140,14 +140,14 @@ $twitch_access_token = 'the token';

// The Guzzle client used can be the included `HelixGuzzleClient` class, for convenience.
// You can also use a mock, fake, or other double for testing, of course.
$helixGuzzleClient = new \NewTwitchApi\HelixGuzzleClient($twitch_client_id);
$helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id);

// Instantiate NewTwitchApi. Can be done in a service layer and injected as well.
$newTwitchApi = new NewTwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);
// Instantiate TwitchApi. Can be done in a service layer and injected as well.
$twitchApi = new TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret);

try {
// Make the API call. A ResponseInterface object is returned.
$response = $newTwitchApi->getUsersApi()->getUserByAccessToken($twitch_access_token);
$response = $twitchApi->getUsersApi()->getUserByAccessToken($twitch_access_token);

// Get and decode the actual content sent by Twitch.
$responseContent = json_decode($response->getBody()->getContents());
Expand Down
84 changes: 45 additions & 39 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
{
"name": "nicklaw5/twitch-api-php",
"type": "library",
"description": "A Twitch API client for PHP.",
"keywords": ["twitch", "twitch.tv", "twitch-tv", "twitch-api", "api"],
"homepage": "http://github.com/nicklaw5/twitch-api-php",
"license": "MIT",
"authors": [
{
"name": "Nicholas Law",
"homepage": "https://github.com/nicklaw5"
},
{
"name": "Brian Zwahr",
"homepage": "https://github.com/echosa"
},
{
"name": "Brandin Arsenault",
"homepage": "https://github.com/brandinarsenault"
}
],
"require": {
"php": ">=7.4.0",
"ext-json": "*",
"guzzlehttp/guzzle": "~6.0|~7.0"
"name": "nicklaw5/twitch-api-php",
"type": "library",
"description": "A Twitch API client for PHP.",
"keywords": [
"twitch",
"twitch.tv",
"twitch-tv",
"twitch-api",
"api"
],
"homepage": "http://github.com/nicklaw5/twitch-api-php",
"license": "MIT",
"authors": [
{
"name": "Nicholas Law",
"homepage": "https://github.com/nicklaw5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.13",
"phpspec/phpspec": "^6.1",
"phpunit/phpunit": "^8.5"
{
"name": "Brian Zwahr",
"homepage": "https://github.com/echosa"
},
"autoload": {
"psr-4": {
"TwitchApi\\": "src/",
"NewTwitchApi\\": "src/NewTwitchApi",
"NewTwitchApi\\Tests\\": "test/NewTwitchApi"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "test/"
}
{
"name": "Brandin Arsenault",
"homepage": "https://github.com/brandinarsenault"
}
],
"require": {
"php": ">=7.4.0",
"ext-json": "*",
"guzzlehttp/guzzle": "~6.0|~7.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.13",
"phpspec/phpspec": "^6.1",
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
"TwitchApi\\": "src/",
"TwitchApi\\Tests\\": "test/TwitchApi",
"NewTwitchApi\\": "src/NewTwitchApi"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "test/"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace spec\NewTwitchApi\Auth;
namespace spec\TwitchApi\Auth;

use GuzzleHttp\Psr7\Uri;
use PhpSpec\ObjectBehavior;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace spec\NewTwitchApi\Auth;
namespace spec\TwitchApi\Auth;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace spec\NewTwitchApi;
namespace spec\TwitchApi;

use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class HelixGuzzleClientSpec extends ObjectBehavior
Expand All @@ -14,7 +14,7 @@ function let(HelixGuzzleClient $guzzleClient)

function it_should_have_correct_base_uri()
{
$this->shouldHaveType('\NewTwitchApi\HelixGuzzleClient');
$this->shouldHaveType('\TwitchApi\HelixGuzzleClient');

/** @var Uri $uri */
$uri = $this->getConfig('base_uri');
Expand All @@ -25,21 +25,21 @@ function it_should_have_correct_base_uri()

function it_should_have_client_id_header()
{
$this->shouldHaveType('\NewTwitchApi\HelixGuzzleClient');
$this->shouldHaveType('\TwitchApi\HelixGuzzleClient');
$this->getConfig('headers')->shouldHaveKeyWithValue('Client-ID', 'TEST_CLIENT_ID');
}

function it_should_have_json_content_type_header()
{

$this->shouldHaveType('\NewTwitchApi\HelixGuzzleClient');
$this->shouldHaveType('\TwitchApi\HelixGuzzleClient');
$this->getConfig('headers')->shouldHaveKeyWithValue('Content-Type', 'application/json');
}

function it_should_have_passed_in_config_params_instead_of_defaults()
{
$this->beConstructedWith('TEST_CLIENT_ID', ['base_uri' => 'https://different.url']);
$this->shouldHaveType('\NewTwitchApi\HelixGuzzleClient');
$this->shouldHaveType('\TwitchApi\HelixGuzzleClient');
$this->getConfig('base_uri')->getHost()->shouldBe('different.url');
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class AdsApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class AnalyticsApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class BitsApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class ChannelPointsApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class ChannelsApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class ChatApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class EntitlementsApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class EventSubApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class GamesApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class ModerationApiSpec extends ObjectBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace spec\NewTwitchApi\Resources;
namespace spec\TwitchApi\Resources;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use NewTwitchApi\RequestGenerator;
use NewTwitchApi\HelixGuzzleClient;
use TwitchApi\RequestGenerator;
use TwitchApi\HelixGuzzleClient;
use PhpSpec\ObjectBehavior;

class PollsApiSpec extends ObjectBehavior
Expand Down
Loading

0 comments on commit 1846cb2

Please sign in to comment.