- Still WIP on Api's wrapper
This commit is contained in:
1 parent
7e69a09e7f
commit
5d9c0f0dbf
16 files changed
+548
-42
No files matched your search
+54
-7
@@ -2,10 +2,18 @@
|
||||
|
||||
namespace Ulmus\Api;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
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\Stream;
|
||||
use Ulmus\Api\Request\JsonRequest;
|
||||
use Ulmus\Api\Request\Request;
|
||||
use Ulmus\Api\Transport\CurlClient;
|
||||
use Ulmus\EntityCollection;
|
||||
use Ulmus\SearchRequest\SearchRequestInterface;
|
||||
|
||||
class ApiRepository extends \Ulmus\Repository
|
||||
{
|
||||
@@ -29,29 +37,41 @@ class ApiRepository extends \Ulmus\Repository
|
||||
# Must manager URL here !
|
||||
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Read::class);
|
||||
|
||||
$this->lastResponse = $response = $this->client->request($attribute->method, $this->buildRequestUrl($attribute->url));
|
||||
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method);
|
||||
|
||||
$dataset = $this->callApiResponseCallback($response, $attribute);
|
||||
$request = $this->callApiRequestCallback($request, $attribute);
|
||||
|
||||
return $this->instanciateEntity()->fromArray($dataset);
|
||||
$this->lastResponse = $response = $this->client->fromRequest($request);
|
||||
|
||||
$response = $this->callApiResponseCallback($response, $attribute);
|
||||
|
||||
return $this->instanciateEntity()->fromArray($response->render());
|
||||
}
|
||||
|
||||
public function loadAll() : EntityCollection
|
||||
{
|
||||
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Collection::class);
|
||||
|
||||
$this->lastResponse = $response = $this->client->request($attribute->method, $this->buildRequestUrl($attribute->url));
|
||||
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method);
|
||||
|
||||
$collection = $this->callApiResponseCallback($response, $attribute);
|
||||
$request = $this->callApiRequestCallback($request, $attribute);
|
||||
|
||||
return $this->instanciateEntityCollection($collection);
|
||||
$this->lastResponse = $response = $this->client->fromRequest($request);
|
||||
|
||||
$response = $this->callApiResponseCallback($response, $attribute);
|
||||
|
||||
return $this->instanciateEntityCollection()->fromArray($response->render());
|
||||
}
|
||||
|
||||
public function save(object|array $entity, ?array $fieldsAndValue = null, bool $replace = false): bool
|
||||
{
|
||||
$attribute = $this->getApiAttribute(Attribute\Obj\Api\Create::class);
|
||||
|
||||
$this->lastResponse = $response = $this->client->request($attribute->method, $this->buildRequestUrl($attribute->url), $entity->entityLoadedDataset);
|
||||
$request = $this->prepareRequest($this->buildRequestUrl($attribute->url), $attribute->method);
|
||||
|
||||
$this->callApiRequestCallback($request, $attribute);
|
||||
|
||||
$this->lastResponse = $response = $this->client->fromRequest($request);
|
||||
|
||||
$ret = $this->callApiResponseCallback($response, $attribute);
|
||||
|
||||
@@ -112,13 +132,40 @@ class ApiRepository extends \Ulmus\Repository
|
||||
return $route;
|
||||
}
|
||||
|
||||
protected function prepareRequest(string|UriInterface $uri, MethodEnum $method, null|string $body = null, array $headers = []) : RequestInterface
|
||||
{
|
||||
return new JsonRequest($uri, $method, $body === null ? Stream::fromTemp() : Stream::fromMemory($body), $headers);
|
||||
}
|
||||
|
||||
public function getApiAttribute(string $type) : object
|
||||
{
|
||||
return $this->entityClass::resolveEntity()->getAttributeImplementing($type);
|
||||
}
|
||||
|
||||
protected function callApiRequestCallback(RequestInterface $request, ApiAction $attribute) : mixed
|
||||
{
|
||||
return call_user_func_array($this->adapter->apiRequestCallback(), func_get_args());
|
||||
}
|
||||
|
||||
protected function callApiResponseCallback(ResponseInterface $response, ApiAction $attribute) : mixed
|
||||
{
|
||||
return call_user_func_array($this->adapter->apiResponseCallback(), func_get_args());
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public function filterServerRequest(SearchRequestInterface $searchRequest, bool $count = true) : self
|
||||
{
|
||||
return $searchRequest->filter($this)
|
||||
->wheres($searchRequest->wheres(), \Ulmus\Query\Where::OPERATOR_EQUAL, \Ulmus\Query\Where::CONDITION_AND)
|
||||
->likes($searchRequest->likes(), \Ulmus\Query\Where::CONDITION_OR)
|
||||
->orders($searchRequest->orders())
|
||||
->groups($searchRequest->groups())
|
||||
->offset($searchRequest->offset())
|
||||
->limit($searchRequest->limit());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user