Skip to content

Commit 722d0e4

Browse files
committed
:octocat:
1 parent 651da63 commit 722d0e4

File tree

3 files changed

+43
-76
lines changed

3 files changed

+43
-76
lines changed

src/CurlClient.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Http\Client\Exception\{NetworkException, RequestException};
1919

2020
class CurlClient implements HTTPClientInterface{
21-
use HTTPRequestTrait;
2221

2322
/**
2423
* @var \chillerlan\HTTP\HTTPOptions
@@ -104,4 +103,47 @@ public function sendRequest(RequestInterface $request):ResponseInterface{
104103

105104
}
106105

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

src/HTTPRequestTrait.php

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

src/MagicAPI/ApiClientInterface.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@
1616

1717
interface ApiClientInterface{
1818

19-
/**
20-
* @param string $endpointMap
21-
*
22-
* @return \chillerlan\HTTP\MagicAPI\ApiClientInterface
23-
* @throws \chillerlan\HTTP\MagicAPI\ApiClientException
24-
*/
25-
public function loadEndpoints(string $endpointMap):ApiClientInterface;
26-
2719
/**
2820
* @param string $name
2921
* @param array $arguments

0 commit comments

Comments
 (0)