Skip to content

Commit

Permalink
Merge 33fa2c2 into 3ccd318
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jun 16, 2019
2 parents 3ccd318 + 33fa2c2 commit 6b06cd1
Show file tree
Hide file tree
Showing 15 changed files with 486 additions and 370 deletions.
13 changes: 8 additions & 5 deletions examples/1-get-request.php
@@ -1,17 +1,20 @@
<?php

use Amp\Artax\Response;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\Loop;

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

Loop::run(function () use ($argv) {
try {
// Instantiate the HTTP client
$client = new Amp\Artax\DefaultClient;
$client = new SocketClient;

// Make an asynchronous HTTP request
$promise = $client->request($argv[1] ?? 'https://httpbin.org/user-agent');
$promise = $client->request(new Request($argv[1] ?? 'https://httpbin.org/user-agent'));

// Client::request() is asynchronous! It doesn't return a response. Instead, it returns a promise to resolve the
// response at some point in the future when we've received the headers of the response. Here we use yield which
Expand All @@ -30,9 +33,9 @@

// The response body is an instance of Message, which allows buffering or streaming by the consumers choice.
// Simply yielding a Message buffers the complete response body.
$body = yield $response->getBody();
$body = yield $response->getBody()->buffer();
print $body . "\n";
} catch (Amp\Artax\HttpException $error) {
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
echo $error;
Expand Down
12 changes: 7 additions & 5 deletions examples/2-custom-header.php
@@ -1,15 +1,17 @@
<?php

use Amp\Artax\Request;
use Amp\Artax\Response;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\Loop;

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

Loop::run(function () {
try {
// Instantiate the HTTP client
$client = new Amp\Artax\DefaultClient;
$client = new SocketClient;

// Here we create a custom request object instead of simply passing an URL to request().
$request = (new Request('https://httpbin.org/headers'))
Expand Down Expand Up @@ -43,9 +45,9 @@

// The response body is an instance of Message, which allows buffering or streaming by the consumers choice.
// Simply yielding a Message buffers the complete response body.
$body = yield $response->getBody();
$body = yield $response->getBody()->buffer();
print $body . "\n";
} catch (Amp\Artax\HttpException $error) {
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
echo $error;
Expand Down
12 changes: 7 additions & 5 deletions examples/3-post-body.php
@@ -1,15 +1,17 @@
<?php

use Amp\Artax\Request;
use Amp\Artax\Response;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\Loop;

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

Loop::run(function () {
try {
// Instantiate the HTTP client
$client = new Amp\Artax\DefaultClient;
$client = new SocketClient;

// Here we create a custom request object instead of simply passing an URL to request().
// We set the method to POST now and add a request body.
Expand All @@ -36,9 +38,9 @@

// The response body is an instance of Message, which allows buffering or streaming by the consumers choice.
// Simply yielding a Message buffers the complete response body.
$body = yield $response->getBody();
$body = yield $response->getBody()->buffer();
print $body . "\n";
} catch (Amp\Artax\HttpException $error) {
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
echo $error;
Expand Down
14 changes: 8 additions & 6 deletions examples/4-forms.php
@@ -1,16 +1,18 @@
<?php

use Amp\Artax\FormBody;
use Amp\Artax\Request;
use Amp\Artax\Response;
use Amp\Http\Client\FormBody;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\Loop;

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

Loop::run(function () {
try {
// Instantiate the HTTP client
$client = new Amp\Artax\DefaultClient;
$client = new SocketClient;

// Here we create a custom request object instead of simply passing an URL to request().
// We set the method to POST and add a FormBody to submit a form.
Expand Down Expand Up @@ -42,9 +44,9 @@

// The response body is an instance of Message, which allows buffering or streaming by the consumers choice.
// Simply yielding a Message buffers the complete response body.
$body = yield $response->getBody();
$body = yield $response->getBody()->buffer();
print $body . "\n";
} catch (Amp\Artax\HttpException $error) {
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
echo $error;
Expand Down
16 changes: 9 additions & 7 deletions examples/5-streaming-responses.php
@@ -1,7 +1,9 @@
<?php

use Amp\Artax\Client;
use Amp\Artax\Response;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\File\Handle;
use Amp\File\StatCache;
use Amp\Loop;
Expand All @@ -13,12 +15,12 @@
$start = \microtime(1);

// Instantiate the HTTP client
$client = new Amp\Artax\DefaultClient;
$client = new SocketClient;

$request = new Request('http://speed.hetzner.de/100MB.bin');

// Make an asynchronous HTTP request
$promise = $client->request('http://speed.hetzner.de/100MB.bin', [
Client::OP_MAX_BODY_BYTES => 120 * 1024 * 1024
]);
$promise = $client->request($request);

// Client::request() is asynchronous! It doesn't return a response. Instead, it returns a promise to resolve the
// response at some point in the future when we've received the headers of the response. Here we use yield which
Expand Down Expand Up @@ -65,7 +67,7 @@
$size = yield Amp\File\size($path);

print \sprintf("%s has a size of %.2fMB\n", $path, (float) $size / 1024 / 1024);
} catch (Amp\Artax\HttpException $error) {
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
echo $error;
Expand Down
13 changes: 8 additions & 5 deletions examples/6-parallel-requests.php
@@ -1,6 +1,9 @@
<?php

use Amp\Artax\Response;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\Loop;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -13,13 +16,13 @@
];

// Instantiate the HTTP client
$client = new Amp\Artax\DefaultClient;
$client = new SocketClient;

$requestHandler = function (string $uri) use ($client) {
/** @var Response $response */
$response = yield $client->request($uri);
$response = yield $client->request(new Request($uri));

return $response->getBody();
return yield $response->getBody()->buffer();
};

try {
Expand All @@ -34,7 +37,7 @@
foreach ($bodies as $uri => $body) {
print $uri . " - " . \strlen($body) . " bytes" . PHP_EOL;
}
} catch (Amp\Artax\HttpException $error) {
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
echo $error;
Expand Down
11 changes: 6 additions & 5 deletions examples/7-unix-sockets.php
@@ -1,8 +1,9 @@
<?php

use Amp\Artax\HttpSocketPool;
use Amp\Artax\Request;
use Amp\Artax\Response;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\Loop;
use Amp\Socket\StaticSocketPool;

Expand All @@ -12,7 +13,7 @@
try {
// Unix sockets require a socket pool that changes all URLs to a fixed one.
$socketPool = new StaticSocketPool("unix:///var/run/docker.sock");
$client = new Amp\Artax\DefaultClient(null, new HttpSocketPool($socketPool));
$client = new SocketClient($socketPool);

// Artax currently requires a host, so just use a dummy one.
$request = new Request('http://docker/info');
Expand All @@ -30,7 +31,7 @@

$body = yield $response->getBody();
print $body . "\n";
} catch (Amp\Artax\HttpException $error) {
} catch (HttpException $error) {
echo $error;
}
});
11 changes: 6 additions & 5 deletions examples/8-benchmark.php
Expand Up @@ -5,23 +5,24 @@
// Infinite (10 x 100 requests): php examples/8-benchmark.php 0
// Custom (10 x $count requests): php examples/8-benchmark.php $count

use Amp\Artax\Client;
use Amp\Artax\Response;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Http\Client\SocketClient;
use Amp\Loop;
use Amp\TimeoutCancellationToken;
use function Amp\coroutine;

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

$count = (int) ($argv[1] ?? "1000");

Loop::run(function () use ($count) {
$client = new Amp\Artax\DefaultClient;
$client->setOption(Client::OP_TRANSFER_TIMEOUT, 50000);
$client = new SocketClient;

$handler = coroutine(function (int $count) use ($client) {
for ($i = 0; $i < $count; $i++) {
/** @var Response $response */
$response = yield $client->request('http://localhost:1337/');
$response = yield $client->request(new Request('http://localhost:1337/'), new TimeoutCancellationToken(50000));
yield $response->getBody();
}
});
Expand Down
19 changes: 19 additions & 0 deletions lib/Driver/DefaultHttpDriverFactory.php
@@ -0,0 +1,19 @@
<?php

namespace Amp\Http\Client\Driver;

use Amp\Http\Client\ConnectionInfo;
use Amp\Http\Client\Request;

final class DefaultHttpDriverFactory implements HttpDriverFactory
{
public function selectDriver(ConnectionInfo $connectionInfo, Request $request): HttpDriver
{
return new Http1Driver;
}

public function getApplicationLayerProtocols(): array
{
return ['http/1.1'];
}
}

0 comments on commit 6b06cd1

Please sign in to comment.