From 62d5854bbe6dbbad514c0692a90b9d7bc60ac7f0 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 17 Mar 2026 15:49:32 -0400 Subject: [PATCH] - First WIP --- composer.json | 31 ++++ meta/config.php | 15 ++ meta/definitions/storage.php | 10 ++ src/ApiHandler.php | 56 +++++++ src/Attribute/Obj/Api/Collection.php | 16 ++ src/Entity/ForecastDay.php | 224 +++++++++++++++++++++++++++ src/Entity/ForecastHour.php | 116 ++++++++++++++ src/Lib/EntityTrait.php | 8 + 8 files changed, 476 insertions(+) create mode 100644 composer.json create mode 100644 meta/config.php create mode 100644 meta/definitions/storage.php create mode 100644 src/ApiHandler.php create mode 100644 src/Attribute/Obj/Api/Collection.php create mode 100644 src/Entity/ForecastDay.php create mode 100644 src/Entity/ForecastHour.php create mode 100644 src/Lib/EntityTrait.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cae45e2 --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "mcnd/ulmus-api-weather", + "description": "A package made for mcnd/ulmus-api", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "info@mcnd.ca" + } + ], + "require": { + "mcnd/ulmus": "dev-master", + "mcnd/ulmus-api": "dev-master" + }, + "repositories": [ + { + "type": "vcs", + "url": "https://git.mcnd.ca/mcndave/ulmus.git" + }, + { + "type": "vcs", + "url": "https://git.mcnd.ca/mcndave/ulmus-api.git" + } + ], + "autoload": { + "psr-4": { + "Ulmus\\Api\\Weather\\": "src/" + } + } +} diff --git a/meta/config.php b/meta/config.php new file mode 100644 index 0000000..deae901 --- /dev/null +++ b/meta/config.php @@ -0,0 +1,15 @@ + [ + 'connections' => [ + 'api-weather' => [ + 'adapter' => getenv("WEATHER_API_ADAPTER"), + 'url' => getenv("WEATHER_API_URL"), + 'auth' => getenv('WEATHER_API_AUTH') ?: 'token', + 'key' => getenv("WEATHER_API_KEY"), + 'options' => [], + ], + ] + ] +]; \ No newline at end of file diff --git a/meta/definitions/storage.php b/meta/definitions/storage.php new file mode 100644 index 0000000..f635ca4 --- /dev/null +++ b/meta/definitions/storage.php @@ -0,0 +1,10 @@ + 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->resolveConfiguration(); + + return $adapter; + }, +]; \ No newline at end of file diff --git a/src/ApiHandler.php b/src/ApiHandler.php new file mode 100644 index 0000000..dc76439 --- /dev/null +++ b/src/ApiHandler.php @@ -0,0 +1,56 @@ +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); + } +} \ No newline at end of file diff --git a/src/Attribute/Obj/Api/Collection.php b/src/Attribute/Obj/Api/Collection.php new file mode 100644 index 0000000..c96d8a2 --- /dev/null +++ b/src/Attribute/Obj/Api/Collection.php @@ -0,0 +1,16 @@ +