- WIP on request/response handling
This commit is contained in:
1 parent
5d9c0f0dbf
commit
7e93e57515
10 files changed
+133
-121
No files matched your search
@@ -3,6 +3,7 @@
|
||||
namespace Ulmus\Api\Transport;
|
||||
|
||||
use Ulmus\Api\Common\{ ContentTypeEnum, MethodEnum, HttpHeaderEnum };
|
||||
use Ulmus\Api\Stream\JsonStream;
|
||||
use Ulmus\Api\Stream\Stream;
|
||||
use Psr\Http\Message\{ RequestInterface, ResponseInterface };
|
||||
use Ulmus\Api\Response\{ Response, JsonResponse };
|
||||
@@ -51,7 +52,7 @@ abstract class CurlTransport {
|
||||
|
||||
public function fromRequest(RequestInterface $request) : ResponseInterface
|
||||
{
|
||||
return $this->request(MethodEnum::from($request->getMethod()), $request->getUri(), $request->getBody()->getContents(), $request->getHeaders());
|
||||
return $this->request(MethodEnum::from($request->getMethod()), $request->getUri(), $request->getBody(), $request->getHeaders());
|
||||
}
|
||||
|
||||
public function request(MethodEnum $method, string $url, mixed $data = null, array $headers = [], array $options = []) : ResponseInterface
|
||||
@@ -143,7 +144,17 @@ abstract class CurlTransport {
|
||||
|
||||
switch($this->contentType) {
|
||||
case ContentTypeEnum::Json:
|
||||
$options[CURLOPT_POSTFIELDS] = json_encode(is_string($data) && empty($data) ? new \stdClass() : $data, JsonResponse::DEFAULT_JSON_ENCODING_FLAGS);
|
||||
if ($data instanceof JsonStream) {
|
||||
$json = $data->getContents();
|
||||
}
|
||||
elseif (is_string($data) && empty($data)) {
|
||||
$json = $this->contentType->defaultBody();
|
||||
}
|
||||
else {
|
||||
$json = json_encode($data, JsonStream::DEFAULT_JSON_ENCODING_FLAGS);
|
||||
}
|
||||
|
||||
$options[CURLOPT_POSTFIELDS] = $json;
|
||||
$headers['Accept'] ??= $this->contentType->value;
|
||||
break;
|
||||
|
||||
|
||||
Reference in new issue
Block a user