Skip to content

Commit

Permalink
update: change api version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin committed Mar 16, 2024
1 parent 18b182b commit 6c515de
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ $orders = $client->Order->getOrderList([
]);
```

## Change API version

> As default, API version 202309 will be used in every api call
> Use below example to change it
```php
$products = $client->Product->useVersion('202312')->checkListingPrerequisites();
```

## Webhook

Use webhook to receive incoming notification from tiktok shop
Expand Down
9 changes: 9 additions & 0 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace EcomPHP\TiktokShop;

use DateTimeInterface;
use EcomPHP\TiktokShop\Errors\TiktokShopException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use EcomPHP\TiktokShop\Client as TiktokShopClient;
Expand All @@ -32,12 +33,20 @@ abstract class Resource

public function useVersion($version)
{
if (intval($version) < 202309) {
throw new TiktokShopException('API version 202309 is the minimum requirement');
}

$this->version = $version;

return $this;
}

public function useHttpClient(Client $client)
{
$this->httpClient = $client;

return $this;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

namespace EcomPHP\TiktokShop\Tests;

use EcomPHP\TiktokShop\Errors\TiktokShopException;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use EcomPHP\TiktokShop\Errors\ResponseException;
use EcomPHP\TiktokShop\Resource;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

class ResourceTest extends TestCase
{
Expand Down Expand Up @@ -57,4 +60,29 @@ public function testLastMessageAndRequestId()
$this->assertEquals($this->resource->getLastMessage(), 'error message');
$this->assertEquals($this->resource->getLastRequestId(), 'request id');
}

public function testChangeAPIVersion()
{
$container = [];

$mockHandler = new MockHandler();
$mockHandler->append(new Response(200, [], '{"code":0,"message":"success","data":[],"request_id":"sample request id"}'));

$handler = HandlerStack::create($mockHandler);
$handler->push(Middleware::history($container));

$this->resource->useHttpClient(new Client([
'handler' => $handler
]));

// test newer version
$this->resource->useVersion('202409')->call('GET', 'test-api');
$request = array_pop($container)['request'];
$this->assertEquals('202409/test-api', $request->getUri()->getPath());

// test older version
$this->expectException(TiktokShopException::class);
$this->expectExceptionMessage('API version 202309 is the minimum requirement');
$this->resource->useVersion('202109')->call('GET', 'test-api');
}
}

0 comments on commit 6c515de

Please sign in to comment.