Skip to content

Commit 4bbfbe0

Browse files
committed
🛀 moved http client
1 parent c2ac6fa commit 4bbfbe0

27 files changed

+113
-114
lines changed

examples/curl_multi.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,26 @@
1010
* @license MIT
1111
*/
1212

13+
use chillerlan\HTTP\{CurlClient , CurlMultiClient, HTTPOptions, MultiResponseHandlerInterface};
14+
use chillerlan\HTTP\Psr7\HTTPFactory;
1315
use chillerlan\HTTP\Utils\{MessageUtil, QueryUtil};
14-
use chillerlan\HTTP\Common\{CurlMultiClient, MultiResponseHandlerInterface};
15-
use chillerlan\HTTP\HTTPOptions;
16-
use chillerlan\HTTP\Psr18\CurlClient;
17-
use chillerlan\HTTP\Psr7\Request;
1816
use Psr\Http\Message\{RequestInterface, ResponseInterface};
1917

2018
require_once __DIR__.'/../vendor/autoload.php';
2119

2220
// options for both clients
2321
$options = new HTTPOptions;
2422
$options->ca_info = __DIR__.'/cacert.pem';
25-
$options->sleep = (60 / 300 * 1000000); // GW2 API limit: 300 requests/minute
23+
$options->sleep = 1; // GW2 API limit: 300 requests/minute
2624
#$options->user_agent = 'my fancy http client';
2725

28-
$client = new CurlClient($options);
26+
$factory = new HTTPFactory;
27+
$client = new CurlClient($factory, $options);
2928

3029
$endpoint = 'https://api.guildwars2.com/v2/items';
3130
$languages = ['de', 'en', 'es'];//, 'fr', 'zh'
3231
// request the list of item ids
33-
$itemResponse = $client->sendRequest(new Request('GET', $endpoint));
32+
$itemResponse = $client->sendRequest($factory->createRequest('GET', $endpoint));
3433

3534
if($itemResponse->getStatusCode() !== 200){
3635
exit('/v2/items fetch error');
@@ -63,7 +62,7 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
6362
try{
6463
$json = MessageUtil::decodeJSON($response);
6564
}
66-
catch(Throwable $e){
65+
catch(Throwable){
6766
// maybe we didn't properly receive the data? let's try again
6867
return $request;
6968
}
@@ -73,7 +72,7 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
7372
$file = $lang.'/'.$item->id;
7473
file_put_contents(__DIR__.'/'.$file.'.json', json_encode($item, (JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)));
7574

76-
# echo $file.PHP_EOL;
75+
echo $file.PHP_EOL;
7776
}
7877

7978
// response ok, nothing to return
@@ -86,12 +85,12 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
8685

8786
};
8887

89-
$multiClient = new CurlMultiClient($handler, $options);
88+
$multiClient = new CurlMultiClient($handler, $factory, $options);
9089

9190
// chunk the item response into arrays of 200 ids each (API limit) and create Request objects for each desired language
9291
foreach(array_chunk(MessageUtil::decodeJSON($itemResponse), 200) as $chunk){
9392
foreach($languages as $lang){
94-
$multiClient->addRequest(new Request('GET', $endpoint.'?'.QueryUtil::build(['lang' => $lang, 'ids' => implode(',', $chunk)])));
93+
$multiClient->addRequest($factory->createRequest('GET', $endpoint.'?'.QueryUtil::build(['lang' => $lang, 'ids' => implode(',', $chunk)])));
9594
}
9695
}
9796

examples/spotify.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
* @license MIT
99
*/
1010

11-
use chillerlan\HTTP\HTTPOptions;
12-
use chillerlan\HTTP\Psr18\CurlClient;
11+
use chillerlan\HTTP\{CurlClient, HTTPOptions};
12+
use chillerlan\HTTP\Psr7\HTTPFactory;
1313
use chillerlan\HTTP\Utils\MessageUtil;
14-
use chillerlan\HTTP\Psr7\{Request, Uri};
1514

1615
require_once __DIR__.'/../vendor/autoload.php';
1716

@@ -24,17 +23,18 @@
2423
$options->ca_info = __DIR__.'/cacert.pem';
2524
$options->user_agent = 'Mozilla/5.0 (Windows NT 6.6.6; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0';
2625

27-
$http = new CurlClient($options);
26+
$factory = new HTTPFactory;
27+
$http = new CurlClient($factory, $options);
2828

29-
$tokenRequest = new Request('GET', new Uri('https://open.spotify.com/get_access_token'));
29+
$tokenRequest = $factory->createRequest('GET', $factory->createUri('https://open.spotify.com/get_access_token'));
3030
$tokenResponse = $http->sendRequest($tokenRequest);
3131

3232
if($tokenResponse->getStatusCode() !== 200){
3333
throw new RuntimeException('could not obtain token');
3434
}
3535

