Skip to content

Commit

Permalink
Refactored the API endpoints and client
Browse files Browse the repository at this point in the history
  • Loading branch information
gplanchat committed Oct 27, 2021
1 parent 65829b4 commit 48cf2b4
Show file tree
Hide file tree
Showing 120 changed files with 2,698 additions and 763 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"autoload-dev": {
"psr-4": {
"spec\\Diglin\\Sylius\\ApiClient\\": "spec/",
"Diglin\\Sylius\\ApiClient\\tests\\": "tests/"
}
},
Expand All @@ -33,12 +34,13 @@
"php-http/message-factory": "^v1.0",
"php-http/multipart-stream-builder": "^1.0",
"php-http/client-implementation": "^1.0",
"symfony/expression-language": "^3.0|^4.0|^5.0"
"symfony/expression-language": "^3.0|^4.0|^5.0",
"webmozart/assert": "^1.10"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^5.7",
"phpspec/phpspec": "^5.0",
"phpunit/phpunit": "^9.0",
"phpspec/phpspec": "^7.1",
"symfony/yaml": "^4.2",
"donatj/mock-webserver": "^2.0",
"php-http/guzzle6-adapter": "^2.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace spec\Diglin\Sylius\ApiClient\Api;
namespace spec\Diglin\Sylius\ApiClient\Api\Legacy;

use Diglin\Sylius\ApiClient\Api\AuthenticationApi;
use Diglin\Sylius\ApiClient\Api\AuthenticationApiInterface;
use Diglin\Sylius\ApiClient\Api\Legacy\AuthenticationApi;
use Diglin\Sylius\ApiClient\Api\Legacy\AuthenticationApiInterface;
use Diglin\Sylius\ApiClient\Client\HttpClient;
use Diglin\Sylius\ApiClient\Routing\UriGeneratorInterface;
use PhpSpec\ObjectBehavior;
Expand Down
2 changes: 1 addition & 1 deletion spec/Client/AuthenticatedHttpClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace spec\Diglin\Sylius\ApiClient\Client;

use Diglin\Sylius\ApiClient\Api\AuthenticationApiInterface;
use Diglin\Sylius\ApiClient\Api\Authentication\AuthenticationApiInterface;
use Diglin\Sylius\ApiClient\Client\AuthenticatedHttpClient;
use Diglin\Sylius\ApiClient\Client\HttpClient;
use Diglin\Sylius\ApiClient\Client\HttpClientInterface;
Expand Down
159 changes: 159 additions & 0 deletions spec/Client/LegacyAuthenticatedHttpClientSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace spec\Diglin\Sylius\ApiClient\Client;

use Diglin\Sylius\ApiClient\Api\Legacy\AuthenticationApiInterface;
use Diglin\Sylius\ApiClient\Client\HttpClient;
use Diglin\Sylius\ApiClient\Client\HttpClientInterface;
use Diglin\Sylius\ApiClient\Client\LegacyAuthenticatedHttpClient;
use Diglin\Sylius\ApiClient\Exception\UnauthorizedHttpException;
use Diglin\Sylius\ApiClient\Security\LegacyAuthentication;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;

