Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.2.0

- **Breaking:** all classes move from `Api\` to `Jschaedl\ApiClient\`, and the sources from
`src/Api/` to `src/`. See [UPGRADE-0.2.md](UPGRADE-0.2.md).

## 0.1.1

- Allow `webmozart/assert` 2.x (`^1.11||^2.0`).
Expand Down
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ composer require jschaedl/api-client
Runnable examples live in [`examples/`](examples) (Shopware 5/6, MobilePay, a generic request).

```php
use Api\Client;
use Api\Request\Encoder\JsonRequestBodyEncoder;
use Api\Request\Handler\AddHeaderHandler;
use Api\Request\Method;
use Api\Request\Request;
use Api\Response\Decoder\JsonResponseBodyDecoder;
use Jschaedl\ApiClient\Client;
use Jschaedl\ApiClient\Request\Encoder\JsonRequestBodyEncoder;
use Jschaedl\ApiClient\Request\Handler\AddHeaderHandler;
use Jschaedl\ApiClient\Request\Method;
use Jschaedl\ApiClient\Request\Request;
use Jschaedl\ApiClient\Response\Decoder\JsonResponseBodyDecoder;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;

Expand Down Expand Up @@ -110,7 +110,7 @@ PDF download or an HTML error page is never run through a JSON decoder unless yo
### Error handling

By default the client throws on every 4xx/5xx response. All exceptions implement
`Api\Exception\ApiExceptionInterface`:
`Jschaedl\ApiClient\Exception\ApiExceptionInterface`:

| Exception | When | `getCode()` |
| --- | --- | --- |
Expand Down Expand Up @@ -180,8 +180,8 @@ honoured; if it asks for longer than `maxDelay`, the request is not retried. `ma
the whole sequence, waits included.

```php
use Api\RetryClient;
use Api\Retry\GenericRetryStrategy;
use Jschaedl\ApiClient\RetryClient;
use Jschaedl\ApiClient\Retry\GenericRetryStrategy;

