- 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
+32
-44
@@ -8,6 +8,7 @@ use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Ulmus\Api\Attribute\Obj\Api\ApiAction;
|
||||
use Ulmus\Api\Common\MethodEnum;
|
||||
use Ulmus\Api\Stream\JsonStream;
|
||||
use Ulmus\Api\Stream\Stream;
|
||||
use Ulmus\Api\Request\JsonRequest;
|
||||
use Ulmus\Api\Request\Request;
|
||||
@@ -28,56 +29,44 @@ class ApiRepository extends \Ulmus\Repository
|
||||
public function __construct(string $entity, string $alias = self::DEFAULT_ALIAS, ConnectionAdapter $adapter = null)
|
||||
{
|
||||
parent::__construct($entity, $alias, $adapter);
|
||||
|
||||
$this->client = $adapter->adapter()->connect();
|
||||
}
|
||||
|
||||
public function loadOne(): ?object
|
||||
{
|
||||
# Must manager URL here !
|
||||
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Read::class);
|
||||
$response = $this->executeRequest(Attribute\Obj\Api\Read::class);
|
||||
|
||||
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method);
|
||||
|
||||
$request = $this->callApiRequestCallback($request, $attribute);
|
||||
|
||||
$this->lastResponse = $response = $this->client->fromRequest($request);
|
||||
|
||||
$response = $this->callApiResponseCallback($response, $attribute);
|
||||
|
||||
return $this->instanciateEntity()->fromArray($response->render());
|
||||
return $this->instanciateEntity()->fromArray($response->getParsedBody());
|
||||
}
|
||||
|
||||
public function loadAll() : EntityCollection
|
||||
{
|
||||
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Collection::class);
|
||||
$response = $this->executeRequest(Attribute\Obj\Api\Collection::class);
|
||||
|
||||
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method);
|
||||
|
||||
$request = $this->callApiRequestCallback($request, $attribute);
|
||||
|
||||
$this->lastResponse = $response = $this->client->fromRequest($request);
|
||||
|
||||
$response = $this->callApiResponseCallback($response, $attribute);
|
||||
|
||||
return $this->instanciateEntityCollection()->fromArray($response->render());
|
||||
return $this->instanciateEntityCollection()->fromArray($response->getParsedBody());
|
||||
}
|
||||
|
||||
public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool
|
||||
{
|
||||
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Create::class);
|
||||
$response = $this->executeRequest(Attribute\Obj\Api\Create::class, $entity->entityLoadedDataset);
|
||||
|
||||
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method);
|
||||
$entity->fromArray($response->getParsedBody());
|
||||
|
||||
$this->callApiRequestCallback($request, $attribute);
|
||||
return $response->getStatusCode() === 200;
|
||||
}
|
||||
|
||||
$this->lastResponse = $response = $this->client->fromRequest($request);
|
||||
public function executeRequest(string $attributeClass, mixed $data = null) : ResponseInterface
|
||||
{
|
||||
$attribute = $this->getApiAttribute($attributeClass);
|
||||
|
||||
$ret = $this->callApiResponseCallback($response, $attribute);
|
||||
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method, $data);
|
||||
|
||||
$entity->fromArray($ret);
|
||||
$request = $this->callApiRequestCallback($request, $attribute);
|
||||
|
||||
return true;
|
||||
$this->lastResponse = $response = $this->adapter->adapter()->connect()->fromRequest($request);
|
||||
|
||||
$response = $this->callApiResponseCallback($response, $attribute);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function bindUrl(string $parameter, mixed $value) : self
|
||||
@@ -110,9 +99,9 @@ class ApiRepository extends \Ulmus\Repository
|
||||
}
|
||||
|
||||
if ( array_key_exists($variable, $arguments) ) {
|
||||
$value = $arguments[ $variable ];
|
||||
$value = $arguments[$variable];
|
||||
|
||||
unset($arguments[ $variable ]);
|
||||
unset($arguments[$variable]);
|
||||
}
|
||||
else {
|
||||
if ($default ?? false) {
|
||||
@@ -132,9 +121,9 @@ class ApiRepository extends \Ulmus\Repository
|
||||
return $route;
|
||||
}
|
||||
|
||||
protected function prepareRequest(string|UriInterface $uri, MethodEnum $method, null|string $body = null, array $headers = []) : RequestInterface
|
||||
protected function prepareRequest(string|UriInterface $uri, MethodEnum $method, mixed $body = null, array $headers = []) : RequestInterface
|
||||
{
|
||||
return new JsonRequest($uri, $method, $body === null ? Stream::fromTemp() : Stream::fromMemory($body), $headers);
|
||||
return new JsonRequest($uri, $method, $body === null ? Stream::fromTemp() : JsonStream::fromContent($body), $headers);
|
||||
}
|
||||
|
||||
public function getApiAttribute(string $type) : object
|
||||
@@ -142,22 +131,16 @@ class ApiRepository extends \Ulmus\Repository
|
||||
return $this->entityClass::resolveEntity()->getAttributeImplementing($type);
|
||||
}
|
||||
|
||||
protected function callApiRequestCallback(RequestInterface $request, ApiAction $attribute) : mixed
|
||||
protected function callApiRequestCallback(RequestInterface $request, ApiAction $attribute) : RequestInterface
|
||||
{
|
||||
return call_user_func_array($this->adapter->apiRequestCallback(), func_get_args());
|
||||
return $this->adapter->apiHandler->handleRequest($request, $attribute);
|
||||
}
|
||||
|
||||
protected function callApiResponseCallback(ResponseInterface $response, ApiAction $attribute) : mixed
|
||||
protected function callApiResponseCallback(ResponseInterface $response, ApiAction $attribute) : ResponseInterface
|
||||
{
|
||||
return call_user_func_array($this->adapter->apiResponseCallback(), func_get_args());
|
||||
return $this->adapter->apiHandler->handleResponse($response, $attribute);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public function filterServerRequest(SearchRequestInterface $searchRequest, bool $count = true) : self
|
||||
{
|
||||
return $searchRequest->filter($this)
|
||||
@@ -168,4 +151,9 @@ class ApiRepository extends \Ulmus\Repository
|
||||
->offset($searchRequest->offset())
|
||||
->limit($searchRequest->limit());
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user