10 Commits
20 changed files with 234 additions and 57 deletions

No files matched your search

+3 -3
View File
@@ -11,12 +11,12 @@
], ],
"require": { "require": {
"ext-curl": "*", "ext-curl": "*",
"mcnd/ulmus": "dev-master" "mcnd/ulmus": "^1.0 || ^2.0"
}, },
"repositories": [ "repositories": [
{ {
"type": "vcs", "type": "composer",
"url": "https://git.mcnd.ca/mcNdave/ulmus.git" "url": "https://git.mcnd.ca/api/packages/mcndave/composer"
} }
], ],
"autoload": { "autoload": {
+1 -1
View File
@@ -50,7 +50,7 @@ class Rest implements AdapterInterface
return "https"; return "https";
} }
public function writableValue(mixed $value) : mixed public function writableValue(mixed $value) : mixed
{ {
switch (true) { switch (true) {
case $value instanceof \UnitEnum: case $value instanceof \UnitEnum:
+1
View File
@@ -10,4 +10,5 @@ interface ApiHandlerInterface
{ {
public function handleRequest(RequestInterface $request, ApiAction $attribute) : RequestInterface; public function handleRequest(RequestInterface $request, ApiAction $attribute) : RequestInterface;
public function handleResponse(ResponseInterface $response, ApiAction $attribute) : ResponseInterface; public function handleResponse(ResponseInterface $response, ApiAction $attribute) : ResponseInterface;
# public function debugResponse(\Ulmus\Api\Transport\CurlTransport $transport, ApiRepository $repository) : void;
} }
+34 -8
View File
@@ -6,6 +6,7 @@ use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Ulmus\Api\Attribute\Property\Bind;
use Ulmus\Api\Common\MethodEnum; use Ulmus\Api\Common\MethodEnum;
use Ulmus\Api\RequestBuilder\Filter; use Ulmus\Api\RequestBuilder\Filter;
use Ulmus\Api\RequestBuilder\UrlParameter; use Ulmus\Api\RequestBuilder\UrlParameter;
@@ -28,7 +29,7 @@ class ApiRepository extends \Ulmus\Repository
public ResponseInterface $lastResponse; public ResponseInterface $lastResponse;
public function __construct(string $entity, string $alias = self::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) public function __construct(string $entity, string $alias = self::DEFAULT_ALIAS, null|ConnectionAdapter $adapter = null)
{ {
parent::__construct($entity, $alias, $adapter); parent::__construct($entity, $alias, $adapter);
} }
@@ -40,9 +41,7 @@ class ApiRepository extends \Ulmus\Repository
public function loadOneFromAttribute(string $attributeClass): ? object public function loadOneFromAttribute(string $attributeClass): ? object
{ {
$response = $this->executeRequest($attributeClass); return $this->instanciateEntity(null, $this->executeRequest($attributeClass)->getParsedBody());
return $this->instanciateEntity()->fromArray($response->getParsedBody());
} }
public function loadAll() : EntityCollection public function loadAll() : EntityCollection
@@ -62,7 +61,13 @@ class ApiRepository extends \Ulmus\Repository
return $this->loadAllFromAttribute(Attribute\Obj\Api\Search::class); return $this->loadAllFromAttribute(Attribute\Obj\Api\Search::class);
} }
#[\Deprecated]
public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool
{
return $this->create($entity, $replace);
}
public function create(object|array $entity, bool $replace = false): bool
{ {
$response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset()); $response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityGetDataset());
@@ -73,6 +78,17 @@ class ApiRepository extends \Ulmus\Repository
return $response->getStatusCode() === 200; return $response->getStatusCode() === 200;
} }
public function update(object|array $entity): bool
{
foreach($this->entityResolver->searchPropertyAttributeList(Bind::class) as $field => $attribute) {
$this->bindUrl($attribute[0]->name ?? $field, $entity->$field);
}
$response = $this->executeRequest(Attribute\Obj\Api\Update::class, $this->generateWritableDataset($entity));
return $response->getStatusCode() === 200;
}
public function destroy(object $entity) : bool public function destroy(object $entity) : bool
{ {
if ( ! $this->matchEntity($entity) ) { if ( ! $this->matchEntity($entity) ) {
@@ -96,6 +112,10 @@ class ApiRepository extends \Ulmus\Repository
throw new \RuntimeException(sprintf("Could not find attribute class '%s' for class '%s'", $attributeClass, $this->entityClass)); throw new \RuntimeException(sprintf("Could not find attribute class '%s' for class '%s'", $attributeClass, $this->entityClass));
} }
if (method_exists($this->adapter->apiHandler, 'executingRequest')) {
$this->adapter->apiHandler->executingRequest($this, $attribute);
}
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method, $data, $this->adapter->adapter()->getHeaders(), $this->adapter->adapter()->getParameters()); $request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method, $data, $this->adapter->adapter()->getHeaders(), $this->adapter->adapter()->getParameters());
$request = $this->compileRequestParameters($request, $attribute); $request = $this->compileRequestParameters($request, $attribute);
@@ -133,6 +153,7 @@ class ApiRepository extends \Ulmus\Repository
$search = []; $search = [];
foreach($matches as $item) { foreach($matches as $item) {
unset($value);
$default = null; $default = null;
$variable = $item[1]; $variable = $item[1];
@@ -151,12 +172,16 @@ class ApiRepository extends \Ulmus\Repository
if ($default ?? false) { if ($default ?? false) {
$value = $default; $value = $default;
} }
elseif ( strpos($route, "[{$matches[0][0]}]") !== false && $this->enforceExistingArguments) { else {
throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s ) from %s::%s", $route, $variable, json_encode($arguments), $routeParam['class'], $routeParam['classMethod'])); throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments %s", $route, $variable, json_encode($arguments)));
} }
} }
$search[$item[0]] = rawurlencode($value ?? ""); if (is_object($value) && enum_exists($value::class)) {
$value = $value->value;
}
$search[$item[0]] = rawurlencode($value);
} }
$route = str_replace(array_keys($search), array_values($search), $route); $route = str_replace(array_keys($search), array_values($search), $route);
@@ -170,7 +195,8 @@ class ApiRepository extends \Ulmus\Repository
$request = new JsonRequest($uri, $method, $body === null ? Stream::fromTemp() : JsonStream::fromContent($body), $headers); $request = new JsonRequest($uri, $method, $body === null ? Stream::fromTemp() : JsonStream::fromContent($body), $headers);
# Adding parameters # Adding parameters
$request = $request->withQueryParams($queryParameters); $request = $request->withQueryParams($queryParameters)
->withAttribute('repository', $this);
return $request; return $request;
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Ulmus\Api\Attribute\Obj\Api;
use Ulmus\Api\Common\MethodEnum; use Ulmus\Api\Common\MethodEnum;
#[\Attribute] #[\Attribute]
abstract class ApiAction class ApiAction
{ {
public function __construct( public function __construct(
public string $url, public string $url,
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Ulmus\Api\Attribute\Property;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Bind {
public function __construct(
public ? string $name = null,
) {}
}
+3 -1
View File
@@ -5,8 +5,10 @@ namespace Ulmus\Api\Common;
enum ContentTypeEnum : string enum ContentTypeEnum : string
{ {
case Json = "application/json"; case Json = "application/json";
case Multipart = "multipart/form-data"; case Xml = "application/xml";
case RssXml = "application/rss+xml";
case UrlEncoded = "application/x-www-form-urlencoded"; case UrlEncoded = "application/x-www-form-urlencoded";
case Multipart = "multipart/form-data";
public function defaultBody() : string public function defaultBody() : string
{ {
+7
View File
@@ -13,4 +13,11 @@ enum MethodEnum : string
case Post = "POST"; # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST ) case Post = "POST"; # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST )
case Put = "PUT"; # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT ) case Put = "PUT"; # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT )
case Trace = "TRACE"; # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE ) case Trace = "TRACE"; # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE )
public static function generateArray() : array
{
$values = array_map(fn($e) => $e->value, MethodEnum::cases());
return array_combine($values, $values);
}
} }
+15 -15
View File
@@ -24,7 +24,7 @@ class Uri implements UriInterface
protected null|string $fragment; protected null|string $fragment;
public function __construct(string $uri = null) public function __construct(?string $uri = null)
{ {
$this->parse($uri); $this->parse($uri);
} }
@@ -53,7 +53,7 @@ class Uri implements UriInterface
$this->setFragment($parts['fragment'] ?? null); $this->setFragment($parts['fragment'] ?? null);
} }
public function getScheme() public function getScheme(): string
{ {
return $this->scheme; return $this->scheme;
} }
@@ -94,7 +94,7 @@ class Uri implements UriInterface
return ( $info = $this->getUserInfo() ) ? "{$info}@" : ""; return ( $info = $this->getUserInfo() ) ? "{$info}@" : "";
} }
public function getHost() public function getHost(): string
{ {
return $this->host; return $this->host;
} }
@@ -106,7 +106,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function getPort() public function getPort(): ?int
{ {
return $this->port; return $this->port;
} }
@@ -118,7 +118,7 @@ class Uri implements UriInterface
return $port && ! in_array($port, $ignore) ? ":{$port}" : ""; return $port && ! in_array($port, $ignore) ? ":{$port}" : "";
} }
public function getPath() public function getPath(): string
{ {
$path = implode('/', array_map('rawurlencode', explode('/', $this->path))); $path = implode('/', array_map('rawurlencode', explode('/', $this->path)));
@@ -130,7 +130,7 @@ class Uri implements UriInterface
return $this->getPath(); return $this->getPath();
} }
public function getQuery() public function getQuery(): string
{ {
return http_build_query($this->query, "", null, PHP_QUERY_RFC3986); return http_build_query($this->query, "", null, PHP_QUERY_RFC3986);
} }
@@ -142,7 +142,7 @@ class Uri implements UriInterface
return $query ? "?{$query}" : ""; return $query ? "?{$query}" : "";
} }
public function getFragment() public function getFragment() : string
{ {
return rawurlencode($this->fragment ? "#{$this->fragment}" : ""); return rawurlencode($this->fragment ? "#{$this->fragment}" : "");
} }
@@ -152,12 +152,12 @@ class Uri implements UriInterface
return $this->getFragment(); return $this->getFragment();
} }
public function withScheme($scheme) public function withScheme($scheme) : UriInterface
{ {
return (clone $this)->setScheme($scheme); return (clone $this)->setScheme($scheme);
} }
public function withUserInfo($user, $password = null) public function withUserInfo($user, $password = null) : UriInterface
{ {
return (clone $this)->setUserInfo($user, $password); return (clone $this)->setUserInfo($user, $password);
} }
@@ -170,12 +170,12 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withHost($host) public function withHost($host) : UriInterface
{ {
return (clone $this)->setHost($host); return (clone $this)->setHost($host);
} }
public function withPort($port) public function withPort($port) : UriInterface
{ {
return (clone $this)->setPort($port); return (clone $this)->setPort($port);
} }
@@ -187,7 +187,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withPath($path) public function withPath($path) : UriInterface
{ {
return (clone $this)->setPath($path); return (clone $this)->setPath($path);
} }
@@ -199,7 +199,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withQuery($query) public function withQuery(null|array|string $query) : UriInterface
{ {
return $this->setQuery($query); return $this->setQuery($query);
} }
@@ -215,7 +215,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function withFragment($fragment) public function withFragment($fragment) : UriInterface
{ {
return (clone $this)->setFragment($fragment); return (clone $this)->setFragment($fragment);
} }
@@ -227,7 +227,7 @@ class Uri implements UriInterface
return $this; return $this;
} }
public function __toString() public function __toString() : string
{ {
return $this->render(); return $this->render();
} }
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Ulmus\Api\Common;
use Psr\Http\Message\MessageInterface;
use Ulmus\Api\Stream\JsonStream;
use Ulmus\Api\Stream\XmlStream;
# @TODO !
trait XmlMessageTrait
{
public function getParsedBody() : null|array|object
{
return XmlStream::fromXmlEncoded( $this->getBody()->getContents() )->decode() ;
}
public function withParsedBody(mixed $data) : static
{
return (clone $this)->setBody(
XmlStream::fromContent($data)
);
}
}
+15 -9
View File
@@ -22,6 +22,8 @@ class Request extends Message implements ServerRequestInterface
protected array $files = []; protected array $files = [];
protected array $attributes = [];
public function __construct(string|UriInterface $uri, MethodEnum $method, null|StreamInterface $stream = null, array $headers = []) public function __construct(string|UriInterface $uri, MethodEnum $method, null|StreamInterface $stream = null, array $headers = [])
{ {
$this->method = $method; $this->method = $method;
@@ -43,7 +45,7 @@ class Request extends Message implements ServerRequestInterface
return $target ?: '/'; return $target ?: '/';
} }
public function withRequestTarget($requestTarget) public function withRequestTarget($requestTarget) : RequestInterface
{ {
return (clone $this)->setRequestTarget($requestTarget); return (clone $this)->setRequestTarget($requestTarget);
} }
@@ -55,12 +57,12 @@ class Request extends Message implements ServerRequestInterface
return $this; return $this;
} }
public function getMethod() public function getMethod(): string
{ {
return $this->method->value; return $this->method->value;
} }
public function withMethod($method) public function withMethod($method) : RequestInterface
{ {
return (clone $this)->setMethod($method); return (clone $this)->setMethod($method);
} }
@@ -77,7 +79,7 @@ class Request extends Message implements ServerRequestInterface
return $this->uri; return $this->uri;
} }
public function withUri(UriInterface $uri, $preserveHost = false) public function withUri(UriInterface $uri, $preserveHost = false) : RequestInterface
{ {
return (clone $this)->setUri($uri, $preserveHost); return (clone $this)->setUri($uri, $preserveHost);
} }
@@ -93,7 +95,7 @@ class Request extends Message implements ServerRequestInterface
return $this; return $this;
} }
protected function renderHostHeader(UriInterface $fromUri = null) : string protected function renderHostHeader(? UriInterface $fromUri = null) : string
{ {
$fromUri ??= $this->uri; $fromUri ??= $this->uri;
@@ -197,21 +199,25 @@ class Request extends Message implements ServerRequestInterface
public function getAttributes() : array public function getAttributes() : array
{ {
// TODO: Implement getAttributes() method. return $this->attributes;
} }
public function getAttribute($name, $default = null) : mixed public function getAttribute($name, $default = null) : mixed
{ {
// TODO: Implement getAttribute() method. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
} }
public function withAttribute($name, $value) : static public function withAttribute($name, $value) : static
{ {
// TODO: Implement withAttribute() method. $this->attributes[$name] = $value;
return $this;
} }
public function withoutAttribute($name): static public function withoutAttribute($name): static
{ {
// TODO: Implement withoutAttribute() method. unset($this->attributes[$name]);
return $this;
} }
} }
+1 -1
View File
@@ -10,7 +10,7 @@ class Filter extends Fragment
public function add($field, mixed $value) : self public function add($field, mixed $value) : self
{ {
$this->conditionList[$field] = $value; $this->conditionList[(string) $field] = $value;
return $this; return $this;
} }
+3 -3
View File
@@ -20,17 +20,17 @@ class Response extends Message implements ResponseInterface
$this->setHeaders($headers); $this->setHeaders($headers);
} }
public function getStatusCode() /* : int */ public function getStatusCode() : int
{ {
return $this->code; return $this->code;
} }
public function withStatus($code, $reasonPhrase = null)/* : ResponseInterface*/ public function withStatus($code, $reasonPhrase = null) : ResponseInterface
{ {
return (clone $this)->setStatusCode($code, $reasonPhrase); return (clone $this)->setStatusCode($code, $reasonPhrase);
} }
public function getReasonPhrase() /* : string */ public function getReasonPhrase() : string
{ {
return $this->message; return $this->message;
} }
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Ulmus\Api\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Ulmus\Api\Common\XmlMessageTrait;
use Ulmus\Api\Stream\XmlStream;
use Ulmus\Api\Common\StreamOutputInterface;
class XmlResponse extends Response implements StreamOutputInterface
{
use XmlMessageTrait;
public function __construct(null|StreamInterface $stream = null, int $statusCode = 200, array $headers = [])
{
parent::__construct($stream, $statusCode, $headers + [ 'Content-Type' => "application/xml" ]);
}
public static function fromResponse(ResponseInterface $response) : static
{
return new static($response->getBody(), $response->getStatusCode(), $response->getHeaders());
}
public static function createFrom(mixed $content) : ResponseInterface
{
return new static(XmlStream::fromContent($content));
}
}
+8 -4
View File
@@ -13,7 +13,7 @@ use Ulmus\{Api\SearchRequest\Attribute\BindParameter,
class ApiSearchRequest extends SearchRequest implements SearchRequestInterface class ApiSearchRequest extends SearchRequest implements SearchRequestInterface
{ {
use SearchRequestPaginationTrait, SearchRequestFromRequestTrait { use /*SearchRequestPaginationTrait,*/ SearchRequestFromRequestTrait {
parseAttributeMethod as parseAttributeMethodParent; parseAttributeMethod as parseAttributeMethodParent;
} }
@@ -26,14 +26,18 @@ class ApiSearchRequest extends SearchRequest implements SearchRequestInterface
], fn($i) => !is_null($i)) + []; ], fn($i) => !is_null($i)) + [];
} }
protected function parseAttributeMethod(object $attribute, string $field, string $propertyName,): void protected function parseAttributeMethod(object $attribute, string|\Stringable $field, string $propertyName) : void
{ {
switch ($attribute->method) { switch ($attribute->method) {
case SearchMethodEnum::Where:
case SearchMethodEnum::Manual: case SearchMethodEnum::Manual:
if ($attribute instanceof BindParameter) { if ($attribute instanceof BindParameter) {
$this->bindings[$field] = $this->$propertyName; if (isset($this->$propertyName)) {
$this->bindings[(string)$field] = $this->$propertyName;
}
return;
} }
break;
default: default:
$this->parseAttributeMethodParent($attribute, $field, $propertyName); $this->parseAttributeMethodParent($attribute, $field, $propertyName);
@@ -2,15 +2,20 @@
namespace Ulmus\Api\SearchRequest\Attribute; namespace Ulmus\Api\SearchRequest\Attribute;
use Ulmus\SearchRequest\{ SearchMethodEnum, Attribute\SearchParameter }; use Ulmus\SearchRequest\{Attribute\PropertyValueSource,
Attribute\SearchManual,
SearchMethodEnum,
Attribute\SearchParameter};
#[\Attribute(\Attribute::TARGET_PROPERTY)] #[\Attribute(\Attribute::TARGET_PROPERTY)]
class BindParameter extends SearchParameter class BindParameter extends SearchManual
{ {
public function __construct( public function __construct(
public ? string $parameter = null, public null|string|array $parameter = null,
public ? string $field = null, public null|string|\Stringable|array $field = null,
public bool $toggle = false, public bool $toggle = false,
public SearchMethodEnum $method = SearchMethodEnum::Manual, public SearchMethodEnum $method = SearchMethodEnum::Manual,
public string $description = "",
public PropertyValueSource|array $source = PropertyValueSource::RequestAttribute,
) {} ) {}
} }
+2 -2
View File
@@ -107,7 +107,7 @@ class Stream implements StreamInterface
return $this->hasMode('acwx+'); return $this->hasMode('acwx+');
} }
public function write($string) : false|int public function write($string) : int
{ {
if (! $this->isWritable()) { if (! $this->isWritable()) {
throw new \UnexpectedValueException("An error occured while trying to seek an unseekable stream."); throw new \UnexpectedValueException("An error occured while trying to seek an unseekable stream.");
@@ -166,7 +166,7 @@ class Stream implements StreamInterface
return $result; return $result;
} }
public function getMetadata(string $key = null) : mixed public function getMetadata(?string $key = null) : mixed
{ {
$metadata = stream_get_meta_data($this->stream); $metadata = stream_get_meta_data($this->stream);
+49
View File
@@ -0,0 +1,49 @@
<?php
namespace Ulmus\Api\Stream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Ulmus\Api\Common\HttpHeaderEnum;
use Ulmus\Api\Stream\Stream;
use Ulmus\Api\Common\StreamOutputInterface;
# @TODO
class XmlStream extends Stream
{
public function __construct()
{
}
public static function fromContent(mixed $content) : StreamInterface
{
#if ( false === $json = json_encode($content) ) {
# throw new \InvalidArgumentException(sprintf('Json encoding failed with message `%s`.', json_last_error_msg()));
# }
# @TODO !
#return static::fromJsonEncoded($json);
return static::fromXmlEncoded($content);
}
public static function fromXmlEncoded(string $xml) : StreamInterface
{
$obj = static::fromTemp($xml);
$obj->rewind();
return $obj;
}
public function decode(): mixed
{
$content = $this->getContents();
if ($content === "") {
return null;
}
return json_decode(json_encode((array) \simplexml_load_string($content)), true);
}
}
+9
View File
@@ -18,6 +18,10 @@ class CurlAuthorization
public function fromAuthenticationEnum(AuthenticationEnum $auth,) : static public function fromAuthenticationEnum(AuthenticationEnum $auth,) : static
{ {
switch($auth) { switch($auth) {
case AuthenticationEnum::Custom:
$this->customHeader($this->configuration['header'], $this->configuration['secret']);
break;
case AuthenticationEnum::Bearer: case AuthenticationEnum::Bearer:
$this->bearer($this->configuration['token']); $this->bearer($this->configuration['token']);
break; break;
@@ -96,6 +100,11 @@ class CurlAuthorization
$this->userpass(CURLAUTH_NEGOTIATE, $username, $password); $this->userpass(CURLAUTH_NEGOTIATE, $username, $password);
} }
public function customHeader(string $header, #[\SensitiveParameter] string $token) : void
{
$this->headers[$header] = $token;
}
public function oauth2(Oauth2GrantTypeEnum $grant, string $oauthTokenUrl, string $redirectUri, string $clientId, string $clientSecret, string $scope) : void public function oauth2(Oauth2GrantTypeEnum $grant, string $oauthTokenUrl, string $redirectUri, string $clientId, string $clientSecret, string $scope) : void
{ {
$unid = sprintf("oauth_token_%s", md5(serialize($this->configuration))); $unid = sprintf("oauth_token_%s", md5(serialize($this->configuration)));
+11 -5
View File
@@ -6,7 +6,7 @@ use Ulmus\Api\Common\{ ContentTypeEnum, MethodEnum, HttpHeaderEnum };
use Ulmus\Api\Stream\JsonStream; use Ulmus\Api\Stream\JsonStream;
use Ulmus\Api\Stream\Stream; use Ulmus\Api\Stream\Stream;
use Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface}; use Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface};
use Ulmus\Api\Response\{ Response, JsonResponse }; use Ulmus\Api\Response\{Response, JsonResponse, XmlResponse};
abstract class CurlTransport { abstract class CurlTransport {
@@ -37,9 +37,9 @@ abstract class CurlTransport {
return $this->request(MethodEnum::Post, $url, $data, $headers, $curlOptions); return $this->request(MethodEnum::Post, $url, $data, $headers, $curlOptions);
} }
public function get(string $url, mixed $data, array $headers = [], array $curlOptions = []) : mixed public function get(string $url, array $headers = [], array $curlOptions = []) : mixed
{ {
return $this->request(MethodEnum::Get, $url, $data, $headers, $curlOptions); return $this->request(MethodEnum::Get, $url, null, $headers, $curlOptions);
} }
public function delete(string $url, mixed $data, array $headers = [], array $curlOptions = []) : mixed public function delete(string $url, mixed $data, array $headers = [], array $curlOptions = []) : mixed
@@ -130,8 +130,14 @@ abstract class CurlTransport {
$response = $response->withBody( Stream::fromMemory($result) ); $response = $response->withBody( Stream::fromMemory($result) );
if ( $response->hasHeader('Content-Type') && false !== strpos($response->getHeader('Content-Type')[0], 'json') ) { if ($response->hasHeader('Content-Type') ) {
$response = JsonResponse::fromResponse($response); list($mime) = explode(';', $response->getHeader('Content-Type')[0]);
return match(ContentTypeEnum::tryFrom($mime)) {
ContentTypeEnum::Json => JsonResponse::fromResponse($response),
ContentTypeEnum::RssXml, ContentTypeEnum::Xml => XmlResponse::fromResponse($response),
default => $response,
};
} }
return $response; return $response;