- First WIP
This commit is contained in:
commit
62d5854bbe
8 files changed
+476
No files matched your search
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Api\Weather;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\{ RequestInterface, ResponseInterface };
|
||||
use Ulmus\Api\ApiHandlerInterface;
|
||||
use Ulmus\Api\Attribute\Obj\Api\{ Read, Collection };
|
||||
use Ulmus\Api\Common\Uri;
|
||||
use Ulmus\Api\Response\JsonResponse;
|
||||
use Ulmus\Api\Stream\JsonStream;
|
||||
use Ulmus\Api\Stream\Stream;
|
||||
|
||||
class ApiHandler implements ApiHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected ? string $unitScale,
|
||||
) {}
|
||||
|
||||
public function handleRequest(RequestInterface $request, \Ulmus\Api\Attribute\Obj\Api\ApiAction $attribute) : RequestInterface
|
||||
{
|
||||
if ($this->unitScale) {
|
||||
$uriParams['unitScale'] = $this->unitScale;
|
||||
}
|
||||
|
||||
if ($attribute->fields) {
|
||||
$uriParams['fields'] = implode(',', $attribute->fields) ;
|
||||
}
|
||||
|
||||
if ($uriParams ?? false) {
|
||||
$request = $request->withUri(
|
||||
( new Uri($request->getUri()) )->withQuery($uriParams)
|
||||
);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function handleResponse(ResponseInterface $response, \Ulmus\Api\Attribute\Obj\Api\ApiAction $attribute) : ResponseInterface
|
||||
{
|
||||
try {
|
||||
$json = $response->getParsedBody();
|
||||
|
||||
if (! empty($json['forecast']) ) {
|
||||
if ($attribute instanceof Collection) {
|
||||
$response = $response->withParsedBody($json['forecast']);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(\Throwable $e) {
|
||||
throw new \Exception(sprintf("WeatherSource error: '%s'", $json['errorMessage']), $json['errorCode']);
|
||||
}
|
||||
|
||||
return JsonResponse::fromResponse($response);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user