-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored the API endpoints and client
- Loading branch information
Showing
120 changed files
with
2,698 additions
and
763 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
spec/Api/AuthenticationApiSpec.php → spec/Api/Legacy/AuthenticationApiSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
Oops, something went wrong.