3636
$token = MessageUtil::decodeJSON($tokenResponse);
37-
$queryUri = new Uri('https://api-partner.spotify.com/pathfinder/v1/query');
37+
$queryUri = $factory->createUri('https://api-partner.spotify.com/pathfinder/v1/query');
3838
$params = [
3939
'operationName' => 'queryArtistOverview',
4040
'extensions' => json_encode(['persistedQuery' => ['version' => 1, 'sha256Hash' => $sha256Hash]]),
@@ -43,7 +43,7 @@
4343
foreach($artists as $artistID){
4444
$params['variables'] = json_encode(['uri' => sprintf('spotify:artist:%s', $artistID)]);
4545

46-
$request = new Request('GET', $queryUri->withQuery(http_build_query($params)));
46+
$request = $factory->createRequest('GET', $queryUri->withQuery(http_build_query($params)));
4747
$request = $request->withHeader('Authorization', sprintf('Bearer %s', $token->accessToken));
4848
/** @phan-suppress-next-line PhanTypeMismatchArgumentSuperType */
4949
$response = $http->sendRequest($request);

src/Psr18/ClientException.php renamed to src/ClientException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace chillerlan\HTTP\Psr18;
13+
namespace chillerlan\HTTP;
1414

1515
use Exception;
1616
use Psr\Http\Client\ClientExceptionInterface;

src/Psr18/CurlClient.php renamed to src/CurlClient.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace chillerlan\HTTP\Psr18;
13+
namespace chillerlan\HTTP;
1414

15-
use chillerlan\HTTP\Common\CurlHandle;
1615
use Psr\Http\Message\{RequestInterface, ResponseInterface};
1716
use function in_array, sprintf;
1817
use const CURLE_OK;

src/Common/CurlHandle.php renamed to src/CurlHandle.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace chillerlan\HTTP\Common;
13+
namespace chillerlan\HTTP;
1414

15-
use chillerlan\HTTP\HTTPOptions;
16-
use chillerlan\HTTP\Psr18\ClientException;
1715
use chillerlan\Settings\SettingsContainerInterface;
1816
use Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface};
1917
use CurlHandle as CH;
@@ -278,7 +276,7 @@ protected function setHeaderOptions():void{
278276
}
279277

280278
/**
281-
* @throws \chillerlan\HTTP\Psr18\ClientException
279+
* @throws \chillerlan\HTTP\ClientException
282280
*/
283281
public function setRequestOptions():void{
284282
$method = $this->request->getMethod();

src/Common/CurlMultiClient.php renamed to src/CurlMultiClient.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace chillerlan\HTTP\Common;
13+
namespace chillerlan\HTTP;
1414

15-
use chillerlan\HTTP\Psr18\ClientException;
16-
use chillerlan\HTTP\HTTPOptions;
1715
use chillerlan\Settings\SettingsContainerInterface;
1816
use Psr\Http\Message\{RequestInterface, ResponseFactoryInterface};
1917
use Psr\Log\{LoggerAwareInterface, LoggerInterface, NullLogger};
@@ -38,7 +36,7 @@ class CurlMultiClient implements LoggerAwareInterface{
3836
/**
3937
* the stack of running handles
4038
*
41-
* @var \chillerlan\HTTP\Common\CurlMultiHandle[]
39+
* @var \chillerlan\HTTP\CurlMultiHandle[]
4240
*/
4341
protected array $handles = [];
4442

@@ -47,8 +45,8 @@ class CurlMultiClient implements LoggerAwareInterface{
4745
*/
4846
public function __construct(
4947
protected MultiResponseHandlerInterface $multiResponseHandler,
48+
protected ResponseFactoryInterface $responseFactory,
5049
protected HTTPOptions|SettingsContainerInterface $options = new HTTPOptions,
51-
protected ResponseFactoryInterface $responseFactory = new HTTPFactory,
5250
protected LoggerInterface $logger = new NullLogger,
5351
){
5452
$this->curl_multi = curl_multi_init();

src/Common/CurlMultiHandle.php renamed to src/CurlMultiHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace chillerlan\HTTP\Common;
13+
namespace chillerlan\HTTP;
1414

1515
use chillerlan\Settings\SettingsContainerInterface;
1616
use Psr\Http\Message\{RequestInterface, ResponseInterface};

src/Psr18/HTTPClientAbstract.php renamed to src/HTTPClientAbstract.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace chillerlan\HTTP\Psr18;
13+
namespace chillerlan\HTTP;
1414

15-
use chillerlan\HTTP\Common\HTTPFactory;
16-
use chillerlan\HTTP\HTTPOptions;
1715
use chillerlan\Settings\SettingsContainerInterface;
1816
use Psr\Http\Message\ResponseFactoryInterface;
1917
use Psr\Http\Message\StreamFactoryInterface;
@@ -30,8 +28,8 @@ abstract class HTTPClientAbstract implements HTTPClientInterface{
3028
* HTTPClientAbstract constructor.
3129
*/
3230
public function __construct(
31+
protected ResponseFactoryInterface $responseFactory,
3332
protected HTTPOptions|SettingsContainerInterface $options = new HTTPOptions,
34-
protected ResponseFactoryInterface $responseFactory = new HTTPFactory,
3533
protected LoggerInterface $logger = new NullLogger,
3634
){
3735

src/Psr18/HTTPClientInterface.php renamed to src/HTTPClientInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010

1111
declare(strict_types=1);
1212

13-
namespace chillerlan\HTTP\Psr18;
13+
namespace chillerlan\HTTP;
1414

15-
use Fig\Http\Message\RequestMethodInterface;
1615
use Psr\Http\Client\ClientInterface;
1716
use Psr\Http\Message\{ResponseFactoryInterface, StreamFactoryInterface};
1817
use Psr\Log\LoggerInterface;
1918

2019
/**
2120
*
2221
*/
23-
interface HTTPClientInterface extends ClientInterface, RequestMethodInterface{
22+
interface HTTPClientInterface extends ClientInterface{
2423

2524
/**
2625
* Sets a PSR-3 Logger

src/HTTPOptionsTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
namespace chillerlan\HTTP;
1616

17-
use chillerlan\HTTP\Psr18\ClientException;
1817
use function file_exists, ini_get, is_dir, is_file, is_link, readlink, trim;
1918
use const CURLOPT_CAINFO, CURLOPT_CAPATH;
2019

0 commit comments

Comments
 (0)