Skip to content

Commit

Permalink
Merge pull request #121 from ThibaultVlacich/guzzle-deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandin committed May 28, 2021
2 parents 9a91bb1 + 89cf63a commit 9ebb174
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $twitch_client_id = 'TWITCH_CLIENT_ID';
$twitch_client_secret = 'TWITCH_CLIENT_SECRET';
$twitch_scopes = '';

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

Expand All @@ -62,7 +62,7 @@ $twitch_client_id = 'TWITCH_CLIENT_ID';
$twitch_client_secret = 'TWITCH_CLIENT_SECRET';
$twitch_scopes = '';

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

Expand Down Expand Up @@ -104,7 +104,7 @@ $twitch_client_secret = 'TWITCH_CLIENT_SECRET';
$twitch_scopes = '';
$user_refresh_token = 'REFRESH_TOKEN';

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

Expand Down Expand Up @@ -140,7 +140,7 @@ $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 HelixGuzzleClient($twitch_client_id);
$helixGuzzleClient = \NewTwitchApi\HelixGuzzleClient::getClient($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);
Expand Down
6 changes: 5 additions & 1 deletion spec/NewTwitchApi/Auth/AuthGuzzleClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class AuthGuzzleClientSpec extends ObjectBehavior
{
function it_should_have_correct_base_uri()
{
$this->beConstructedThrough('getClient');
$this->shouldHaveType('\GuzzleHttp\Client');

/** @var Uri $uri */
$uri = $this->getConfig('base_uri');
$uri->getScheme()->shouldBe('https');
Expand All @@ -18,7 +21,8 @@ function it_should_have_correct_base_uri()

function it_should_have_passed_in_config_params_instead_of_defaults()
{
$this->beConstructedWith(['base_uri' => 'https://different.url']);
$this->beConstructedThrough('getClient', [['base_uri' => 'https://different.url']]);
$this->shouldHaveType('\GuzzleHttp\Client');
$this->getConfig('base_uri')->getHost()->shouldBe('different.url');
}
}
15 changes: 9 additions & 6 deletions spec/NewTwitchApi/HelixGuzzleClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

class HelixGuzzleClientSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('client-id');
}

function it_should_have_correct_base_uri()
{
$this->beConstructedThrough('getClient', ['client-id']);
$this->shouldHaveType('\GuzzleHttp\Client');

/** @var Uri $uri */
$uri = $this->getConfig('base_uri');
$uri->getScheme()->shouldBe('https');
Expand All @@ -23,17 +21,22 @@ function it_should_have_correct_base_uri()

function it_should_have_client_id_header()
{
$this->beConstructedThrough('getClient', ['client-id']);
$this->shouldHaveType('\GuzzleHttp\Client');
$this->getConfig('headers')->shouldHaveKeyWithValue('Client-ID', 'client-id');
}

function it_should_have_json_content_type_header()
{
$this->beConstructedThrough('getClient', ['client-id']);
$this->shouldHaveType('\GuzzleHttp\Client');
$this->getConfig('headers')->shouldHaveKeyWithValue('Content-Type', 'application/json');
}

function it_should_have_passed_in_config_params_instead_of_defaults()
{
$this->beConstructedWith('client-id', ['base_uri' => 'https://different.url']);
$this->beConstructedThrough('getClient', ['client-id', ['base_uri' => 'https://different.url']]);
$this->shouldHaveType('\GuzzleHttp\Client');
$this->getConfig('base_uri')->getHost()->shouldBe('different.url');
}
}
6 changes: 3 additions & 3 deletions src/NewTwitchApi/Auth/AuthGuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use GuzzleHttp\Client;

class AuthGuzzleClient extends Client
class AuthGuzzleClient
{
private const BASE_URI = 'https://id.twitch.tv/oauth2/';

public function __construct(array $config = [])
public static function getClient(array $config = []): Client
{
parent::__construct($config + [
return new Client($config + [
'base_uri' => self::BASE_URI,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NewTwitchApi/Auth/OauthApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(string $clientId, string $clientSecret, Client $guzz
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->guzzleClient = $guzzleClient ?? new AuthGuzzleClient();
$this->guzzleClient = $guzzleClient ?? AuthGuzzleClient::getClient();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/NewTwitchApi/HelixGuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use GuzzleHttp\Client;

class HelixGuzzleClient extends Client
class HelixGuzzleClient
{
private const BASE_URI = 'https://api.twitch.tv/helix/';

public function __construct(string $clientId, array $config = [])
public static function getClient(string $clientId, array $config = []): Client
{
parent::__construct($config + [
return new Client($config + [
'base_uri' => self::BASE_URI,
'timeout' => 30,
'headers' => ['Client-ID' => $clientId, 'Content-Type' => 'application/json'],
Expand Down
2 changes: 1 addition & 1 deletion src/NewTwitchApi/Webhooks/WebhooksSubscriptionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(string $clientId, string $secret, Client $guzzleClie
{
$this->clientId = $clientId;
$this->secret = $secret;
$this->guzzleClient = $guzzleClient ?? new HelixGuzzleClient($clientId);
$this->guzzleClient = $guzzleClient ?? HelixGuzzleClient::getClient($clientId);
}

public function subscribeToStream(string $twitchId, string $callback, string $bearer, int $leaseSeconds = 0): void
Expand Down

0 comments on commit 9ebb174

Please sign in to comment.