Skip to content

[BETA] Upgrading API for Sylius 1.9 and newer #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
```

14 changes: 10 additions & 4 deletions 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 @@ -20,11 +24,12 @@
},
"autoload-dev": {
"psr-4": {
"spec\\Diglin\\Sylius\\ApiClient\\": "spec/",
"Diglin\\Sylius\\ApiClient\\tests\\": "tests/"
}
},
"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 All @@ -33,12 +38,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
162 changes: 162 additions & 0 deletions spec/Client/LegacyAuthenticatedHttpClientSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?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->getXauthtokenHeader()->willReturn(null);
$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->getXauthtokenHeader()->willReturn(null);
$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->getXauthtokenHeader()->willReturn(null);
$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/Client/ResourceClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function it_returns_a_page_when_requesting_a_list_of_resources(
->willReturn(json_encode($resources))
;

$this->getResources('api/rest/v1/categories', [], 10, ['foo' => 'bar'])->shouldReturn($resources);
$this->getResources('api/rest/v1/categories', [], 10, ['foo' => 'bar', 'with_count' => true])->shouldReturn($resources);
}

public function it_returns_a_list_of_resources_without_limit_and_count(
Expand Down
17 changes: 9 additions & 8 deletions spec/Pagination/PageFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use Diglin\Sylius\ApiClient\Pagination\Page;
use Diglin\Sylius\ApiClient\Pagination\PageFactory;
use Diglin\Sylius\ApiClient\Pagination\PageFactoryInterface;
use Diglin\Sylius\ApiClient\Routing\UriGeneratorInterface;
use PhpSpec\ObjectBehavior;

class PageFactorySpec extends ObjectBehavior
{
public function let(HttpClientInterface $httpClient)
public function let(HttpClientInterface $httpClient, UriGeneratorInterface $uriGenerator)
{
$this->beConstructedWith($httpClient);
$this->beConstructedWith($httpClient, $uriGenerator);
}

public function it_is_initializable()
Expand All @@ -21,7 +22,7 @@ public function it_is_initializable()
$this->shouldImplement(PageFactoryInterface::class);
}

public function it_creates_a_page_with_all_links($httpClient)
public function it_creates_a_page_with_all_links($httpClient, $uriGenerator)
{
$data = [
'_links' => [
Expand Down Expand Up @@ -50,7 +51,7 @@ public function it_creates_a_page_with_all_links($httpClient)
$this->createPage($data)->shouldReturnAnInstanceOf(Page::class);
$this->createPage($data)->shouldBeLike(
new Page(
new PageFactory($httpClient->getWrappedObject()),
new PageFactory($httpClient->getWrappedObject(), $uriGenerator->getWrappedObject()),
$httpClient->getWrappedObject(),
'http://diglin.com/first',
'http://diglin.com/previous',
Expand All @@ -64,7 +65,7 @@ public function it_creates_a_page_with_all_links($httpClient)
);
}

public function it_creates_a_page_without_next_and_previous_links($httpClient)
public function it_creates_a_page_without_next_and_previous_links($httpClient, $uriGenerator)
{
$data = [
'_links' => [
Expand All @@ -87,7 +88,7 @@ public function it_creates_a_page_without_next_and_previous_links($httpClient)
$this->createPage($data)->shouldReturnAnInstanceOf(Page::class);
$this->createPage($data)->shouldBeLike(
new Page(
new PageFactory($httpClient->getWrappedObject()),
new PageFactory($httpClient->getWrappedObject(), $uriGenerator->getWrappedObject()),
$httpClient->getWrappedObject(),
'http://diglin.com/first',
null,
Expand All @@ -101,7 +102,7 @@ public function it_creates_a_page_without_next_and_previous_links($httpClient)
);
}

public function it_creates_a_page_without_count($httpClient)
public function it_creates_a_page_without_count($httpClient, $uriGenerator)
{
$data = [
'_links' => [
Expand Down Expand Up @@ -129,7 +130,7 @@ public function it_creates_a_page_without_count($httpClient)
$this->createPage($data)->shouldReturnAnInstanceOf(Page::class);
$this->createPage($data)->shouldBeLike(
new Page(
new PageFactory($httpClient->getWrappedObject()),
new PageFactory($httpClient->getWrappedObject(), $uriGenerator->getWrappedObject()),
$httpClient->getWrappedObject(),
'http://diglin.com/first',
'http://diglin.com/previous',
Expand Down
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')
;
}
}
Loading