2018-04-11 19:49:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Grocy\Controllers;
|
|
|
|
|
|
|
|
class BaseApiController extends BaseController
|
|
|
|
{
|
2020-08-31 20:40:31 +02:00
|
|
|
protected $OpenApiSpec = null;
|
2018-04-22 14:25:08 +02:00
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
public function __construct(\DI\Container $container)
|
2018-04-22 14:25:08 +02:00
|
|
|
{
|
|
|
|
parent::__construct($container);
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
protected function ApiResponse(\Psr\Http\Message\ResponseInterface $response, $data)
|
2018-04-22 14:25:08 +02:00
|
|
|
{
|
2020-02-11 17:42:03 +01:00
|
|
|
$response->getBody()->write(json_encode($data));
|
|
|
|
return $response;
|
2018-04-22 14:25:08 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
protected function EmptyApiResponse(\Psr\Http\Message\ResponseInterface $response, $status = 204)
|
2019-01-19 14:51:51 +01:00
|
|
|
{
|
|
|
|
return $response->withStatus($status);
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
protected function GenericErrorResponse(\Psr\Http\Message\ResponseInterface $response, $errorMessage, $status = 400)
|
2018-04-11 19:49:35 +02:00
|
|
|
{
|
2020-08-31 20:40:31 +02:00
|
|
|
return $response->withStatus($status)->withJson([
|
2018-04-22 14:25:08 +02:00
|
|
|
'error_message' => $errorMessage
|
2020-08-31 20:40:31 +02:00
|
|
|
]);
|
2018-04-11 19:49:35 +02:00
|
|
|
}
|
2020-08-31 20:40:31 +02:00
|
|
|
|
|
|
|
protected function getOpenApispec()
|
|
|
|
{
|
|
|
|
if ($this->OpenApiSpec == null)
|
|
|
|
{
|
|
|
|
$this->OpenApiSpec = json_decode(file_get_contents(__DIR__ . '/../grocy.openapi.json'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->OpenApiSpec;
|
|
|
|
}
|
|
|
|
|
2018-04-11 19:49:35 +02:00
|
|
|
}
|