Skip to content

Commit 4c0bd4b

Browse files
committed
✈️
1 parent 4217d82 commit 4c0bd4b

11 files changed

+183
-164
lines changed

src/CurlClient.php

Lines changed: 0 additions & 151 deletions
This file was deleted.

src/HTTPOptionsTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace chillerlan\HTTP;
1414

15+
use chillerlan\HTTP\Psr18\{ClientException, CurlHandle};
16+
1517
trait HTTPOptionsTrait{
1618

1719
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
*
55
* @filesource ClientException.php
66
* @created 10.09.2018
7-
* @package chillerlan\HTTP
7+
* @package chillerlan\HTTP\Psr18
88
* @author smiley <smiley@chillerlan.net>
99
* @copyright 2018 smiley
1010
* @license MIT
1111
*/
1212

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

1515
use Psr\Http\Client\ClientExceptionInterface;
1616

src/Psr18/CurlClient.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Class HTTPClient
4+
*
5+
* @filesource HTTPClient.php
6+
* @created 27.08.2018
7+
* @package chillerlan\HTTP
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\HTTP\Psr18;
14+
15+
use Psr\Http\Message\{RequestInterface, ResponseInterface};
16+
17+
class CurlClient extends HTTPClientAbstract{
18+
19+
/**
20+
* Sends a PSR-7 request.
21+
*
22+
* @param \Psr\Http\Message\RequestInterface $request
23+
*
24+
* @return \Psr\Http\Message\ResponseInterface
25+
*
26+
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens during processing the request.
27+
* @throws \Exception If processing the request is impossible (eg. bad configuration).
28+
*/
29+
public function sendRequest(RequestInterface $request):ResponseInterface{
30+
/** @var \chillerlan\HTTP\Psr18\CurlHandle $handle */
31+
$handle = new $this->options->curlHandle($request, $this->responseFactory->createResponse(), $this->options);
32+
$handle->init();
33+
34+
curl_exec($handle->curl);
35+
36+
$errno = curl_errno($handle->curl);
37+
38+
if($errno !== CURLE_OK){
39+
$error = curl_error($handle->curl);
40+
41+
$network_errors = [
42+
CURLE_COULDNT_RESOLVE_PROXY,
43+
CURLE_COULDNT_RESOLVE_HOST,
44+
CURLE_COULDNT_CONNECT,
45+
CURLE_OPERATION_TIMEOUTED,
46+
CURLE_SSL_CONNECT_ERROR,
47+
CURLE_GOT_NOTHING,
48+
];
49+
50+
$this->logger->error('cURL error #'.$errno.': '.$error);
51+
52+
if(in_array($errno, $network_errors, true)){
53+
throw new NetworkException($error, $request);
54+
}
55+
56+
throw new RequestException($error, $request);
57+
}
58+
59+
$handle->close();
60+
$handle->response->getBody()->rewind();
61+
62+
return $handle->response;
63+
}
64+
65+
}

src/CurlHandle.php renamed to src/Psr18/CurlHandle.php

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

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

1515
use chillerlan\Settings\SettingsContainerInterface;
1616
use Psr\Http\Message\{RequestInterface, ResponseInterface};
@@ -37,7 +37,7 @@ class CurlHandle{
3737
/**
3838
* @var \chillerlan\HTTP\HTTPOptions
3939
*/
40-
public $options;
40+
protected $options;
4141

4242
/**
4343
* CurlHandle constructor.

src/Psr18/HTTPClientAbstract.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Class HTTPClientAbstract
4+
*
5+
* @filesource HTTPClientAbstract.php
6+
* @created 22.02.2019
7+
* @package chillerlan\HTTP\Psr18
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2019 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\HTTP\Psr18;
14+
15+
use chillerlan\HTTP\{HTTPOptions, Psr7, Psr17};
16+
use chillerlan\HTTP\Psr7\Request;
17+
use chillerlan\HTTP\Psr17\ResponseFactory;
18+
use chillerlan\Settings\SettingsContainerInterface;
19+
use Psr\Http\Message\{ResponseFactoryInterface, ResponseInterface};
20+
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
21+
22+
abstract class HTTPClientAbstract implements HTTPClientInterface, LoggerAwareInterface{
23+
use LoggerAwareTrait;
24+
25+
/**
26+
* @var \chillerlan\HTTP\HTTPOptions
27+
*/
28+
protected $options;
29+
30+
/**
31+
* @var \Psr\Http\Message\RequestFactoryInterface
32+
*/
33+
protected $requestFactory;
34+
35+
/**
36+
* @var \Psr\Http\Message\ResponseFactoryInterface
37+
*/
38+
protected $responseFactory;
39+
40+
/**
41+
* CurlClient constructor.
42+
*
43+
* @param \chillerlan\Settings\SettingsContainerInterface|null $options
44+
* @param \Psr\Http\Message\ResponseFactoryInterface|null $responseFactory
45+
* @param \Psr\Log\LoggerInterface|null $logger
46+
*/
47+
public function __construct(
48+
SettingsContainerInterface $options = null,
49+
ResponseFactoryInterface $responseFactory = null,
50+
LoggerInterface $logger = null
51+
){
52+
$this->options = $options ?? new HTTPOptions;
53+
$this->responseFactory = $responseFactory ?? new ResponseFactory;
54+
$this->logger = $logger ?? new NullLogger;
55+
}
56+
57+
/**
58+
* @todo: files, content-type
59+
*
60+
* @param string $uri
61+
* @param string|null $method
62+
* @param array|null $query
63+
* @param mixed|null $body
64+
* @param array|null $headers
65+
*
66+
* @return \Psr\Http\Message\ResponseInterface
67+
*/
68+
public function request(string $uri, string $method = null, array $query = null, $body = null, array $headers = null):ResponseInterface{
69+
$method = strtoupper($method ?? 'GET');
70+
$headers = Psr7\normalize_request_headers($headers);
71+
$request = new Request($method, Psr7\merge_query($uri, $query ?? []));
72+
73+
if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $body !== null){
74+
75+
if(is_array($body) || is_object($body)){
76+
77+
if(!isset($headers['Content-type'])){
78+
$headers['Content-type'] = 'application/x-www-form-urlencoded';
79+
}
80+
81+
if($headers['Content-type'] === 'application/x-www-form-urlencoded'){
82+
$body = http_build_query($body, '', '&', PHP_QUERY_RFC1738);
83+
}
84+
elseif($headers['Content-type'] === 'application/json'){
85+
$body = json_encode($body);
86+
}
87+
else{
88+
$body = null; // @todo
89+
}
90+
91+
}
92+
93+
$request = $request->withBody(Psr17\create_stream((string)$body));
94+
}
95+
96+
foreach($headers as $header => $value){
97+
$request = $request->withAddedHeader($header, $value);
98+
}
99+
100+
return $this->sendRequest($request);
101+
}
102+
103+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @license MIT
1111
*/
1212

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

1515
use Fig\Http\Message\RequestMethodInterface;
1616
use Psr\Http\Client\ClientInterface;

src/NetworkException.php renamed to src/Psr18/NetworkException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @license MIT
1111
*/
1212

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

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

src/RequestException.php renamed to src/Psr18/RequestException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @license MIT
1111
*/
1212

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

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

0 commit comments

Comments
 (0)