Working on Api's platform from Ulmus\Api
This commit is contained in:
parent
340421083c
commit
0c9afdd908
38
src/ApiHandler.php
Normal file
38
src/ApiHandler.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user