Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
042ff5cc2f | ||
|
|
fe56a1727b | ||
|
|
b1adf5a086 | ||
|
|
0550036fd9 |
No files matched your search
@@ -28,7 +28,7 @@ class ApiRepository extends \Ulmus\Repository
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -40,9 +40,7 @@ class ApiRepository extends \Ulmus\Repository
|
||||
|
||||
public function loadOneFromAttribute(string $attributeClass): ? object
|
||||
{
|
||||
$response = $this->executeRequest($attributeClass);
|
||||
|
||||
return $this->instanciateEntity()->fromArray($response->getParsedBody());
|
||||
return $this->instanciateEntity(null, $this->executeRequest($attributeClass)->getParsedBody());
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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->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);
|
||||
|
||||
# Adding parameters
|
||||
$request = $request->withQueryParams($queryParameters);
|
||||
$request = $request->withQueryParams($queryParameters)
|
||||
->withAttribute('repository', $this);
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ namespace Ulmus\Api\Common;
|
||||
enum ContentTypeEnum : string
|
||||
{
|
||||
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 Multipart = "multipart/form-data";
|
||||
|
||||
public function defaultBody() : string
|
||||
{
|
||||
|
||||
@@ -13,4 +13,11 @@ enum MethodEnum : string
|
||||
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 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
@@ -24,7 +24,7 @@ class Uri implements UriInterface
|
||||
|
||||
protected null|string $fragment;
|
||||
|
||||
public function __construct(string $uri = null)
|
||||
public function __construct(?string $uri = null)
|
||||
{
|
||||
$this->parse($uri);
|
||||
}
|
||||
|
||||
@@ -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
@@ -22,6 +22,8 @@ class Request extends Message implements ServerRequestInterface
|
||||
|
||||
protected array $files = [];
|
||||
|
||||
protected array $attributes = [];
|
||||
|
||||
public function __construct(string|UriInterface $uri, MethodEnum $method, null|StreamInterface $stream = null, array $headers = [])
|
||||
{
|
||||
$this->method = $method;
|
||||
@@ -197,21 +199,25 @@ class Request extends Message implements ServerRequestInterface
|
||||
|
||||
public function getAttributes() : array
|
||||
{
|
||||
// TODO: Implement getAttributes() method.
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
// TODO: Implement withAttribute() method.
|
||||
$this->attributes[$name] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withoutAttribute($name): static
|
||||
{
|
||||
// TODO: Implement withoutAttribute() method.
|
||||
unset($this->attributes[$name]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ class Filter extends Fragment
|
||||
|
||||
public function add($field, mixed $value) : self
|
||||
{
|
||||
$this->conditionList[$field] = $value;
|
||||
$this->conditionList[(string) $field] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ use Ulmus\{Api\SearchRequest\Attribute\BindParameter,
|
||||
|
||||
class ApiSearchRequest extends SearchRequest implements SearchRequestInterface
|
||||
{
|
||||
use SearchRequestPaginationTrait, SearchRequestFromRequestTrait {
|
||||
use /*SearchRequestPaginationTrait,*/ SearchRequestFromRequestTrait {
|
||||
parseAttributeMethod as parseAttributeMethodParent;
|
||||
}
|
||||
|
||||
@@ -26,14 +26,18 @@ class ApiSearchRequest extends SearchRequest implements SearchRequestInterface
|
||||
], 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) {
|
||||
case SearchMethodEnum::Where:
|
||||
case SearchMethodEnum::Manual:
|
||||
if ($attribute instanceof BindParameter) {
|
||||
$this->bindings[$field] = $this->$propertyName;
|
||||
if (isset($this->$propertyName)) {
|
||||
$this->bindings[(string)$field] = $this->$propertyName;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->parseAttributeMethodParent($attribute, $field, $propertyName);
|
||||
|
||||
@@ -2,15 +2,20 @@
|
||||
|
||||
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)]
|
||||
class BindParameter extends SearchParameter
|
||||
class BindParameter extends SearchManual
|
||||
{
|
||||
public function __construct(
|
||||
public ? string $parameter = null,
|
||||
public ? string $field = null,
|
||||
public null|string|array $parameter = null,
|
||||
public null|string|\Stringable|array $field = null,
|
||||
public bool $toggle = false,
|
||||
public SearchMethodEnum $method = SearchMethodEnum::Manual,
|
||||
public string $description = "",
|
||||
public PropertyValueSource|array $source = PropertyValueSource::RequestAttribute,
|
||||
) {}
|
||||
}
|
||||
@@ -107,7 +107,7 @@ class Stream implements StreamInterface
|
||||
return $this->hasMode('acwx+');
|
||||
}
|
||||
|
||||
public function write($string) : false|int
|
||||
public function write($string) : int
|
||||
{
|
||||
if (! $this->isWritable()) {
|
||||
throw new \UnexpectedValueException("An error occured while trying to seek an unseekable stream.");
|
||||
@@ -166,7 +166,7 @@ class Stream implements StreamInterface
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getMetadata(string $key = null) : mixed
|
||||
public function getMetadata(?string $key = null) : mixed
|
||||
{
|
||||
$metadata = stream_get_meta_data($this->stream);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,10 @@ class CurlAuthorization
|
||||
public function fromAuthenticationEnum(AuthenticationEnum $auth,) : static
|
||||
{
|
||||
switch($auth) {
|
||||
case AuthenticationEnum::Custom:
|
||||
$this->customHeader($this->configuration['header'], $this->configuration['secret']);
|
||||
break;
|
||||
|
||||
case AuthenticationEnum::Bearer:
|
||||
$this->bearer($this->configuration['token']);
|
||||
break;
|
||||
@@ -96,6 +100,11 @@ class CurlAuthorization
|
||||
$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
|
||||
{
|
||||
$unid = sprintf("oauth_token_%s", md5(serialize($this->configuration)));
|
||||
|
||||
@@ -6,7 +6,7 @@ use Ulmus\Api\Common\{ ContentTypeEnum, MethodEnum, HttpHeaderEnum };
|
||||
use Ulmus\Api\Stream\JsonStream;
|
||||
use Ulmus\Api\Stream\Stream;
|
||||
use Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface};
|
||||
use Ulmus\Api\Response\{ Response, JsonResponse };
|
||||
use Ulmus\Api\Response\{Response, JsonResponse, XmlResponse};
|
||||
|
||||
abstract class CurlTransport {
|
||||
|
||||
@@ -37,9 +37,9 @@ abstract class CurlTransport {
|
||||
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
|
||||
@@ -130,8 +130,14 @@ abstract class CurlTransport {
|
||||
|
||||
$response = $response->withBody( Stream::fromMemory($result) );
|
||||
|
||||
if ( $response->hasHeader('Content-Type') && false !== strpos($response->getHeader('Content-Type')[0], 'json') ) {
|
||||
$response = JsonResponse::fromResponse($response);
|
||||
if ($response->hasHeader('Content-Type') ) {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user