Validate AppData, easier client instantiation
This commit is contained in:
commit
1206a69bef
38
README.md
38
README.md
@ -7,25 +7,23 @@
|
|||||||
API client implementation for AliExpress TOP.
|
API client implementation for AliExpress TOP.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
1. This library uses `php-http/httplug` under the hood. If you don't want to bother with details, just install library and it's dependencies through Composer:
|
1. This library uses `php-http/httplug` under the hood. If you don't want to bother with details, just install library and it's dependencies via Composer:
|
||||||
```sh
|
```sh
|
||||||
composer require php-http/curl-client nyholm/psr7 php-http/message retailcrm/aliexpress-top-client
|
composer require php-http/curl-client nyholm/psr7 php-http/message retailcrm/aliexpress-top-client
|
||||||
```
|
```
|
||||||
Details about those third-party libraries and why you need to install them can be found [here](http://docs.php-http.org/en/latest/httplug/users.html).
|
Details about those third-party libraries and why you need to install them can be found [here](http://docs.php-http.org/en/latest/httplug/users.html).
|
||||||
|
|
||||||
2. Instantiate client like that:
|
2. Instantiate client using `TopClientFactory`:
|
||||||
```php
|
```php
|
||||||
use RetailCrm\Component\AppData;
|
use RetailCrm\Component\AppData;
|
||||||
use RetailCrm\Builder\TopClientBuilder;
|
use RetailCrm\Factory\TopClientFactory;
|
||||||
use RetailCrm\Builder\ContainerBuilder;
|
|
||||||
use RetailCrm\Component\Authenticator\TokenAuthenticator;
|
|
||||||
|
|
||||||
$appData = new AppData(AppData::OVERSEAS_ENDPOINT, 'appKey', 'appSecret');
|
$client = TopClientFactory::createClient(
|
||||||
$client = TopClientBuilder::create()
|
AppData::OVERSEAS_ENDPOINT,
|
||||||
->setContainer(ContainerBuilder::create()->build())
|
'appKey',
|
||||||
->setAppData($appData)
|
'appSecret',
|
||||||
->setAuthenticator(new TokenAuthenticator('session token here'))
|
'session token here'
|
||||||
->build();
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Create and fill request data. All requests and responses use the same naming: part of the namespace is the first word in the request name, and everything else is in the request DTO class name. Requests live under `RetailCrm\Model\Request` namespace, and responses can be found in the `RetailCrm\Model\Response` namespace.
|
3. Create and fill request data. All requests and responses use the same naming: part of the namespace is the first word in the request name, and everything else is in the request DTO class name. Requests live under `RetailCrm\Model\Request` namespace, and responses can be found in the `RetailCrm\Model\Response` namespace.
|
||||||
@ -75,5 +73,21 @@ $client = TopClientBuilder::create()
|
|||||||
->build();
|
->build();
|
||||||
```
|
```
|
||||||
Logger should implement `Psr\Log\LoggerInterface` (PSR-3), HTTP client should implement `Psr\Http\TopClient\TopClientInterface` (PSR-18), HTTP objects must be compliant to PSR-7.
|
Logger should implement `Psr\Log\LoggerInterface` (PSR-3), HTTP client should implement `Psr\Http\TopClient\TopClientInterface` (PSR-18), HTTP objects must be compliant to PSR-7.
|
||||||
|
You can use your own container if you want to - it must be compliant to PSR-11. This is strongly discouraged because it'll be much easier to just integrate library with your own application, and your own DI system.
|
||||||
|
|
||||||
|
The simplest example of client initialization without using `TopClientFactory` looks like this:
|
||||||
|
```php
|
||||||
|
use RetailCrm\Component\AppData;
|
||||||
|
use RetailCrm\Builder\TopClientBuilder;
|
||||||
|
use RetailCrm\Builder\ContainerBuilder;
|
||||||
|
use RetailCrm\Component\Authenticator\TokenAuthenticator;
|
||||||
|
|
||||||
|
$appData = new AppData(AppData::OVERSEAS_ENDPOINT, 'appKey', 'appSecret');
|
||||||
|
$client = TopClientBuilder::create()
|
||||||
|
->setContainer(ContainerBuilder::create()->build())
|
||||||
|
->setAppData($appData)
|
||||||
|
->setAuthenticator(new TokenAuthenticator('session token here'))
|
||||||
|
->build();
|
||||||
|
```
|
||||||
|
In fact, `TopClientFactory` works just like this under the hood.
|
||||||
|
|
||||||
You can use your own container - it must be compliant to PSR-11. This is strongly discouraged because it'll be much easier to just integrate library with your own application, and your own DI system.
|
|
||||||
|
@ -26,7 +26,7 @@ class AppData implements AppDataInterface
|
|||||||
/**
|
/**
|
||||||
* @var string $serviceUrl
|
* @var string $serviceUrl
|
||||||
* @Assert\Url()
|
* @Assert\Url()
|
||||||
* @Assert\Choice(choices=Client::AVAILABLE_ENDPOINTS, message="Invalid endpoint provided.")
|
* @Assert\Choice(choices=AppData::AVAILABLE_ENDPOINTS, message="Invalid endpoint provided.")
|
||||||
*/
|
*/
|
||||||
protected $serviceUrl;
|
protected $serviceUrl;
|
||||||
|
|
||||||
|
53
src/Factory/TopClientFactory.php
Normal file
53
src/Factory/TopClientFactory.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PHP version 7.3
|
||||||
|
*
|
||||||
|
* @category TopClientFactory
|
||||||
|
* @package RetailCrm\Factory
|
||||||
|
*/
|
||||||
|
namespace RetailCrm\Factory;
|
||||||
|
|
||||||
|
use RetailCrm\Builder\ContainerBuilder;
|
||||||
|
use RetailCrm\Builder\TopClientBuilder;
|
||||||
|
use RetailCrm\Component\AppData;
|
||||||
|
use RetailCrm\Component\Authenticator\TokenAuthenticator;
|
||||||
|
use RetailCrm\TopClient\TopClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TopClientFactory
|
||||||
|
*
|
||||||
|
* @category TopClientFactory
|
||||||
|
* @package RetailCrm\Factory
|
||||||
|
*/
|
||||||
|
class TopClientFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create new TopClient
|
||||||
|
*
|
||||||
|
* @param string $serviceUrl
|
||||||
|
* @param string $appKey
|
||||||
|
* @param string $appSecret
|
||||||
|
* @param string $token
|
||||||
|
*
|
||||||
|
* @return \RetailCrm\TopClient\TopClient
|
||||||
|
* @throws \RetailCrm\Component\Exception\ValidationException
|
||||||
|
*/
|
||||||
|
public static function createClient(
|
||||||
|
string $serviceUrl,
|
||||||
|
string $appKey,
|
||||||
|
string $appSecret,
|
||||||
|
string $token = ''
|
||||||
|
): TopClient {
|
||||||
|
$appData = new AppData($serviceUrl, $appKey, $appSecret);
|
||||||
|
$builder = TopClientBuilder::create()
|
||||||
|
->setContainer(ContainerBuilder::create()->build())
|
||||||
|
->setAppData($appData);
|
||||||
|
|
||||||
|
if ('' !== $token) {
|
||||||
|
$builder->setAuthenticator(new TokenAuthenticator($token));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $builder->build();
|
||||||
|
}
|
||||||
|
}
|
@ -108,6 +108,7 @@ class TopClient implements TopClientInterface
|
|||||||
public function validateSelf(): void
|
public function validateSelf(): void
|
||||||
{
|
{
|
||||||
$this->validate($this);
|
$this->validate($this);
|
||||||
|
$this->validate($this->appData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
41
tests/RetailCrm/Tests/Factory/TopClientFactoryTest.php
Normal file
41
tests/RetailCrm/Tests/Factory/TopClientFactoryTest.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PHP version 7.3
|
||||||
|
*
|
||||||
|
* @category TopClientFactoryTest
|
||||||
|
* @package RetailCrm\Tests\Factory
|
||||||
|
*/
|
||||||
|
namespace RetailCrm\Tests\Factory;
|
||||||
|
|
||||||
|
use RetailCrm\Component\AppData;
|
||||||
|
use RetailCrm\Component\Exception\ValidationException;
|
||||||
|
use RetailCrm\Factory\TopClientFactory;
|
||||||
|
use RetailCrm\Test\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TopClientFactoryTest
|
||||||
|
*
|
||||||
|
* @category TopClientFactoryTest
|
||||||
|
* @package RetailCrm\Tests\Factory
|
||||||
|
*/
|
||||||
|
class TopClientFactoryTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testCreateClient(): void
|
||||||
|
{
|
||||||
|
$client = TopClientFactory::createClient(
|
||||||
|
AppData::OVERSEAS_ENDPOINT,
|
||||||
|
'appKey',
|
||||||
|
'appSecret',
|
||||||
|
'token'
|
||||||
|
);
|
||||||
|
|
||||||
|
self::assertNotEmpty($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateClientException(): void
|
||||||
|
{
|
||||||
|
$this->expectException(ValidationException::class);
|
||||||
|
TopClientFactory::createClient('https://example.com', 'appKey', 'appSecret');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user