Skip to content

Commit 9258b84

Browse files
committed
Middleware 4.0
1 parent 98e8f76 commit 9258b84

8 files changed

+198
-56
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"require": {
1111
"php": "^7.0",
1212
"api-clients/json": "^1.0",
13-
"api-clients/middleware": "^2.0",
13+
"api-clients/middleware": "^4.0",
1414
"clue/buzz-react": "^1.1",
1515
"psr/http-message": "^1.0",
1616
"ringcentral/psr7": "^1.2",

composer.lock

Lines changed: 171 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AcceptJsonMiddleware.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ class AcceptJsonMiddleware implements MiddlewareInterface
1414
{
1515
use PostTrait;
1616
use ErrorTrait;
17-
use DefaultPriorityTrait;
1817

1918
/**
2019
* @param RequestInterface $request
2120
* @param array $options
2221
* @return CancellablePromiseInterface
2322
*/
24-
public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
25-
{
23+
public function pre(
24+
RequestInterface $request,
25+
string $transactionId,
26+
array $options = []
27+
): CancellablePromiseInterface {
2628
return resolve($request->withAddedHeader('Accept', 'application/json'));
2729
}
2830
}

src/JsonDecodeMiddleware.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiClients\Middleware\Json;
44

5+
use ApiClients\Foundation\Middleware\Annotation\First;
56
use ApiClients\Foundation\Middleware\ErrorTrait;
67
use ApiClients\Foundation\Middleware\MiddlewareInterface;
78
use ApiClients\Foundation\Middleware\PreTrait;
@@ -31,21 +32,18 @@ public function __construct(JsonDecodeService $jsonDecodeService)
3132
$this->jsonDecodeService = $jsonDecodeService;
3233
}
3334

34-
/**
35-
* @return int
36-
*/
37-
public function priority(): int
38-
{
39-
return Priority::FIRST;
40-
}
41-
4235
/**
4336
* @param ResponseInterface $response
4437
* @param array $options
4538
* @return CancellablePromiseInterface
39+
*
40+
* @First()
4641
*/
47-
public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface
48-
{
42+
public function post(
43+
ResponseInterface $response,
44+
string $transactionId,
45+
array $options = []
46+
): CancellablePromiseInterface {
4947
if ($response->getBody() instanceof ReadableStreamInterface) {
5048
return resolve($response);
5149
}

src/JsonEncodeMiddleware.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiClients\Middleware\Json;
44

5+
use ApiClients\Foundation\Middleware\Annotation\First;
56
use ApiClients\Foundation\Middleware\ErrorTrait;
67
use ApiClients\Foundation\Middleware\MiddlewareInterface;
78
use ApiClients\Foundation\Middleware\PostTrait;
@@ -30,21 +31,18 @@ public function __construct(JsonEncodeService $jsonEncodeService)
3031
$this->jsonEncodeService = $jsonEncodeService;
3132
}
3233

33-
/**
34-
* @return int
35-
*/
36-
public function priority(): int
37-
{
38-
return Priority::FIRST;
39-
}
40-
4134
/**
4235
* @param RequestInterface $request
4336
* @param array $options
4437
* @return CancellablePromiseInterface
38+
*
39+
* @First()
4540
*/
46-
public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
47-
{
41+
public function pre(
42+
RequestInterface $request,
43+
string $transactionId,
44+
array $options = []
45+
): CancellablePromiseInterface {
4846
if (!($request->getBody() instanceof JsonStream)) {
4947
return resolve($request);
5048
}

tests/AcceptJsonMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testPre()
1515
$middleware = new AcceptJsonMiddleware();
1616
$request = new Request('GET', 'https://example.com', [], '');
1717

18-
$modifiedRequest = await($middleware->pre($request), Factory::create());
18+
$modifiedRequest = await($middleware->pre($request, 'abc'), Factory::create());
1919
self::assertSame(
2020
[
2121
'Host' => ['example.com'],

tests/JsonDecodeMiddlewareTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testPost()
2222
$response = new Response(200, [], '[]');
2323

2424
$body = await(
25-
$middleware->post($response),
25+
$middleware->post($response, 'abc'),
2626
$loop
2727
)->getBody();
2828

@@ -44,17 +44,9 @@ public function testPostNoJson()
4444
self::assertSame(
4545
$response,
4646
await(
47-
$middleware->post($response),
47+
$middleware->post($response, 'abc'),
4848
$loop
4949
)
5050
);
5151
}
52-
53-
public function testPriority()
54-
{
55-
$loop = Factory::create();
56-
$service = new JsonDecodeService($loop);
57-
$middleware = new JsonDecodeMiddleware($service);
58-
self::assertSame(1000, $middleware->priority());
59-
}
6052
}

0 commit comments

Comments
 (0)