class LegacyAuthenticatedHttpClientSpec extends ObjectBehavior
{
public function let(
HttpClient $httpClient,
AuthenticationApiInterface $authenticationApi,
LegacyAuthentication $authentication
) {
$this->beConstructedWith($httpClient, $authenticationApi, $authentication);
}

public function it_is_initializable()
{
$this->shouldHaveType(LegacyAuthenticatedHttpClient::class);
$this->shouldImplement(HttpClientInterface::class);
}

public function it_sends_an_authenticated_and_successful_request_when_access_token_is_defined(
$httpClient,
$authentication,
ResponseInterface $response
) {
$authentication->getAccessToken()->willReturn('bar');

$httpClient->sendRequest(
'POST',
'http://diglin.com/api/rest/v1/products/foo',
['Content-Type' => 'application/json', 'Authorization' => 'Bearer bar'],
'{"identifier": "foo"}'
)->willReturn($response);

$this->sendRequest(
'POST',
'http://diglin.com/api/rest/v1/products/foo',
['Content-Type' => 'application/json'],
'{"identifier": "foo"}'
)->shouldReturn($response);
}

public function it_sends_an_authenticated_and_successful_request_at_first_call(
$httpClient,
$authenticationApi,
$authentication,
ResponseInterface $response
) {
$authentication->getClientId()->willReturn('client_id');
$authentication->getSecret()->willReturn('secret');
$authentication->getUsername()->willReturn('julia');
$authentication->getPassword()->willReturn('julia_pwd');
$authentication->getAccessToken()->willReturn(null, 'foo');

$authenticationApi
->authenticateByPassword('client_id', 'secret', 'julia', 'julia_pwd')
->willReturn([
'access_token' => 'foo',
'expires_in' => 3600,
'token_type' => 'bearer',
'scope' => null,
'refresh_token' => 'bar',
])
;

$authentication
->setAccessToken('foo')
->shouldBeCalled()
->willReturn($authentication)
;

$authentication
->setRefreshToken('bar')
->shouldBeCalled()
->willReturn($authentication)
;

$httpClient->sendRequest(
'POST',
'http://diglin.com/api/rest/v1/products/foo',
['Content-Type' => 'application/json', 'Authorization' => 'Bearer foo'],
'{"identifier": "foo"}'
)->willReturn($response);

$this->sendRequest(
'POST',
'http://diglin.com/api/rest/v1/products/foo',
['Content-Type' => 'application/json'],
'{"identifier": "foo"}'
)->shouldReturn($response);
}

public function it_sends_an_authenticated_and_successful_request_when_access_token_expired(
$httpClient,
$authenticationApi,
$authentication,
ResponseInterface $response
) {
$authentication->getClientId()->willReturn('client_id');
$authentication->getSecret()->willReturn('secret');
$authentication->getUsername()->willReturn('julia');
$authentication->getPassword()->willReturn('julia_pwd');
$authentication->getAccessToken()->willReturn('foo', 'foo', 'baz');
$authentication->getRefreshToken()->willReturn('bar');

$httpClient->sendRequest(
'POST',
'http://diglin.com/api/rest/v1/products/foo',
['Content-Type' => 'application/json', 'Authorization' => 'Bearer foo'],
'{"identifier": "foo"}'
)->willThrow(UnauthorizedHttpException::class);

$authenticationApi
->authenticateByRefreshToken('client_id', 'secret', 'bar')
->willReturn([
'access_token' => 'baz',
'expires_in' => 3600,
'token_type' => 'bearer',
'scope' => null,
'refresh_token' => 'foz',
])
;

$authentication
->setAccessToken('baz')
->shouldBeCalled()
->willReturn($authentication)
;

$authentication
->setRefreshToken('foz')
->shouldBeCalled()
->willReturn($authentication)
;

$httpClient->sendRequest(
'POST',
'http://diglin.com/api/rest/v1/products/foo',
['Content-Type' => 'application/json', 'Authorization' => 'Bearer baz'],
'{"identifier": "foo"}'
)->willReturn($response);

$this->sendRequest(
'POST',
'http://diglin.com/api/rest/v1/products/foo',
['Content-Type' => 'application/json'],
'{"identifier": "foo"}'
);
}
}
2 changes: 1 addition & 1 deletion spec/Routing/UriGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function it_generates_uri_having_search_parameter_encoded_in_json()

$this
->generate('/api', [], $queryParameters)
->shouldReturn(static::BASE_URI.'api?search=%7B%22categories%22%3A%5B%7B%22operator%22%3A%22IN%22%2C%22value%22%3A%22master%22%7D%5D%7D')
->shouldReturn(static::BASE_URI.'api?search%5Bcategories%5D%5B0%5D%5Boperator%5D=IN&search%5Bcategories%5D%5B0%5D%5Bvalue%5D=master')
;
}
}
12 changes: 6 additions & 6 deletions spec/SyliusClientSpec.php → spec/SyliusLegacyClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace spec\Diglin\Sylius\ApiClient;

