Skip to content

Commit d86fa25

Browse files
committed
1 parent 979ecb0 commit d86fa25

File tree

2 files changed

+68
-42
lines changed

2 files changed

+68
-42
lines changed

src/CurlClient.php

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
namespace chillerlan\HTTP;
1414

1515
use chillerlan\HTTP\Psr17\{RequestFactory, ResponseFactory, StreamFactory};
16-
use chillerlan\HTTP\Psr7;
1716
use chillerlan\Settings\SettingsContainerInterface;
1817
use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, StreamFactoryInterface};
1918
use Http\Client\Exception\{NetworkException, RequestException};
2019

2120
class CurlClient implements HTTPClientInterface{
21+
use HTTPRequestTrait;
2222

2323
/**
2424
* @var \chillerlan\HTTP\HTTPOptions
@@ -104,45 +104,4 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
104104

105105
}
106106

107-
/**
108-
* @param string $uri
109-
* @param string|null $method
110-
* @param array|null $query
111-
* @param mixed|null $body
112-
* @param array|null $headers
113-
*
114-
* @return \Psr\Http\Message\ResponseInterface
115-
*/
116-
public function request(string $uri, string $method = null, array $query = null, $body = null, array $headers = null):ResponseInterface{
117-
$method = strtoupper($method ?? 'GET');
118-
$headers = Psr7\normalize_request_headers($headers);
119-
$request = $this->requestFactory->createRequest($method, Psr7\merge_query($uri, $query ?? []));
120-
121-
if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $body !== null){
122-
123-
if(is_array($body) || is_object($body)){
124-
125-
if(!isset($headers['Content-type'])){
126-
$headers['Content-type'] = 'application/x-www-form-urlencoded';
127-
}
128-
129-
if($headers['Content-type'] === 'application/x-www-form-urlencoded'){
130-
$body = http_build_query($body, '', '&', PHP_QUERY_RFC1738);
131-
}
132-
elseif($headers['Content-type'] === 'application/json'){
133-
$body = json_encode($body);
134-
}
135-
136-
}
137-
138-
$request = $request->withBody($this->streamFactory->createStream((string)$body));
139-
}
140-
141-
foreach($headers as $header => $value){
142-
$request = $request->withAddedHeader($header, $value);
143-
}
144-
145-
return $this->sendRequest($request);
146-
}
147-
148107
}

src/HTTPRequestTrait.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Trait HTTPRequestTrait
4+
*
5+
* @filesource HTTPRequestTrait.php
6+
* @created 02.09.2018
7+
* @package chillerlan\HTTP
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\HTTP;
14+
15+
use Psr\Http\Message\ResponseInterface;
16+
17+
/**
18+
* @property \Psr\Http\Message\RequestFactoryInterface $requestFactory
19+
* @property \Psr\Http\Message\StreamFactoryInterface $streamFactory
20+
* @method sendRequest(\Psr\Http\Message\RequestInterface $request):\Psr\Http\Message\ResponseInterface
21+
*/
22+
trait HTTPRequestTrait{
23+
24+
/**
25+
* @todo: files, content-type
26+
*
27+
* @param string $uri
28+
* @param string|null $method
29+
* @param array|null $query
30+
* @param mixed|null $body
31+
* @param array|null $headers
32+
*
33+
* @return \Psr\Http\Message\ResponseInterface
34+
*/
35+
public function request(string $uri, string $method = null, array $query = null, $body = null, array $headers = null):ResponseInterface{
36+
$method = strtoupper($method ?? 'GET');
37+
$headers = Psr7\normalize_request_headers($headers);
38+
$request = $this->requestFactory->createRequest($method, Psr7\merge_query($uri, $query ?? []));
39+
40+
if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $body !== null){
41+
42+
if(is_array($body) || is_object($body)){
43+
44+
if(!isset($headers['Content-type'])){
45+
$headers['Content-type'] = 'application/x-www-form-urlencoded';
46+
}
47+
48+
if($headers['Content-type'] === 'application/x-www-form-urlencoded'){
49+
$body = http_build_query($body, '', '&', PHP_QUERY_RFC1738);
50+
}
51+
elseif($headers['Content-type'] === 'application/json'){
52+
$body = json_encode($body);
53+
}
54+
55+
}
56+
57+
$request = $request->withBody($this->streamFactory->createStream((string)$body));
58+
}
59+
60+
foreach($headers as $header => $value){
61+
$request = $request->withAddedHeader($header, $value);
62+
}
63+
64+
return $this->sendRequest($request);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)