Skip to content

Commit

Permalink
Remove PoolClient, simplify parser
Browse files Browse the repository at this point in the history
This also drops support for header lines delimited with \n instead of \r\n.
  • Loading branch information
kelunik committed Jun 9, 2019
1 parent 7ac3f4d commit ae4d3b3
Show file tree
Hide file tree
Showing 15 changed files with 664 additions and 751 deletions.
11 changes: 11 additions & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ public function request(
Request $request,
CancellationToken $cancellation = null
): Promise;

/**
* Adds a network interceptor.
*
* Whether the given network interceptor will be respected for currently running requests is undefined.
*
* Any new requests have to take the new interceptor into account.
*
* @param NetworkInterceptor $networkInterceptor
*/
public function addNetworkInterceptor(NetworkInterceptor $networkInterceptor): void;
}
6 changes: 4 additions & 2 deletions lib/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Amp\Http\Client;

use Amp\Http\Client\Internal\ApplicationInterceptorClient;
use Amp\Http\Client\Internal\PoolClient;
use Amp\Socket\SocketPool;
use Amp\Socket\UnlimitedSocketPool;

Expand Down Expand Up @@ -34,7 +33,10 @@ public function addApplicationInterceptor(ApplicationInterceptor $applicationInt

public function build(): Client
{
$client = new PoolClient($this->socketPool, ...$this->networkInterceptors);
$client = new SocketClient($this->socketPool);
foreach ($this->networkInterceptors as $networkInterceptor) {
$client->addNetworkInterceptor($networkInterceptor);
}

$applicationInterceptors = $this->applicationInterceptors;
while ($applicationInterceptor = \array_pop($applicationInterceptors)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/Internal/ApplicationInterceptorClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Amp\CancellationToken;
use Amp\Http\Client\ApplicationInterceptor;
use Amp\Http\Client\Client;
use Amp\Http\Client\NetworkInterceptor;
use Amp\Http\Client\Request;
use Amp\NullCancellationToken;
use Amp\Promise;
Expand All @@ -31,4 +32,9 @@ public function request(Request $request, CancellationToken $cancellation = null

return $this->interceptor->interceptApplicationRequest($request, $cancellation, $this->client);
}

public function addNetworkInterceptor(NetworkInterceptor $networkInterceptor): void
{
throw new \RuntimeException('Operation not supported');
}
}
40 changes: 40 additions & 0 deletions lib/Internal/CallableNetworkClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Amp\Http\Client\Internal;

use Amp\CancellationToken;
use Amp\Failure;
use Amp\Http\Client\Client;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\NetworkInterceptor;
use Amp\Http\Client\Request;
use Amp\Promise;
use function Amp\call;

class CallableNetworkClient implements Client
{
private $callable;

public function __construct(callable $callable)
{
$this->callable = $callable;
}

public function request(Request $request, CancellationToken $cancellation = null): Promise
{
$callable = $this->callable;
$this->callable = null;

if ($callable === null) {
return new Failure(new HttpException(__METHOD__ . ' may only be invoked once per instance. '
. 'If you need to implement retries or otherwise issue multiple requests, register an ApplicationInterceptor to do so.'));
}

return call($callable);
}

public function addNetworkInterceptor(NetworkInterceptor $networkInterceptor): void
{
throw new \RuntimeException('Operation not supported');
}
}
5 changes: 5 additions & 0 deletions lib/Internal/NetworkInterceptorClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public function request(Request $request, CancellationToken $cancellation = null

return $interceptor->interceptNetworkRequest($request, $cancellation, $this->connectionInfo, $next);
}

public function addNetworkInterceptor(NetworkInterceptor $networkInterceptor): void
{
throw new \RuntimeException('Operation not supported');
}
}
Loading

0 comments on commit ae4d3b3

Please sign in to comment.