Skip to content

Commit

Permalink
Added more Admin API endpoints support
Browse files Browse the repository at this point in the history
  • Loading branch information
gplanchat committed Nov 10, 2021
1 parent dc12154 commit 756ffac
Show file tree
Hide file tree
Showing 61 changed files with 2,221 additions and 65 deletions.
63 changes: 54 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"name": "Diglin (Derivative Work)",
"homepage": "https://www.diglin.com"
},
{
"name": "Gyroscops (Derivative Work)",
"homepage": "https://gyroscops.com"
}
],
"autoload": {
Expand All @@ -25,7 +29,7 @@
}
},
"require": {
"php": ">=7.1",
"php": ">=8.0",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0",
"php-http/httplug": "^1.1 || ^2.0",
Expand Down
79 changes: 79 additions & 0 deletions src/Api/Admin/OrderApi.php
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));
}
}
52 changes: 52 additions & 0 deletions src/Api/Admin/OrderApiInterface.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\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;
}
55 changes: 55 additions & 0 deletions src/Api/Admin/PaymentApi.php
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);
}
}
19 changes: 19 additions & 0 deletions src/Api/Admin/PaymentApiInterface.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\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;
}
19 changes: 19 additions & 0 deletions src/Api/Admin/PaymentMethodApi.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 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]);
}
}
9 changes: 9 additions & 0 deletions src/Api/Admin/PaymentMethodApiInterface.php
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
{
}
Loading

0 comments on commit 756ffac

Please sign in to comment.