Skip to content

Commit b8fcf75

Browse files
committed
Initial commit
0 parents  commit b8fcf75

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/vendor/

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "kelunik/php-http-artax",
3+
"type": "library",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Niklas Keller",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"Kelunik\\Http\\Adapter\\Artax\\": "src"
14+
}
15+
},
16+
"require": {
17+
"amphp/artax": "dev-master",
18+
"php-http/httplug": "^1.1",
19+
"php-http/discovery": "^1.2"
20+
},
21+
"require-dev": {
22+
"amphp/phpunit-util": "^1",
23+
"php-http/client-integration-tests": "^0.6.0",
24+
"php-http/message": "^1.6"
25+
},
26+
"provide": {
27+
"php-http/client-implementation": "1.0"
28+
},
29+
"scripts": {
30+
"test": "vendor/bin/phpunit"
31+
}
32+
}

phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true" bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="Artax Adapter Test Suite">
5+
<directory>test</directory>
6+
</testsuite>
7+
</testsuites>
8+
<php>
9+
<server name="TEST_SERVER" value="http://127.0.0.1:10000/server.php" />
10+
</php>
11+
<filter>
12+
<whitelist>
13+
<directory suffix=".php">src/</directory>
14+
</whitelist>
15+
</filter>
16+
<listeners>
17+
<listener class="Amp\PHPUnit\LoopReset" />
18+
</listeners>
19+
</phpunit>

src/Client.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Kelunik\Http\Adapter\Artax;
4+
5+
use Amp\Artax;
6+
use Amp\Promise;
7+
use Http\Client\Exception\RequestException;
8+
use Http\Client\HttpClient;
9+
use Http\Discovery\MessageFactoryDiscovery;
10+
use Http\Discovery\StreamFactoryDiscovery;
11+
use Http\Message\ResponseFactory;
12+
use Http\Message\StreamFactory;
13+
use Psr\Http\Message\RequestInterface;
14+
use function Amp\call;
15+
16+
class Client implements HttpClient {
17+
private $client;
18+
private $responseFactory;
19+
private $streamFactory;
20+
21+
public function __construct(Artax\Client $client = null, ResponseFactory $responseFactory = null, StreamFactory $streamFactory = null) {
22+
$this->client = $client ?? new Artax\DefaultClient;
23+
$this->responseFactory = $responseFactory ?? MessageFactoryDiscovery::find();
24+
$this->streamFactory = $streamFactory ?? StreamFactoryDiscovery::find();
25+
}
26+
27+
/** @inheritdoc */
28+
public function sendRequest(RequestInterface $request) {
29+
return Promise\wait(call(function () use ($request) {
30+
/** @var Artax\Request $req */
31+
$req = new Artax\Request($request->getUri(), $request->getMethod());
32+
$req = $req->withProtocolVersions([$request->getProtocolVersion()]);
33+
$req = $req->withHeaders($request->getHeaders());
34+
$req = $req->withBody((string) $request->getBody());
35+
36+
try {
37+
/** @var Artax\Response $resp */
38+
$resp = yield $this->client->request($req, [
39+
Artax\Client::OP_MAX_REDIRECTS => 0,
40+
]);
41+
} catch (Artax\HttpException $e) {
42+
throw new RequestException($e->getMessage(), $request, $e);
43+
}
44+
45+
$respBody = $resp->getBody();
46+
$bodyStream = $this->streamFactory->createStream();
47+
48+
while (null !== $chunk = yield $respBody->read()) {
49+
$bodyStream->write($chunk);
50+
}
51+
52+
$bodyStream->rewind();
53+
54+
return $this->responseFactory->createResponse(
55+
$resp->getStatus(),
56+
$resp->getReason(),
57+
$resp->getHeaders(),
58+
$bodyStream,
59+
$resp->getProtocolVersion()
60+
);
61+
}));
62+
}
63+
}

test/ClientTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Kelunik\Http\Adapter\Artax\Test;
4+
5+
use Amp\Artax;
6+
use Http\Client\HttpClient;
7+
use Http\Client\Tests\HttpClientTest;
8+
use Kelunik\Http\Adapter\Artax\Client;
9+
10+
class ClientTest extends HttpClientTest {
11+
/** @return HttpClient */
12+
protected function createHttpAdapter() {
13+
$client = new Artax\DefaultClient;
14+
$client->setOption(Artax\Client::OP_TRANSFER_TIMEOUT, 1000);
15+
16+
return new Client($client);
17+
}
18+
19+
/**
20+
* @dataProvider requestProvider
21+
* @group integration
22+
*/
23+
public function testSendRequest($method, $uri, array $headers, $body) {
24+
if ($method === "TRACE") {
25+
$this->markTestSkipped("Currently skipped, because Artax refuses to send bodies for TRACE requests");
26+
}
27+
28+
parent::testSendRequest($method, $uri, $headers, $body);
29+
}
30+
}

0 commit comments

Comments
 (0)