Skip to content

Commit b6234af

Browse files
DEV-4690 - Update to a much cleaner way of doing it
Pawel knows his stuff
1 parent 3abea60 commit b6234af

File tree

3 files changed

+66
-31
lines changed

3 files changed

+66
-31
lines changed

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ Check out **example** and **test** directories for more specific usage examples.
1818
is the preferred way of doing authorization in Printful API. Read more about how to acquire and
1919
use an access token in our docs: https://developers.printful.com/docs/#section/Authentication
2020

21-
In order to use OAuth through the SDK you can set you can pass it in as the second argument to the constructor of the client
21+
You can create an OAuth enabled APIClient using the following factory method:
2222
```php
23-
$client = new PrintfulApiClient(null, $myOauthToken)
23+
...
24+
use Printful\PrintfulApiClient;
25+
...
26+
$client = PrintfulApiClient::createOauthClient('my-oauth-token')
2427
```
2528

26-
You can still use the old store keys
29+
You can still use the old store keys, like this:
2730
```php
28-
$client = new PrintfulApiClient($storeKey)
31+
...
32+
use Printful\PrintfulApiClient;
33+
...
34+
$client = PrintfulApiClient::createLegacyStoreKeyClient('my-legacy-store-key')
2935
```
30-
But theses are being phased out and will no longer work after
31-
September 30th, 2022.
32-
33-
If you pass in both a store key and an OAuth token, the OAuth
34-
token will take precedence.
36+
or, by using the constructor like this:
3537
```php
36-
// $myOauthToken will be used when making requests, not $storeKey
37-
$client = new PrintfulApiClient($storeKey, $myOauthToken)
38+
$client = new PrintfulApiClient($storeKey)
3839
```
40+
However, please note that legacy keys will be phased out on September 30th, 2022.

src/PrintfulApiClient.php

+31-14
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
*/
1111
class PrintfulApiClient
1212
{
13+
const TYPE_LEGACY_STORE_KEY = 'legacy-store-key';
14+
const TYPE_OAUTH_TOKEN = 'oauth-token';
15+
const DEFAULT_KEY = self::TYPE_LEGACY_STORE_KEY;
16+
1317
/**
1418
* Printful API key
15-
* @var string
19+
* @var string|null
1620
*/
1721
private $key;
1822

1923
/**
2024
* Printful OAuth token
21-
* @var string
25+
* @var string|null
2226
*/
2327
private $oauthToken;
2428

@@ -43,22 +47,35 @@ class PrintfulApiClient
4347
public $curlTimeout = 20;
4448

4549
/**
46-
* @param ?string $key
47-
* @param ?string $oauthToken
50+
* @param string $key
4851
* @throws \Printful\Exceptions\PrintfulException if the library failed to initialize
4952
*/
50-
public function __construct($key = null, $oauthToken = null)
53+
public function __construct($key, $type = self::DEFAULT_KEY)
5154
{
52-
if ($key === null && $oauthToken === null) {
53-
throw new PrintfulException('Missing credentials, please provide a store key or an OAuth token!');
54-
}
55-
56-
if ($key && strlen($key) < 32) {
55+
if ($type === self::TYPE_LEGACY_STORE_KEY && strlen($key) < 32) {
5756
throw new PrintfulException('Invalid Printful store key!');
5857
}
5958

60-
$this->key = $key;
61-
$this->oauthToken = $oauthToken;
59+
$this->key = $type === self::TYPE_LEGACY_STORE_KEY ? $key : null;
60+
$this->oauthToken = $type === self::TYPE_OAUTH_TOKEN ? $key : null;
61+
}
62+
63+
/**
64+
* @param string $key
65+
* @throws PrintfulException
66+
*/
67+
public static function createOauthClient($key)
68+
{
69+
return new self($key, self::TYPE_OAUTH_TOKEN);
70+
}
71+
72+
/**
73+
* @param string $key
74+
* @throws PrintfulException
75+
*/
76+
public static function createLegacyStoreKeyClient($key)
77+
{
78+
return new self($key, self::TYPE_LEGACY_STORE_KEY);
6279
}
6380

6481
/**
@@ -215,8 +232,8 @@ private function request($method, $path, array $params = [], $data = null)
215232
private function setCredentials($curl)
216233
{
217234
if ($this->oauthToken !== null) {
218-
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Bearer $this->oauthToken" ]);
219-
} else if ($this->key !== null) {
235+
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Bearer $this->oauthToken"]);
236+
} elseif ($this->key !== null) {
220237
curl_setopt($curl, CURLOPT_USERPWD, $this->key);
221238
} else {
222239
throw new PrintfulException('Either OAuth token or store key must be set to make this request.');

tests/ApiClient/PrintfulApiClientTest.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@
99

1010
class PrintfulApiClientTest extends TestCase
1111
{
12+
1213
/**
1314
* @throws \Printful\Exceptions\PrintfulException
1415
* @throws \Printful\Exceptions\PrintfulApiException
1516
*/
1617
public function testGet_withApiKey_returnsWithNoAuthErrors()
1718
{
18-
$sut = new PrintfulApiClient(Credentials::$apiKey);
19+
$sut = PrintfulApiClient::createLegacyStoreKeyClient(Credentials::$apiKey);
1920

2021
$this->overrideUrl($sut);
2122

22-
$sut->get('orders', [
23+
$result = $sut->get('orders', [
2324
'offset' => 0,
2425
'limit' => 10,
2526
'status' => null,
2627
]);
28+
29+
self::assertNotNull($result);
2730
}
2831

2932
/**
@@ -32,21 +35,34 @@ public function testGet_withApiKey_returnsWithNoAuthErrors()
3235
*/
3336
public function testGet_withOauthToken_returnsWithNoAuthErrors()
3437
{
35-
$sut = new PrintfulApiClient(null, Credentials::$oAuthToken);
38+
$sut = PrintfulApiClient::createOauthClient(Credentials::$oAuthToken);
3639

3740
$this->overrideUrl($sut);
3841

39-
$sut->get('orders', [
42+
$result = $sut->get('orders', [
4043
'offset' => 0,
4144
'limit' => 10,
4245
'status' => null,
4346
]);
47+
self::assertNotNull($result);
4448
}
4549

46-
public function testConstruct_withNoCredentials_throwsException()
50+
/**
51+
* @throws \Printful\Exceptions\PrintfulException
52+
* @throws \Printful\Exceptions\PrintfulApiException
53+
*/
54+
public function testGet_withInvalidCredentials_throwsApiException()
4755
{
56+
$sut = PrintfulApiClient::createOauthClient('invalid key');
57+
58+
$this->overrideUrl($sut);
59+
4860
$this->expectException(PrintfulException::class);
49-
new PrintfulApiClient();
61+
$sut->get('orders', [
62+
'offset' => 0,
63+
'limit' => 10,
64+
'status' => null,
65+
]);
5066
}
5167

5268
private function overrideUrl(PrintfulApiClient $sut)

0 commit comments

Comments
 (0)