Skip to content

Commit 67b9f03

Browse files
committed
🚿
1 parent d091f07 commit 67b9f03

7 files changed

+58
-52
lines changed

examples/curl_multi.php

+37-30
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
// options for both clients
2121
$options = new HTTPOptions;
22-
$options->ca_info = __DIR__.'/cacert.pem';
23-
$options->sleep = 1; // GW2 API limit: 300 requests/minute
22+
$options->ca_info = __DIR__.'/cacert.pem';
23+
$options->sleep = 750000; // GW2 API limit: 300 requests/minute
24+
$options->window_size = 5;
25+
$options->retries = 1;
2426
#$options->user_agent = 'my fancy http client';
2527

2628
$factory = new HTTPFactory;
@@ -47,40 +49,45 @@
4749
// the multi request handler
4850
$handler = new class () implements MultiResponseHandlerInterface{
4951

50-
public function handleResponse(ResponseInterface $response, RequestInterface $request, int $id, array $curl_info):?RequestInterface{
52+
public function handleResponse(
53+
ResponseInterface $response,
54+
RequestInterface $request,
55+
int $id,
56+
array $curl_info,
57+
):RequestInterface|null{
5158

5259
// the API returns either 200 or 206 on OK responses
5360
// https://gitter.im/arenanet/api-cdi?at=5738e2c0ae26c1967f9eb4a0
54-
if(in_array($response->getStatusCode(), [200, 206], true)){
55-
$lang = $response->getHeaderLine('content-language');
56-
57-
// the response body is empty for some reason, we pretend that's fine and exit
58-
if($response->getBody()->getSize() === 0){
59-
return null;
60-
}
61-
62-
try{
63-
$json = MessageUtil::decodeJSON($response);
64-
}
65-
catch(Throwable){
66-
// maybe we didn't properly receive the data? let's try again
67-
return $request;
68-
}
69-
70-
// create a file for each item in the response (ofc you'd rather put this in a DB)
71-
foreach($json as $item){
72-
$file = $lang.'/'.$item->id;
73-
file_put_contents(__DIR__.'/'.$file.'.json', json_encode($item, (JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)));
74-
75-
echo $file.PHP_EOL;
76-
}
77-
78-
// response ok, nothing to return
61+
if(!in_array($response->getStatusCode(), [200, 206], true)){
62+
// return the failed request back to the stack
63+
return $request;
64+
}
65+
66+
// the response body is empty for some reason, we pretend that's fine and exit
67+
if($response->getBody()->getSize() === 0){
7968
return null;
8069
}
8170

82-
// return the failed request back to the stack
83-
return $request;
71+
try{
72+
$json = MessageUtil::decodeJSON($response);
73+
}
74+
catch(Throwable){
75+
// maybe we didn't properly receive the data? let's try again
76+
return $request;
77+
}
78+
79+
$lang = $response->getHeaderLine('content-language');
80+
81+
// create a file for each item in the response (ofc you'd rather put this in a DB)
82+
foreach($json as $item){
83+
$file = $lang.'/'.$item->id;
84+
file_put_contents(__DIR__.'/'.$file.'.json', json_encode($item, (JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)));
85+
86+
echo $file.PHP_EOL;
87+
}
88+
89+
// response ok, nothing to return
90+
return null;
8491
}
8592

8693
};

src/HTTPOptionsTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ trait HTTPOptionsTrait{
7070
protected int $window_size = 5;
7171

7272
/**
73-
* sleep timer (milliseconds) between each fired multi request on startup
73+
* sleep timer (microseconds) between each fired multi request on startup
7474
*/
7575
protected int|null $sleep = null;
7676

tests/CurlClientTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15-
use chillerlan\HTTP\CurlClient;
15+
use chillerlan\HTTP\{CurlClient, RequestException};
1616
use PHPUnit\Framework\Attributes\Group;
1717
use Psr\Http\Client\ClientInterface;
1818

@@ -27,7 +27,7 @@ protected function initClient():ClientInterface{
2727
}
2828

2929
public function testRequestError():void{
30-
$this->expectException(\chillerlan\HTTP\RequestException::class);
30+
$this->expectException(RequestException::class);
3131

3232
$this->http->sendRequest($this->requestFactory->createRequest('GET', ''));
3333
}

tests/CurlHandleTest.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15-
use chillerlan\HTTP\CurlClient;
16-
use chillerlan\HTTP\HTTPOptions;
15+
use chillerlan\HTTP\{CurlClient, HTTPOptions};
1716
use chillerlan\HTTP\Utils\MessageUtil;
18-
use Exception;
1917
use PHPUnit\Framework\Attributes\DataProvider;
2018
use PHPUnit\Framework\Attributes\Group;
2119
use PHPUnit\Framework\TestCase;
2220
use Psr\Http\Client\ClientInterface;
23-
use function str_repeat;
24-
use function strlen;
25-
use function strtolower;
21+
use Exception;
22+
use function str_repeat, strlen, strtolower;
2623

2724
/**
2825
*
@@ -174,7 +171,7 @@ public function testLargeBody():void{
174171

175172
$this::assertSame(strlen($body), (int)$data->headers->{'Content-Length'});
176173
}
177-
catch(Exception $e){
174+
catch(Exception){
178175
// httpbin times out after 10 seconds and will most likely fail to transfer 1MB of data
179176
// so fool the code coverage if that happens, as we're only interested in request creation
180177
$this::assertTrue(true);

tests/CurlMultiClientTest.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15-
use chillerlan\HTTP\{CurlMultiClient, MultiResponseHandlerInterface};
16-
use chillerlan\HTTP\HTTPOptions;
15+
use chillerlan\HTTP\{CurlMultiClient, HTTPOptions, MultiResponseHandlerInterface};
1716
use chillerlan\HTTP\Utils\QueryUtil;
1817
use PHPUnit\Framework\Attributes\Group;
19-
use PHPUnit\Framework\ExpectationFailedException;
20-
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Framework\{ExpectationFailedException, TestCase};
2119
use Psr\Http\Client\ClientExceptionInterface;
2220
use Psr\Http\Message\{RequestInterface, ResponseInterface};
2321
use function array_column, defined, implode, in_array, ksort;
@@ -66,13 +64,18 @@ protected function getRequests():array{
6664
return $requests;
6765
}
6866

69-
protected function getTestResponseHandler():\chillerlan\HTTP\MultiResponseHandlerInterface{
67+
protected function getTestResponseHandler():MultiResponseHandlerInterface{
7068

71-
return new class () implements \chillerlan\HTTP\MultiResponseHandlerInterface{
69+
return new class () implements MultiResponseHandlerInterface{
7270

7371
protected array $responses = [];
7472

75-
public function handleResponse(ResponseInterface $response, RequestInterface $request, int $id, array $curl_info):?RequestInterface{
73+
public function handleResponse(
74+
ResponseInterface $response,
75+
RequestInterface $request,
76+
int $id,
77+
array $curl_info,
78+
):RequestInterface|null{
7679

7780
if(in_array($response->getStatusCode(), [200, 206], true)){
7881
$this->responses[$id]['lang'] = $response->getHeaderLine('content-language');

tests/HTTPOptionsTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15-
use chillerlan\HTTP\CurlHandle;
16-
use chillerlan\HTTP\HTTPOptions;
15+
use chillerlan\HTTP\{CurlHandle, HTTPOptions};
1716
use PHPUnit\Framework\TestCase;
1817
use Psr\Http\Client\ClientExceptionInterface;
1918
use const CURLOPT_CAINFO, CURLOPT_CAPATH, CURLOPT_SSL_VERIFYHOST, CURLOPT_SSL_VERIFYPEER;

tests/LoggingClientTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15+
use chillerlan\HTTP\LoggingClient;
1516
use PHPUnit\Framework\Attributes\Group;
1617
use Psr\Log\AbstractLogger;
1718
use Stringable;
18-
use function date;
19-
use function sprintf;
19+
use function date, sprintf;
2020

2121
/**
2222
*
@@ -33,7 +33,7 @@ public function log($level, string|Stringable $message, array $context = []):voi
3333
}
3434
};
3535
// we'll just wrap the parent's client
36-
$this->http = new \chillerlan\HTTP\LoggingClient($this->http, $logger);
36+
$this->http = new LoggingClient($this->http, $logger);
3737
}
3838

3939
public function testNetworkError():void{

0 commit comments

Comments
 (0)