Skip to content

Commit 9ebb174

Browse files
authored
Merge pull request #121 from ThibaultVlacich/guzzle-deprecation
2 parents 9a91bb1 + 89cf63a commit 9ebb174

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $twitch_client_id = 'TWITCH_CLIENT_ID';
4040
$twitch_client_secret = 'TWITCH_CLIENT_SECRET';
4141
$twitch_scopes = '';
4242

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

@@ -62,7 +62,7 @@ $twitch_client_id = 'TWITCH_CLIENT_ID';
6262
$twitch_client_secret = 'TWITCH_CLIENT_SECRET';
6363
$twitch_scopes = '';
6464

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

@@ -104,7 +104,7 @@ $twitch_client_secret = 'TWITCH_CLIENT_SECRET';
104104
$twitch_scopes = '';
105105
$user_refresh_token = 'REFRESH_TOKEN';
106106

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

@@ -140,7 +140,7 @@ $twitch_access_token = 'the token';
140140

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

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

spec/NewTwitchApi/Auth/AuthGuzzleClientSpec.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class AuthGuzzleClientSpec extends ObjectBehavior
99
{
1010
function it_should_have_correct_base_uri()
1111
{
12+
$this->beConstructedThrough('getClient');
13+
$this->shouldHaveType('\GuzzleHttp\Client');
14+
1215
/** @var Uri $uri */
1316
$uri = $this->getConfig('base_uri');
1417
$uri->getScheme()->shouldBe('https');
@@ -18,7 +21,8 @@ function it_should_have_correct_base_uri()
1821

1922
function it_should_have_passed_in_config_params_instead_of_defaults()
2023
{
21-
$this->beConstructedWith(['base_uri' => 'https://different.url']);
24+
$this->beConstructedThrough('getClient', [['base_uri' => 'https://different.url']]);
25+
$this->shouldHaveType('\GuzzleHttp\Client');
2226
$this->getConfig('base_uri')->getHost()->shouldBe('different.url');
2327
}
2428
}

spec/NewTwitchApi/HelixGuzzleClientSpec.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
class HelixGuzzleClientSpec extends ObjectBehavior
99
{
10-
function let()
11-
{
12-
$this->beConstructedWith('client-id');
13-
}
14-
1510
function it_should_have_correct_base_uri()
1611
{
12+
$this->beConstructedThrough('getClient', ['client-id']);
13+
$this->shouldHaveType('\GuzzleHttp\Client');
14+
1715
/** @var Uri $uri */
1816
$uri = $this->getConfig('base_uri');
1917
$uri->getScheme()->shouldBe('https');
@@ -23,17 +21,22 @@ function it_should_have_correct_base_uri()
2321

2422
function it_should_have_client_id_header()
2523
{
24+
$this->beConstructedThrough('getClient', ['client-id']);
25+
$this->shouldHaveType('\GuzzleHttp\Client');
2626
$this->getConfig('headers')->shouldHaveKeyWithValue('Client-ID', 'client-id');
2727
}
2828

2929
function it_should_have_json_content_type_header()
3030
{
31+
$this->beConstructedThrough('getClient', ['client-id']);
32+
$this->shouldHaveType('\GuzzleHttp\Client');
3133
$this->getConfig('headers')->shouldHaveKeyWithValue('Content-Type', 'application/json');
3234
}
3335

3436
function it_should_have_passed_in_config_params_instead_of_defaults()
3537
{
36-
$this->beConstructedWith('client-id', ['base_uri' => 'https://different.url']);
38+
$this->beConstructedThrough('getClient', ['client-id', ['base_uri' => 'https://different.url']]);
39+
$this->shouldHaveType('\GuzzleHttp\Client');
3740
$this->getConfig('base_uri')->getHost()->shouldBe('different.url');
3841
}
3942
}

src/NewTwitchApi/Auth/AuthGuzzleClient.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
use GuzzleHttp\Client;
88

9-
class AuthGuzzleClient extends Client
9+
class AuthGuzzleClient
1010
{
1111
private const BASE_URI = 'https://id.twitch.tv/oauth2/';
1212

13-
public function __construct(array $config = [])
13+
public static function getClient(array $config = []): Client
1414
{
15-
parent::__construct($config + [
15+
return new Client($config + [
1616
'base_uri' => self::BASE_URI,
1717
]);
1818
}

src/NewTwitchApi/Auth/OauthApi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(string $clientId, string $clientSecret, Client $guzz
2020
{
2121
$this->clientId = $clientId;
2222
$this->clientSecret = $clientSecret;
23-
$this->guzzleClient = $guzzleClient ?? new AuthGuzzleClient();
23+
$this->guzzleClient = $guzzleClient ?? AuthGuzzleClient::getClient();
2424
}
2525

2626
/**

src/NewTwitchApi/HelixGuzzleClient.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
use GuzzleHttp\Client;
88

9-
class HelixGuzzleClient extends Client
9+
class HelixGuzzleClient
1010
{
1111
private const BASE_URI = 'https://api.twitch.tv/helix/';
1212

13-
public function __construct(string $clientId, array $config = [])
13+
public static function getClient(string $clientId, array $config = []): Client
1414
{
15-
parent::__construct($config + [
15+
return new Client($config + [
1616
'base_uri' => self::BASE_URI,
1717
'timeout' => 30,
1818
'headers' => ['Client-ID' => $clientId, 'Content-Type' => 'application/json'],

src/NewTwitchApi/Webhooks/WebhooksSubscriptionApi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct(string $clientId, string $secret, Client $guzzleClie
2020
{
2121
$this->clientId = $clientId;
2222
$this->secret = $secret;
23-
$this->guzzleClient = $guzzleClient ?? new HelixGuzzleClient($clientId);
23+
$this->guzzleClient = $guzzleClient ?? HelixGuzzleClient::getClient($clientId);
2424
}
2525

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

0 commit comments

Comments
 (0)