Skip to content

Commit

Permalink
Merge pull request #29 from picqer/v8
Browse files Browse the repository at this point in the history
V8
  • Loading branch information
robvanaarle authored Oct 12, 2022
2 parents dbe6e63 + 0bf6f6a commit 312de1e
Show file tree
Hide file tree
Showing 215 changed files with 8,859 additions and 6,627 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Main pipeline
on: [push, pull_request]
jobs:
Tests:
runs-on: ubuntu-latest
runs-on: ubuntu-18.04
strategy:
matrix:
php-versions: ['7.4', '8.0', '8.1']
php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
57 changes: 43 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Bol.com Retailer API client for PHP
This is an open source PHP client for the [Bol.com Retailer API](https://api.bol.com/retailer/public/Retailer-API/v6/releasenotes.html) version 6.
This is an open source PHP client for the [Bol.com Retailer API](https://api.bol.com/retailer/public/Retailer-API/v8/releasenotes.html) version 8.8.

## Installation
This project can easily be installed through Composer:

```
composer require picqer/bol-retailer-php-client "^6"
composer require picqer/bol-retailer-php-client "^8"
```

## Usage
Create an instance of the client and authenticate
```php
$client = new \Picqer\BolRetailerV6\Client();
$client = new \Picqer\BolRetailerV8\Client();
$client->authenticate('your-client-id', 'your-client-secret');
```

Expand All @@ -25,32 +25,60 @@ foreach ($reducedOrders as $reducedOrder) {
```

## Exceptions
Methods on the Client may throw Exceptions. All Exceptions have the parent class `Picqer\BolRetailerV6\Exception\Exception`:
Methods on the Client may throw Exceptions. All Exceptions have the parent class `Picqer\BolRetailerV8\Exception\Exception`:
- `ConnectException` is thrown when a problem occurred in the connection (e.g. API server is down or a network issue). You may retry later.
- `ServerException` (extends `ConnectException`) is thrown when a problem occurred on the Server (e.g. 500 Internal Server Error). You may retry later.
- `ResponseException` is thrown when the received response could not be handled (e.g. not of proper format or unexpected type). Retrying will not help, investigation is needed.
- `UnauthorizedException` is thrown when the server responded with 400 Unauthorized (e.g. invalid credentials).
- `RateLimitException` is thrown when the throttling limit has been reached for the API user.
- `Exception` is thrown when an error occurred in the HTTP library that is not covered by the cases above. We aim to map as much as possible to either `ConnectionException` or `ResponseException`.

## Migrate from v4 to v5
If you're migrating from v4 to v5, please have a look at the [Migration guide](https://api.bol.com/retailer/public/Retailer-API/v5/migrationguide/v4-v5/migrationguide.html) to see what has changed.
## Migrate to v8
If you're migrating to v8, please have a look at the official migration guides to find out what has changed:
- [bol.com Retailer API migration guide from v7 to v8](https://api.bol.com/retailer/public/Retailer-API/v8/migrationguide/v7-v8/migrationguide.html)
- [bol.com Retailer API migration guide from v6 to v7](https://api.bol.com/retailer/public/Retailer-API/v7/migrationguide/v6-v7/migrationguide.html)
- [bol.com Retailer API migration guide from v5 to v7](https://api.bol.com/retailer/public/Retailer-API/v7/migrationguide/v5-v7/migrationguide.html)

### Gradual rollout
It's easy to overlook changes when migrating to a new version, which could result in undesired behaviour. You may consider a gradual rollout to minimize impact on your business. You can achieve this by using two versions of the API client in your project and a way to test the new version with a small percentage of requests. To use different versions of this client through Composer, fork this project and use a specific version branch of that new temporary repository as dependency.

For example, if you forked it to `my-namespace/bol-retailer-php-client`, you can add v8 next to your current version with:

```
composer require my-namespace/bol-retailer-php-client "v8.x-dev"
```

You might need to add that temporary repository as [vcs repository in Composer](https://getcomposer.org/doc/05-repositories.md#vcs) for this package to be visible to Composer. When the new version is running stable, remove the old version from your project and delete the fork.

## Version support expectations
As we're require this client in production at Picqer and this will be the case for the foreseeable future, we will make sure that there is always support a version that is either in the GA or Deprecation [lifecycle stage](https://api.bol.com/retailer/public/Retailer-API/release-planning.html) (and not removed). We have thousands of connected partners using many API services and ideally we want to rollout new API versions slowly, so it might happen that we update this library to the latest GA version in the final weeks before removal of the current supported version.

## Contributing
Please follow the guidelines below if you want to contribute.
- Add the latest API specs of the version you want to contribute to and generate the models and client (see: 'Generated Models and Client').
- Sometimes generation fails due to an error or outputs unexpected code. Fix this in the generator class, do not alter generated classes manually.
- If a generator required a change due to a quirk in the Bol.com API specs, please add that case to the 'Known quirks' section of this README. It would be great if you check whether the current known quirks are still relevant.
- If you contribute with a new major version, any references to 'v8' have to be replaced with the new version:
- Rename the namespaces in `/src`, `/tests` and `composer.json`.
- Replace 'v8' with the new version in the test fixtures and in `BaseClient`.
- Update this README with links to the new migration guide(s) and replace 'v8' with the new version.
- Keep in mind that we want to support PHP 7.1 as long as possible.

## Generated Models and Client
All the Models and Client are generated by the supplied [API specifications by Bol](https://api.bol.com/retailer/public/apispec/v5). By generating these this ensures no typos are made, not every operation needs a test and future (minor) updates to the specifications can easily be applied.
The Client and all models are generated by the supplied [Retailer API specifications](https://api.bol.com/retailer/public/apispec/v8) (`src/OpenApi/retailer.json`) and [Shared API specification](https://api.bol.com/retailer/public/apispec/Shared%20API%20-%20v8) (`src/OpenApi/shared.json`). These specifications are merged. Generating the code ensures there are no typos, not every operation needs a test and future (minor) updates to the specifications can easily be applied. To build the classes for the latest Bol Retailer API version, replace the two specification files with the latest version first.

The generated classes contain all data to properly map method arguments and response data to the models: the specifications are only used to generate them.
The generated classes contain all data required to properly map method arguments and response data to the models: the specifications are only used to generate them.

### Client
The Client contains all operations specified in the specifications. The 'operationId' is converted to camelCase and used as method name for each operation.
The Client contains all operations specified in the specifications. The 'operationId' value is converted to camelCase and used as method name for each operation.

The specifications define types for each request and response (if it needs to send data). If a model for such a type encapsulates just one field, that model is abstracted from the operation have a smoother development experience:
- A method in the Client accepts that field as argument instead of the model
- A method in the Client returns the field from that model instead of the model itself

To generate the Client, the following composer script may be used:
```
# Generates Picqer\BolRetailerV6\Client
# Generates Picqer\BolRetailerV8\Client
composer run-script generate-client
```

Expand All @@ -59,13 +87,13 @@ The class names for models are equal to the keys of the array 'definitions' in t

To generate the Models, the following composer script may be used:
```
# Generates all Picqer\BolRetailerV6\Model\* models
# Generates all Picqer\BolRetailerV8\Model\* models
composer run-script generate-models
```

### Known quirks
## Known quirks
- Some type definitions in de specifications are sentences, for example 'Container for the order items that have to be cancelled.'. These are converted to CamelCase and dots are removed.
- Some operations in the specifications have no response type specified, while there is a response. Currently, this is only the case for operations that return CSV.
- Some operations (get-offer-export and get-unpublished-offer-report) in the specifications have no response schema (type) specified, while there is a response. Currently, this is only the case for operations that return CSV.
- There a type 'Return' defined in the specifications. As this is a reserved keyword in PHP, it can't be used as class name for the model (in PHP <= 7), so for now it's replaced with 'ReturnObject'.
- If an array field in a response is empty, the field is (sometimes?) omitted from the response. E.g. the raw JSON response for getOrders is
```
Expand All @@ -77,4 +105,5 @@ composer run-script generate-models
"orders": [ ]
}
```
- Operation 'Get all invoices' is specified to have a string as response, while there is clearly some data model returned in JSON or XML.
- Operation 'get-invoices' is specified to have a string as response, while there is clearly some data model returned in JSON or XML.
- The description of the operation 'get-invoices' contains a weird space marked as 'ENSP'.
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,31 @@
"guzzlehttp/guzzle": "^6.3|^7.0"
},
"require-dev": {
"phpspec/prophecy": "^1.15",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^7|^8|^9",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^7|^8",
"squizlabs/php_codesniffer": "^3.0",
"vimeo/psalm": "^4.27"
"vimeo/psalm": "^3.5"
},
"autoload": {
"psr-4": {
"Picqer\\BolRetailerV6\\": "src"
"Picqer\\BolRetailerV8\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Picqer\\BolRetailerV6\\Tests\\": "tests"
"Picqer\\BolRetailerV8\\Tests\\": "tests"
}
},
"scripts": {
"test": "phpunit",
"check-style": "phpcs src tests",
"fix-style": "phpcbf src tests",
"generate-client": "Picqer\\BolRetailerV6\\OpenApi\\ClientGenerator::run",
"generate-models": "Picqer\\BolRetailerV6\\OpenApi\\ModelGenerator::run"
"generate-client": "Picqer\\BolRetailerV8\\OpenApi\\ClientGenerator::run",
"generate-models": "Picqer\\BolRetailerV8\\OpenApi\\ModelGenerator::run"
},
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
"dev-master": "8.0-dev"
}
},
"config": {
Expand Down
Binary file removed composer.phar
Binary file not shown.
42 changes: 24 additions & 18 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
<?php

namespace Picqer\BolRetailerV6;
namespace Picqer\BolRetailerV8;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ConnectException as GuzzleConnectException;
use Picqer\BolRetailerV6\Exception\RateLimitException;
use Picqer\BolRetailerV6\Exception\ServerException;
use Picqer\BolRetailerV6\Model\AbstractModel;
use Picqer\BolRetailerV6\Exception\AuthenticationException;
use Picqer\BolRetailerV6\Exception\ConnectException;
use Picqer\BolRetailerV6\Exception\Exception;
use Picqer\BolRetailerV6\Exception\ResponseException;
use Picqer\BolRetailerV6\Exception\UnauthorizedException;
use Picqer\BolRetailerV6\OpenApi\ModelCreator;
use Picqer\BolRetailerV8\Exception\RateLimitException;
use Picqer\BolRetailerV8\Exception\ServerException;
use Picqer\BolRetailerV8\Model\AbstractModel;
use Picqer\BolRetailerV8\Exception\AuthenticationException;
use Picqer\BolRetailerV8\Exception\ConnectException;
use Picqer\BolRetailerV8\Exception\Exception;
use Picqer\BolRetailerV8\Exception\ResponseException;
use Picqer\BolRetailerV8\Exception\UnauthorizedException;
use Picqer\BolRetailerV8\OpenApi\ModelCreator;
use Psr\Http\Message\ResponseInterface;

class BaseClient
{
protected const API_TOKEN_URI = 'https://login.bol.com/token';
protected const API_ENDPOINT = 'https://api.bol.com/retailer/';
protected const API_DEMO_ENDPOINT = 'https://api.bol.com/retailer-demo/';
protected const API_CONTENT_TYPE_JSON = 'application/vnd.retailer.v6+json';
protected const API_ENDPOINT = 'https://api.bol.com/';
protected const API_CONTENT_TYPE_JSON = 'application/vnd.retailer.v8+json';

/**
* @var bool Whether request will be sent to the demo endpoint.
Expand Down Expand Up @@ -178,7 +177,7 @@ public function request(string $method, string $url, array $options, array $resp
throw new UnauthorizedException('No or expired token, please authenticate first');
}

$url = $this->getEndpoint() . $url;
$url = $this->getEndpoint($url);

$httpOptions = [];
$httpOptions['headers'] = [
Expand Down Expand Up @@ -226,14 +225,16 @@ public function request(string $method, string $url, array $options, array $resp
/**
* Returns the url of the endpoint, taking demo mode into account.
*
* @param string $url The relative url of the endpoint.
* @return string The url of the endpoint.
*/
protected function getEndpoint(): string
protected function getEndpoint(string $url): string
{
if ($this->isDemoMode) {
return static::API_DEMO_ENDPOINT;
// add '-demo' to the first path item of the url
$url = preg_replace('/^([^\/]+)/', '$1-demo', $url);
}
return static::API_ENDPOINT;
return static::API_ENDPOINT . $url;
}

/**
Expand Down Expand Up @@ -277,7 +278,12 @@ protected function rawRequest(string $method, string $url, array $options = []):
if ($statusCode == 401) {
throw new UnauthorizedException($message, $statusCode);
} if ($statusCode == 429) {
throw new RateLimitException($message, $statusCode);
$retryAfter = null;
if ($response->hasHeader('Retry-After')) {
$retryAfter = (int) $response->getHeader('Retry-After')[0];
}

throw new RateLimitException($message, $statusCode, null, $retryAfter);
} elseif (in_array($statusCode, [500, 502, 503, 504, 507])) {
throw new ServerException($message, $statusCode);
} elseif ($statusCode != 404) {
Expand Down
Loading

0 comments on commit 312de1e

Please sign in to comment.