-
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.
Added more Admin API endpoints support
- Loading branch information
Showing
61 changed files
with
2,221 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,64 @@ A simple PHP client to use the [Sylius PHP API](https://docs.sylius.com/en/lates | |
|
||
*IMPORTANT:* Documentation is work in progress. | ||
|
||
Matrix compatibility: | ||
Compatibility matrix: | ||
|
||
| Sylius version(s) | API PHP Client version |CI status | | ||
|--------------------|-------------------------|--------------------------------------------------------------------------------------------------------------------------| | ||
| v1.6 | v1.0 || | ||
| v1.7 | v1.0 || | ||
| - | master || | ||
| Sylius version(s) | API PHP Client version | PHP requirements |CI status | | ||
|--------------------|------------------------|------------------|--------------------------------------------------------------------------------------------------------------------------| | ||
| \>= 1.6 <=1.7 | ^1.0 (master) | ^7.3 | | | ||
| 1.8 | no support | | | | ||
| \>= 1.9 | ^2.0 (next) | ^8.0 | | | ||
|
||
Note that our PHP client is backward compatible. | ||
|
||
## Requirements | ||
## Usage for API v2 (Sylius >= 1.9) | ||
|
||
* PHP >= 7.3 | ||
* Composer | ||
In Sylius versions 1.9 and later, you will be using the v2 API, or Unified API. | ||
This APU will expose 2 sections: | ||
* the Shop API, for accessing data from the customer's point of view | ||
* the Admin API, for accessing data from an administrator point of view | ||
|
||
Additionally, you can activate the now deprecated v1 Admin API. | ||
|
||
To create your client, there is a client builder for each API that will take care for | ||
you of the internals and dependency injection. | ||
|
||
### Admin API usage | ||
|
||
```php | ||
<?php | ||
|
||
$builder = new \Diglin\Sylius\ApiClient\SyliusAdminClientBuilder(); | ||
|
||
$client = $builder->buildAuthenticatedByPassword('johndoe', 'password'); | ||
$client->getProductApi()->all(); | ||
``` | ||
|
||
### Store API usage | ||
|
||
```php | ||
<?php | ||
|
||
$builder = new \Diglin\Sylius\ApiClient\SyliusStoreClientBuilder(); | ||
|
||
$client = $builder->buildAuthenticatedByPassword('[email protected]', 'password'); | ||
$client->getProductApi()->all(); | ||
``` | ||
|
||
## Usage for API v1 (Sylius >= 1.6 <=1.7, deprecated after 1.7) | ||
|
||
> NOTE: If you are using Sylius version >= 1.10, you will need to reactivate this API | ||
> following this documentation: https://docs.sylius.com/en/1.10/book/api/introduction.html?highlight=sylius_api | ||
To create your client, there is a client builder that will take care for | ||
you of the internals and dependency injection. | ||
|
||
```php | ||
<?php | ||
|
||
$builder = new \Diglin\Sylius\ApiClient\SyliusLegacyClientBuilder(); | ||
|
||
$client = $builder->buildAuthenticatedByPassword('johndoe', 'password', '<api key>', '<api secret>'); | ||
$client->getProductsApi()->all(); | ||
``` | ||
|
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,79 @@ | ||
<?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 OrderApi implements OrderApiInterface | ||
{ | ||
public function __construct( | ||
private ResourceClientInterface $resourceClient, | ||
private PageFactoryInterface $pageFactory, | ||
private ResourceCursorFactoryInterface $cursorFactory, | ||
) {} | ||
|
||
public function get($code): array | ||
{ | ||
Assert::string($code); | ||
return $this->resourceClient->getResource('api/v2/admin/orders/%s', [$code]); | ||
} | ||
|
||
public function listPerPage( | ||
int $limit = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): PageInterface { | ||
$data = $this->resourceClient->getResources('api/v2/admin/orders', [], $limit, $queryParameters, $filterBuilder, $sortBuilder); | ||
|
||
return $this->pageFactory->createPage($data); | ||
} | ||
|
||
public function all( | ||
int $pageSize = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): ResourceCursorInterface { | ||
$data = $this->listPerPage($pageSize, $queryParameters, $filterBuilder, $sortBuilder); | ||
|
||
return $this->cursorFactory->createCursor($pageSize, $data); | ||
} | ||
|
||
public function cancel(string $code, array $data = []): int | ||
{ | ||
Assert::string($code); | ||
return $this->resourceClient->patchResource('api/v2/admin/orders/%s/cancel', [$code], $data); | ||
} | ||
|
||
public function listPayments( | ||
string $code, | ||
int $pageSize = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): ResourceCursorInterface { | ||
$data = $this->resourceClient->getResources('api/v2/admin/orders/%s/payments', [], $pageSize, $queryParameters, $filterBuilder, $sortBuilder); | ||
|
||
return $this->cursorFactory->createCursor($pageSize, $this->pageFactory->createPage($data)); | ||
} | ||
|
||
public function listShipments( | ||
string $code, | ||
int $pageSize = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): ResourceCursorInterface { | ||
$data = $this->resourceClient->getResources('api/v2/admin/orders/%s/shipments', [], $pageSize, $queryParameters, $filterBuilder, $sortBuilder); | ||
|
||
return $this->cursorFactory->createCursor($pageSize, $this->pageFactory->createPage($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,52 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Diglin\Sylius\ApiClient\Api\Admin; | ||
|
||
use Diglin\Sylius\ApiClient\Api\Operation\GettableResourceInterface; | ||
use Diglin\Sylius\ApiClient\Api\Operation\ListableResourceInterface; | ||
use Diglin\Sylius\ApiClient\Exception\HttpException; | ||
use Diglin\Sylius\ApiClient\Filter\FilterBuilderInterface; | ||
use Diglin\Sylius\ApiClient\Pagination\ResourceCursorInterface; | ||
use Diglin\Sylius\ApiClient\Sort\SortBuilderInterface; | ||
|
||
interface OrderApiInterface extends GettableResourceInterface, ListableResourceInterface | ||
{ | ||
/** | ||
* Cancel an order. | ||
* | ||
* @param string $code Code of the order | ||
* | ||
* @throws HttpException if the request failed | ||
*/ | ||
public function cancel(string $code, array $data = []): int; | ||
|
||
/** | ||
* Lists an order payments. | ||
* | ||
* @param string $code Code of the order | ||
* | ||
* @throws HttpException if the request failed | ||
*/ | ||
public function listPayments( | ||
string $code, | ||
int $pageSize = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): ResourceCursorInterface; | ||
|
||
/** | ||
* Lists an order shipments. | ||
* | ||
* @param string $code Code of the order | ||
* | ||
* @throws HttpException if the request failed | ||
*/ | ||
public function listShipments( | ||
string $code, | ||
int $pageSize = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): ResourceCursorInterface; | ||
} |
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,55 @@ | ||
<?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 PaymentApi implements PaymentApiInterface | ||
{ | ||
public function __construct( | ||
private ResourceClientInterface $resourceClient, | ||
private PageFactoryInterface $pageFactory, | ||
private ResourceCursorFactoryInterface $cursorFactory, | ||
) {} | ||
|
||
public function get($code): array | ||
{ | ||
Assert::string($code); | ||
return $this->resourceClient->getResource('api/v2/admin/payments/%s', [$code]); | ||
} | ||
|
||
public function listPerPage( | ||
int $limit = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): PageInterface { | ||
$data = $this->resourceClient->getResources('api/v2/admin/payments', [], $limit, $queryParameters, $filterBuilder, $sortBuilder); | ||
|
||
return $this->pageFactory->createPage($data); | ||
} | ||
|
||
public function all( | ||
int $pageSize = 10, | ||
array $queryParameters = [], | ||
FilterBuilderInterface $filterBuilder = null, | ||
SortBuilderInterface $sortBuilder = null | ||
): ResourceCursorInterface { | ||
$data = $this->listPerPage($pageSize, $queryParameters, $filterBuilder, $sortBuilder); | ||
|
||
return $this->cursorFactory->createCursor($pageSize, $data); | ||
} | ||
|
||
public function complete(string $code, array $data = []): int | ||
{ | ||
Assert::string($code); | ||
return $this->resourceClient->patchResource('api/v2/admin/payments/%s/complete', [$code], $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,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Diglin\Sylius\ApiClient\Api\Admin; | ||
|
||
use Diglin\Sylius\ApiClient\Api\Operation\GettableResourceInterface; | ||
use Diglin\Sylius\ApiClient\Api\Operation\ListableResourceInterface; | ||
use Diglin\Sylius\ApiClient\Exception\HttpException; | ||
|
||
interface PaymentApiInterface extends GettableResourceInterface, ListableResourceInterface | ||
{ | ||
/** | ||
* Cancel an order. | ||
* | ||
* @param string $code Code of the order | ||
* | ||
* @throws HttpException if the request failed | ||
*/ | ||
public function complete(string $code, array $data = []): int; | ||
} |
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 PaymentMethodApi implements PaymentMethodApiInterface | ||
{ | ||
public function __construct( | ||
private ResourceClientInterface $resourceClient, | ||
) {} | ||
|
||
public function get($code): array | ||
{ | ||
Assert::string($code); | ||
return $this->resourceClient->getResource('api/v2/admin/payment-methods/%s', [$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 declare(strict_types=1); | ||
|
||
namespace Diglin\Sylius\ApiClient\Api\Admin; | ||
|
||
use Diglin\Sylius\ApiClient\Api\Operation\GettableResourceInterface; | ||
|
||
interface PaymentMethodApiInterface extends GettableResourceInterface | ||
{ | ||
} |
Oops, something went wrong.