diff --git a/meta/config.php b/meta/config.php index deae901..13f957d 100644 --- a/meta/config.php +++ b/meta/config.php @@ -7,7 +7,8 @@ return [ 'adapter' => getenv("WEATHER_API_ADAPTER"), 'url' => getenv("WEATHER_API_URL"), 'auth' => getenv('WEATHER_API_AUTH') ?: 'token', - 'key' => getenv("WEATHER_API_KEY"), + 'header' => getenv("WEATHER_API_HEADER"), + 'secret' => getenv("WEATHER_API_SECRET"), 'options' => [], ], ] diff --git a/meta/definitions/storage.php b/meta/definitions/storage.php index f635ca4..31f3388 100644 --- a/meta/definitions/storage.php +++ b/meta/definitions/storage.php @@ -1,8 +1,10 @@ DI\create(Ulmus\Api\Weather\ApiHandler::class)->constructor(getenv('WEATHER_API_UNIT_SCALE')), + 'api.weather' => function($c) { - $adapter = new \Ulmus\Api\ConnectionAdapter($c->create(Ulmus\Api\Weather\ApiHandler::class)->constructor(getenv('WEATHER_API_UNIT_SCALE')), 'api-weather', $c->get('config')['ulmus'], false); + $adapter = new \Ulmus\Api\ConnectionAdapter($c->get(Ulmus\Api\Weather\ApiHandler::class), 'api-weather', $c->get('config')['ulmus'], false); $adapter->resolveConfiguration(); return $adapter; diff --git a/src/ApiHandler.php b/src/ApiHandler.php index dc76439..64a613c 100644 --- a/src/ApiHandler.php +++ b/src/ApiHandler.php @@ -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'); + } } \ No newline at end of file diff --git a/src/Attribute/Obj/Api/Read.php b/src/Attribute/Obj/Api/Read.php new file mode 100644 index 0000000..64177e8 --- /dev/null +++ b/src/Attribute/Obj/Api/Read.php @@ -0,0 +1,16 @@ +