Working on Api's platform from Ulmus\Api

This commit is contained in:
Dave M. 2023-10-30 15:22:11 +00:00
parent 340421083c
commit 0c9afdd908
2 changed files with 38 additions and 29 deletions

38
src/ApiHandler.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace Ulmus\Api\Gitea;
use Psr\Http\Message\{ RequestInterface, ResponseInterface };
use Ulmus\Api\ApiHandlerInterface;
use Ulmus\Api\Response\JsonResponse;
class ApiHandler implements ApiHandlerInterface
{
public function handleRequest(RequestInterface $request, \Ulmus\Api\Attribute\Obj\Api\ApiAction $attribute) : RequestInterface
{
return $request;
}
public function handleResponse(ResponseInterface $response, \Ulmus\Api\Attribute\Obj\Api\ApiAction $attribute) : ResponseInterface
{
$json = $response->getParsedBody();
if ($json['message'] ?? false) {
if ( array_key_exists('errors', $json) && null === $json['errors'] ?? null) {
# if ($attribute instanceof \Ulmus\Api\Attribute\Obj\Api\Collection) {
# return [];
# }
# elseif ($attribute instanceof \Ulmus\Api\Attribute\Obj\Api\Read) {
# return null;
# }
# elseif ($attribute instanceof \Ulmus\Api\Attribute\Obj\Api\Create) {
# return false;
# }
}
throw new \Exception($json['message']);
}
return JsonResponse::fromResponse($response)->withParsedBody($json);
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace Ulmus\Api\Gitea;
class ApiResponseHandler
{
public static function handle(\Psr\Http\Message\ResponseInterface $response, \Ulmus\Api\Attribute\Obj\Api\ApiAction $attribute) : mixed
{
$json = $response->render();
if ($json['message'] ?? false) {
if ( array_key_exists('errors', $json) && null === $json['errors'] ?? null) {
if ($attribute instanceof \Ulmus\Api\Attribute\Obj\Api\Collection) {
return [];
}
elseif ($attribute instanceof \Ulmus\Api\Attribute\Obj\Api\Read) {
return null;
}
elseif ($attribute instanceof \Ulmus\Api\Attribute\Obj\Api\Create) {
return false;
}
}
throw new \Exception($json['message']);
}
return $json;
}
}