- Bugfixes, still a WIP, but mostly functionnal now !

This commit is contained in:
Dave Mc Nicoll committed 2023-11-01 11:32:02 -04:00
1 parent 7e93e57515
commit 30f8d319bf
5 files changed
+29 -8

No files matched your search

+17 -2
View File
@@ -5,7 +5,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 Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface};
use Ulmus\Api\Response\{ Response, JsonResponse };
abstract class CurlTransport {
@@ -14,6 +14,8 @@ abstract class CurlTransport {
public int $maximumRedirections = 10;
protected StreamInterface $stderr;
public function __construct(
public array $headers = [],
public array $curlOptions = [],
@@ -21,6 +23,7 @@ abstract class CurlTransport {
public ContentTypeEnum $contentType = ContentTypeEnum::Json,
# Matching Guzzle's great user-agent syntax
public string $userAgent = "ulmus-api/1.0 curl/{curl} php/{php}",
public bool $debug = false,
) {
$this->userAgent = str_replace([ '{curl}', '{php}', ], [ curl_version()['version'] ?? "???", phpversion() ], $this->userAgent);
}
@@ -59,6 +62,11 @@ abstract class CurlTransport {
{
$response = new Response();
if ($this->debug) {
$this->stderr = Stream::fromMemory();
$stderrResource = $this->stderr->detach();
}
$headers = array_merge_recursive(HttpHeaderEnum::normalizeHeaderArray($headers), HttpHeaderEnum::normalizeHeaderArray($this->headers));
$options += $this->curlOptions;
@@ -92,7 +100,10 @@ abstract class CurlTransport {
return strlen($headerLine);
}
];
] + ( $this->debug ? [
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => $stderrResource,
] : [] );
$ch = curl_init();
@@ -107,6 +118,10 @@ abstract class CurlTransport {
throw new \Exception(implode(PHP_EOL, $errors), $errno);
}
if ($this->debug) {
$this->stderr->attach($stderrResource);
}
$response = $response->withBody( Stream::fromMemory($result) );
if ( $response->hasHeader('Content-Type') && false !== strpos($response->getHeader('Content-Type')[0], 'json') ) {