use Diglin\Sylius\ApiClient\Api;
use Diglin\Sylius\ApiClient\Api\Legacy as Api;
use Diglin\Sylius\ApiClient\Security\Authentication;
use Diglin\Sylius\ApiClient\SyliusClient;
use Diglin\Sylius\ApiClient\SyliusClientInterface;
use Diglin\Sylius\ApiClient\SyliusAdminClient;
use Diglin\Sylius\ApiClient\SyliusAdminClientInterface;
use PhpSpec\ObjectBehavior;

class SyliusClientSpec extends ObjectBehavior
class SyliusLegacyClientSpec extends ObjectBehavior
{
public function let(
Authentication $authentication,
Expand Down Expand Up @@ -70,8 +70,8 @@ public function let(

public function it_is_initializable()
{
$this->shouldImplement(SyliusClientInterface::class);
$this->shouldHaveType(SyliusClient::class);
$this->shouldImplement(SyliusAdminClientInterface::class);
$this->shouldHaveType(SyliusAdminClient::class);
}

public function it_gets_access_token($authentication)
Expand Down
19 changes: 19 additions & 0 deletions src/Api/Admin/AddressApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Diglin\Sylius\ApiClient\Api\Admin;

use Diglin\Sylius\ApiClient\Client\ResourceClientInterface;
use Webmozart\Assert\Assert;

final class AddressApi implements AddressApiInterface
{
public function __construct(
private ResourceClientInterface $resourceClient,
) {}

public function get($code): array
{
Assert::integer($code);
return $this->resourceClient->getResource('api/v2/admin/addresses/%d', [$code]);
}
}
9 changes: 9 additions & 0 deletions src/Api/Admin/AddressApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Diglin\Sylius\ApiClient\Api\Admin;

use Diglin\Sylius\ApiClient\Api\Operation\GettableResourceInterface;

interface AddressApiInterface extends GettableResourceInterface
{
}
52 changes: 52 additions & 0 deletions src/Api/Admin/AdjustmentApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);

namespace Diglin\Sylius\ApiClient\Api\Admin;

use Diglin\Sylius\ApiClient\Client\ResourceClientInterface;
use Diglin\Sylius\ApiClient\Filter\FilterBuilderInterface;
use Diglin\Sylius\ApiClient\Pagination\PageFactoryInterface;
use Diglin\Sylius\ApiClient\Pagination\PageInterface;
use Diglin\Sylius\ApiClient\Pagination\ResourceCursorFactoryInterface;
use Diglin\Sylius\ApiClient\Pagination\ResourceCursorInterface;
use Diglin\Sylius\ApiClient\Sort\SortBuilderInterface;
use Webmozart\Assert\Assert;

final class AdjustmentApi implements AdjustmentApiInterface
{
public function __construct(
private ResourceClientInterface $resourceClient,
private PageFactoryInterface $pageFactory,
private ResourceCursorFactoryInterface $cursorFactory,
) {}

public function get($code): array
{
Assert::integer($code);
return $this->resourceClient->getResource('api/v2/admin/adjustments/%d', [$code]);
}

public function listPerPage(
$code,
int $limit = 10,
array $queryParameters = [],
FilterBuilderInterface $filterBuilder = null,
SortBuilderInterface $sortBuilder = null
): PageInterface {
Assert::string($code);
$data = $this->resourceClient->getResources('api/v2/admin/order-items/%s/adjustments', [$code]);

return $this->pageFactory->createPage($data);
}

public function all(
$code,
int $pageSize = 10,
array $queryParameters = [],
FilterBuilderInterface $filterBuilder = null,
SortBuilderInterface $sortBuilder = null
): ResourceCursorInterface {
$data = $this->listPerPage($code, $pageSize, $queryParameters, $filterBuilder, $sortBuilder);

return $this->cursorFactory->createCursor($pageSize, $data);
}
}
10 changes: 10 additions & 0 deletions src/Api/Admin/AdjustmentApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Diglin\Sylius\ApiClient\Api\Admin;

use Diglin\Sylius\ApiClient\Api\Operation\GettableResourceInterface;
use Diglin\Sylius\ApiClient\Api\Operation\ListableDoubleResourceInterface;

interface AdjustmentApiInterface extends GettableResourceInterface, ListableDoubleResourceInterface
{
}
Loading

0 comments on commit 48cf2b4

Please sign in to comment.