commit 62d5854bbe6dbbad514c0692a90b9d7bc60ac7f0 Author: Dave Mc Nicoll Date: Tue Mar 17 15:49:32 2026 -0400 - First WIP 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 @@ +