$client = new RetryClient(
$client,
Expand All @@ -201,11 +201,11 @@ Pass a PSR-3 `logger` to have every retry logged at info level (and every retry
prevents). Entries name method and path only, never the query string or a body; status code,
retry count and delay are in the context.

Waiting and the deadline go through `Api\Clock\ClockInterface` (`NativeClock` by default). To use
Waiting and the deadline go through `Jschaedl\ApiClient\Clock\ClockInterface` (`NativeClock` by default). To use
a Symfony clock, e.g. the framework's `clock` service, wrap it in `SymfonyClockAdapter`:

```php
use Api\Clock\SymfonyClockAdapter;
use Jschaedl\ApiClient\Clock\SymfonyClockAdapter;

$clock = new SymfonyClockAdapter($symfonyClock); // requires symfony/clock

Expand All @@ -214,7 +214,7 @@ new RetryClient($client, new GenericRetryStrategy(clock: $clock), clock: $clock)

### Testing with Mocks

`Api\Testing\MockClient` answers requests from a queue and records them, which is handy for
`Jschaedl\ApiClient\Testing\MockClient` answers requests from a queue and records them, which is handy for
testing code that uses a client. Error responses are thrown as `ResponseException`, like the real client
does:

Expand All @@ -229,7 +229,7 @@ $client = new MockClient([
self::assertCount(2, $client->requests());
```

`Api\Clock\MockClock` (or Symfony's `MockClock` through `SymfonyClockAdapter`) advances its time on `sleep()` instead of waiting. Pass it to `RetryClient`
`Jschaedl\ApiClient\Clock\MockClock` (or Symfony's `MockClock` through `SymfonyClockAdapter`) advances its time on `sleep()` instead of waiting. Pass it to `RetryClient`
and `GenericRetryStrategy` to test retries without delays; `sleeps()` returns the waits.

To test the `Client` itself without network access, wrap Symfony's `MockHttpClient` in a
Expand All @@ -254,9 +254,10 @@ keys.

See [Testing with Mocks](#testing-with-mocks).

**How do I upgrade from 0.0.1?**
**How do I upgrade?**

See [UPGRADE-0.1.md](UPGRADE-0.1.md).
From 0.1.x see [UPGRADE-0.2.md](UPGRADE-0.2.md) (namespace change), from 0.0.1 first see
[UPGRADE-0.1.md](UPGRADE-0.1.md).

---

Expand Down
30 changes: 30 additions & 0 deletions UPGRADE-0.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Upgrade from 0.1.x to 0.2.0

0.2.0 moves every class from the `Api\` namespace to `Jschaedl\ApiClient\`. Nothing else
changes: class names, sub-namespaces and behaviour stay the same.

| 0.1.x | 0.2.0 |
| --- | --- |
| `Api\Client` | `Jschaedl\ApiClient\Client` |
| `Api\Exception\ClientException` | `Jschaedl\ApiClient\Exception\ClientException` |
| `Api\Retry\GenericRetryStrategy` | `Jschaedl\ApiClient\Retry\GenericRetryStrategy` |
| ... | ... |

`Api\` was too generic for a library: any application or package could claim it as well.

## Migrating

Replace the namespace prefix in your code and config. This rewrites `use` statements (group
`use` included), fully qualified names (`\Api\...`), class names in strings and service IDs in
YAML, and leaves names like `MyApi\` or `App\ApiResource\` alone:

```bash
grep -rlF 'Api\' src tests config \
| xargs perl -pi -e 's/(?<![A-Za-z0-9_])Api\\(?=[A-Z{])/Jschaedl\\ApiClient\\/g'
```

Then check the diff, and search for escaped forms the command does not touch, such as
`Api\\Client` in JSON or double-quoted YAML strings.

Rector is no help here: `RenameNamespaceRector` no longer exists in Rector 2, and
`RenameClassRector` leaves `use` statements unchanged for classes that no longer exist.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"autoload": {
"psr-4": {
"Api\\": "src/Api"
"Jschaedl\\ApiClient\\": "src/"
},
"files": [
"examples/_function.php"
Expand Down
10 changes: 5 additions & 5 deletions examples/generic-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

require_once __DIR__.'/../vendor/autoload.php';

use Api\Request\Encoder\JsonRequestBodyEncoder;
use Api\Request\Request;
use Api\Request\Method;
use Jschaedl\ApiClient\Request\Encoder\JsonRequestBodyEncoder;
use Jschaedl\ApiClient\Request\Request;
use Jschaedl\ApiClient\Request\Method;
use Symfony\Component\HttpClient\Psr18Client;

$psr18HttpClient = new Psr18Client();
$requestBodyEncoder = new JsonRequestBodyEncoder();

$apiClient = new Api\Client(
$apiClient = new Jschaedl\ApiClient\Client(
$psr18HttpClient,
$psr18HttpClient,
$psr18HttpClient,
Expand All @@ -31,6 +31,6 @@

var_dump($response->statusCode());
var_dump($response->body());
} catch (Api\Exception\ApiExceptionInterface $exception) {
} catch (Jschaedl\ApiClient\Exception\ApiExceptionInterface $exception) {
var_dump($exception->getMessage());
}
16 changes: 8 additions & 8 deletions examples/mobilepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

require_once __DIR__.'/../vendor/autoload.php';

use Api\Client;
use Api\Request\Encoder\JsonRequestBodyEncoder;
use Api\Request\Handler\AddHeaderHandler;
use Api\Request\Handler\ChainRequestHandler;
use Api\Request\Method;
use Api\Request\Request;
use Api\Response\Decoder\JsonResponseBodyDecoder;
use Api\Response\ResponseInterface;
use Jschaedl\ApiClient\Client;
use Jschaedl\ApiClient\Request\Encoder\JsonRequestBodyEncoder;
use Jschaedl\ApiClient\Request\Handler\AddHeaderHandler;
use Jschaedl\ApiClient\Request\Handler\ChainRequestHandler;
use Jschaedl\ApiClient\Request\Method;
use Jschaedl\ApiClient\Request\Request;
use Jschaedl\ApiClient\Response\Decoder\JsonResponseBodyDecoder;
use Jschaedl\ApiClient\Response\ResponseInterface;
use Symfony\Component\HttpClient\Psr18Client;

final class GetPaymentPointRequest extends Request
Expand Down
14 changes: 7 additions & 7 deletions examples/shopware5.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
require_once __DIR__.'/../vendor/autoload.php';

$psr18HttpClient = new Symfony\Component\HttpClient\Psr18Client();
$requestBodyEncoder = new Api\Request\Encoder\JsonRequestBodyEncoder();
$responseBodyDecoder = new Api\Response\Decoder\JsonResponseBodyDecoder();
$requestBodyEncoder = new Jschaedl\ApiClient\Request\Encoder\JsonRequestBodyEncoder();
$responseBodyDecoder = new Jschaedl\ApiClient\Response\Decoder\JsonResponseBodyDecoder();

$shopware5AdminApiClient = new Api\Client(
$shopware5AdminApiClient = new Jschaedl\ApiClient\Client(
$psr18HttpClient,
$psr18HttpClient,
$psr18HttpClient,
getEnvVarValue('SHOPWARE5_API_HOST'),
requestHandler: new Api\Request\Handler\ChainRequestHandler([
new Api\Request\Handler\AddHeaderHandler('Accept', 'application/json'),
new Api\Request\Handler\AddHeaderHandler(
requestHandler: new Jschaedl\ApiClient\Request\Handler\ChainRequestHandler([
new Jschaedl\ApiClient\Request\Handler\AddHeaderHandler('Accept', 'application/json'),
new Jschaedl\ApiClient\Request\Handler\AddHeaderHandler(
'Authorization',
'Basic'.base64_encode(sprintf('%s:%s', getEnvVarValue('SHOPWARE5_API_USER'), getEnvVarValue('SHOPWARE5_API_KEY')))
),
Expand All @@ -25,7 +25,7 @@
);

$response = $shopware5AdminApiClient->request(
new \Api\Request\Request(\Api\Request\Method::GET, '/api/categories')
new \Jschaedl\ApiClient\Request\Request(\Jschaedl\ApiClient\Request\Method::GET, '/api/categories')
);

var_dump($response->statusCode());
Expand Down
24 changes: 12 additions & 12 deletions examples/shopware6.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

declare(strict_types=1);

use Api\Client;
use Api\ClientInterface;
use Api\Request\Encoder\JsonRequestBodyEncoder;
use Api\Request\Handler\AccessToken;
use Api\Request\Handler\AccessTokenProviderInterface;
use Api\Request\Handler\AddAccessTokenHandler;
use Api\Request\Handler\AddHeaderHandler;
use Api\Request\Handler\ChainRequestHandler;
use Api\Request\Method;
use Api\Request\Request;
use Api\Response\Decoder\JsonResponseBodyDecoder;
use Api\Response\ResponseInterface;
use Jschaedl\ApiClient\Client;
use Jschaedl\ApiClient\ClientInterface;
use Jschaedl\ApiClient\Request\Encoder\JsonRequestBodyEncoder;
use Jschaedl\ApiClient\Request\Handler\AccessToken;
use Jschaedl\ApiClient\Request\Handler\AccessTokenProviderInterface;
use Jschaedl\ApiClient\Request\Handler\AddAccessTokenHandler;
use Jschaedl\ApiClient\Request\Handler\AddHeaderHandler;
use Jschaedl\ApiClient\Request\Handler\ChainRequestHandler;
use Jschaedl\ApiClient\Request\Method;
use Jschaedl\ApiClient\Request\Request;
use Jschaedl\ApiClient\Response\Decoder\JsonResponseBodyDecoder;
use Jschaedl\ApiClient\Response\ResponseInterface;
use Symfony\Component\HttpClient\Psr18Client;

require_once __DIR__.'/../vendor/autoload.php';
Expand Down
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// bug: rector ends in an error refactoring this class
//__DIR__.'/src/MobilePay/AppPayment/PaymentPoints/PaymentPointState.php',
// bug: rector applies FinalizeClassesWithoutChildrenRector
//__DIR__.'/src/Api/Exception/ClientException.php',
//__DIR__.'/src/Exception/ClientException.php',
]);

$rectorConfig->rules([
Expand Down
26 changes: 13 additions & 13 deletions src/Api/Client.php → src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

declare(strict_types=1);

namespace Api;

use Api\Exception\ResponseException;
use Api\Exception\TransportException;
use Api\Request\BodyEncoderAwareRequestInterface;
use Api\Request\HeadersAwareRequestInterface;
use Api\Request\RequestBodyEncoderInterface;
use Api\Request\RequestHandlerInterface;
use Api\Request\RequestInterface;
use Api\Response\Response;
use Api\Response\ResponseBodyDecoderInterface;
use Api\Response\ResponseHandlerInterface;
use Api\Response\ResponseInterface;
namespace Jschaedl\ApiClient;

use Jschaedl\ApiClient\Exception\ResponseException;
use Jschaedl\ApiClient\Exception\TransportException;
use Jschaedl\ApiClient\Request\BodyEncoderAwareRequestInterface;
use Jschaedl\ApiClient\Request\HeadersAwareRequestInterface;
use Jschaedl\ApiClient\Request\RequestBodyEncoderInterface;
use Jschaedl\ApiClient\Request\RequestHandlerInterface;
use Jschaedl\ApiClient\Request\RequestInterface;
use Jschaedl\ApiClient\Response\Response;
use Jschaedl\ApiClient\Response\ResponseBodyDecoderInterface;
use Jschaedl\ApiClient\Response\ResponseHandlerInterface;
use Jschaedl\ApiClient\Response\ResponseInterface;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface as PsrClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
Expand Down
12 changes: 6 additions & 6 deletions src/Api/ClientInterface.php → src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Api;
namespace Jschaedl\ApiClient;

use Api\Exception\ApiExceptionInterface;
use Api\Exception\ResponseException;
use Api\Exception\TransportException;
use Api\Request\RequestInterface;
use Api\Response\ResponseInterface;
use Jschaedl\ApiClient\Exception\ApiExceptionInterface;
use Jschaedl\ApiClient\Exception\ResponseException;
use Jschaedl\ApiClient\Exception\TransportException;
use Jschaedl\ApiClient\Request\RequestInterface;
use Jschaedl\ApiClient\Response\ResponseInterface;

interface ClientInterface
{
Expand Down
36 changes: 18 additions & 18 deletions src/Api/ClientTest.php → src/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

declare(strict_types=1);

namespace Api;

use Api\Exception\ClientException;
use Api\Exception\DecodingException;
use Api\Exception\ResponseException;
use Api\Exception\ServerException;
use Api\Exception\TransportException;
use Api\Request\Encoder\FormRequestBodyEncoder;
use Api\Request\Encoder\JsonRequestBodyEncoder;
use Api\Request\Handler\AddHeaderHandler;
use Api\Request\Header;
use Api\Request\Method;
use Api\Request\Request;
use Api\Request\RequestHandlerInterface;
use Api\Request\RequestInterface;
use Api\Response\Decoder\JsonResponseBodyDecoder;
use Api\Response\ResponseHandlerInterface;
use Api\Response\ResponseInterface;
namespace Jschaedl\ApiClient;

use Jschaedl\ApiClient\Exception\ClientException;
use Jschaedl\ApiClient\Exception\DecodingException;
use Jschaedl\ApiClient\Exception\ResponseException;
use Jschaedl\ApiClient\Exception\ServerException;
use Jschaedl\ApiClient\Exception\TransportException;
use Jschaedl\ApiClient\Request\Encoder\FormRequestBodyEncoder;
use Jschaedl\ApiClient\Request\Encoder\JsonRequestBodyEncoder;
use Jschaedl\ApiClient\Request\Handler\AddHeaderHandler;
use Jschaedl\ApiClient\Request\Header;
use Jschaedl\ApiClient\Request\Method;
use Jschaedl\ApiClient\Request\Request;
use Jschaedl\ApiClient\Request\RequestHandlerInterface;
use Jschaedl\ApiClient\Request\RequestInterface;
use Jschaedl\ApiClient\Response\Decoder\JsonResponseBodyDecoder;
use Jschaedl\ApiClient\Response\ResponseHandlerInterface;
use Jschaedl\ApiClient\Response\ResponseInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\UsesClass;
Expand Down
16 changes: 8 additions & 8 deletions src/Api/ClientsTest.php → src/ClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

declare(strict_types=1);

namespace Api;
namespace Jschaedl\ApiClient;

use Api\Request\Encoder\JsonRequestBodyEncoder;
use Api\Request\Handler\AddHeaderHandler;
use Api\Request\Handler\ChainRequestHandler;
use Api\Request\Header;
use Api\Request\Method;
use Api\Request\Request;
use Api\Response\Decoder\JsonResponseBodyDecoder;
use GuzzleHttp\Psr7\HttpFactory;
use Jschaedl\ApiClient\Request\Encoder\JsonRequestBodyEncoder;
use Jschaedl\ApiClient\Request\Handler\AddHeaderHandler;
use Jschaedl\ApiClient\Request\Handler\ChainRequestHandler;
use Jschaedl\ApiClient\Request\Header;
use Jschaedl\ApiClient\Request\Method;
use Jschaedl\ApiClient\Request\Request;
use Jschaedl\ApiClient\Response\Decoder\JsonResponseBodyDecoder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Api\Clock;
namespace Jschaedl\ApiClient\Clock;

/**
* A PSR-20 clock that can also wait, so retry delays and deadlines can be tested without sleeping.
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Clock/MockClock.php → src/Clock/MockClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Api\Clock;
namespace Jschaedl\ApiClient\Clock;

/**
* A clock for tests: sleep() advances the time instead of waiting, and every sleep is recorded.
Expand Down
Loading
Loading