1
0
mirror of synced 2024-11-27 23:36:02 +03:00

Validate AppData, easier client instantiation

This commit is contained in:
Alex Lushpai 2020-12-29 12:38:21 +03:00 committed by GitHub
commit 1206a69bef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 122 additions and 13 deletions

View File

@ -7,25 +7,23 @@
API client implementation for AliExpress TOP.
## 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
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).
2. Instantiate client like that:
2. Instantiate client using `TopClientFactory`:
```php
use RetailCrm\Component\AppData;
use RetailCrm\Builder\TopClientBuilder;
use RetailCrm\Builder\ContainerBuilder;
use RetailCrm\Component\Authenticator\TokenAuthenticator;
use RetailCrm\Factory\TopClientFactory;
$appData = new AppData(AppData::OVERSEAS_ENDPOINT, 'appKey', 'appSecret');
$client = TopClientBuilder::create()
->setContainer(ContainerBuilder::create()->build())
->setAppData($appData)
->setAuthenticator(new TokenAuthenticator('session token here'))
->build();
$client = TopClientFactory::createClient(
AppData::OVERSEAS_ENDPOINT,
'appKey',
'appSecret',
'session token here'
);
```
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();
```
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.

View File

@ -26,7 +26,7 @@ class AppData implements AppDataInterface
/**
* @var string $serviceUrl
* @Assert\Url()
* @Assert\Choice(choices=Client::AVAILABLE_ENDPOINTS, message="Invalid endpoint provided.")
* @Assert\Choice(choices=AppData::AVAILABLE_ENDPOINTS, message="Invalid endpoint provided.")
*/
protected $serviceUrl;

View 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();
}
}

View File

@ -108,6 +108,7 @@ class TopClient implements TopClientInterface
public function validateSelf(): void
{
$this->validate($this);
$this->validate($this->appData);
}
/**

View 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');
}
}