- Working version
This commit is contained in:
1 parent
c2e7628b22
commit
9c70225a77
7 files changed
+173
-81
No files matched your search
+28
-2
@@ -10,6 +10,7 @@ use Ulmus\Api\Common\Uri;
|
||||
use Ulmus\Api\Response\JsonResponse;
|
||||
use Ulmus\Api\Stream\JsonStream;
|
||||
use Ulmus\Api\Stream\Stream;
|
||||
use Ulmus\Repository\RepositoryInterface;
|
||||
|
||||
class ApiHandler implements ApiHandlerInterface
|
||||
{
|
||||
@@ -17,6 +18,11 @@ class ApiHandler implements ApiHandlerInterface
|
||||
protected ? string $unitScale,
|
||||
) {}
|
||||
|
||||
public function executingRequest(RepositoryInterface $repository, \Ulmus\Api\Attribute\Obj\Api\ApiAction $attribute) : void
|
||||
{
|
||||
$repository->bindUrl('apiBase', $repository->entityClass::API_BASE_URL);
|
||||
}
|
||||
|
||||
public function handleRequest(RequestInterface $request, \Ulmus\Api\Attribute\Obj\Api\ApiAction $attribute) : RequestInterface
|
||||
{
|
||||
if ($this->unitScale) {
|
||||
@@ -24,7 +30,7 @@ class ApiHandler implements ApiHandlerInterface
|
||||
}
|
||||
|
||||
if ($attribute->fields) {
|
||||
$uriParams['fields'] = implode(',', $attribute->fields) ;
|
||||
$uriParams['fields'] = implode(',', $attribute->fields);
|
||||
}
|
||||
|
||||
if ($uriParams ?? false) {
|
||||
@@ -42,15 +48,35 @@ class ApiHandler implements ApiHandlerInterface
|
||||
$json = $response->getParsedBody();
|
||||
|
||||
if (! empty($json['forecast']) ) {
|
||||
foreach($json['forecast'] as &$forecast) {
|
||||
$forecast['date'] = $this->adjustTimezone($forecast['date']);
|
||||
}
|
||||
|
||||
if ($attribute instanceof Collection) {
|
||||
$response = $response->withParsedBody($json['forecast']);
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($json['nowcast'])) {
|
||||
$json['nowcast']['timestamp'] = new \DateTime($json['nowcast']['timestamp'], new \DateTimeZone('UTC'))->setTimezone(new \DateTimeZone('America/Toronto'))->format('Y-m-d H:i:s');
|
||||
|
||||
if ($attribute instanceof Read) {
|
||||
$response = $response->withParsedBody($json['nowcast']);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(\Throwable $e) {
|
||||
throw new \Exception(sprintf("WeatherSource error: '%s'", $json['errorMessage']), $json['errorCode']);
|
||||
$msg = $json['errorMessage'] ?? $e->getMessage();
|
||||
$code = $json['errorCode'] ?? $e->getCode();
|
||||
|
||||
throw new \Exception(sprintf("WeatherSource error: '%s'", $msg), $code);
|
||||
}
|
||||
|
||||
return JsonResponse::fromResponse($response);
|
||||
}
|
||||
|
||||
protected function adjustTimezone(string $datetime) : string
|
||||
{
|
||||
return new \DateTime("@" . strtotime($datetime))->format('Y-m-d H:i:s');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user