5 Commits
15 changed files with 171 additions and 28 deletions

No files matched your search

+8 -5
View File
@@ -28,7 +28,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 +40,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
@@ -96,6 +94,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);
@@ -170,7 +172,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,
+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);
}
} }
+1 -1
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);
} }
+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)
);
}
}
+10 -4
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;
@@ -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;
} }
+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;