Compare commits
3 Commits
a0d4bf6f91
...
package
Author | SHA1 | Date | |
---|---|---|---|
f9247aa54c
|
|||
24007122d8
|
|||
120dee172b
|
111
README.md
111
README.md
@@ -1,42 +1,99 @@
|
||||
# Slim Framework 4 Skeleton Application
|
||||
## Using the OpenWeatherClient Library
|
||||
|
||||
[](https://coveralls.io/github/slimphp/Slim-Skeleton?branch=master)
|
||||
To integrate weather data into your application using the `ohrionmartin/weather` package, follow these steps:
|
||||
|
||||
Use this skeleton application to quickly setup and start working on a new Slim Framework 4 application. This application uses the latest Slim 4 with Slim PSR-7 implementation and PHP-DI container implementation. It also uses the Monolog logger.
|
||||
### 1. Add the Repository and Dependency
|
||||
|
||||
This skeleton application was built for Composer. This makes setting up a new Slim Framework application quick and easy.
|
||||
Update your `composer.json` file to include the custom repository and require the `ohrionmartin/weather` package:
|
||||
|
||||
## Install the Application
|
||||
|
||||
Run this command from the directory in which you want to install your new Slim Framework application. You will require PHP 7.4 or newer.
|
||||
|
||||
```bash
|
||||
composer create-project slim/slim-skeleton [my-app-name]
|
||||
```json
|
||||
"repositories": [
|
||||
{
|
||||
"type": "vcs",
|
||||
"url": "https://git.nampharm.com.na/ohrionmartin/weather"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"ohrionmartin/weather": "dev-master"
|
||||
}
|
||||
```
|
||||
|
||||
Replace `[my-app-name]` with the desired directory name for your new application. You'll want to:
|
||||
|
||||
* Point your virtual host document root to your new application's `public/` directory.
|
||||
* Ensure `logs/` is web writable.
|
||||
|
||||
To run the application in development, you can run these commands
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
cd [my-app-name]
|
||||
composer start
|
||||
composer update
|
||||
```
|
||||
|
||||
Or you can use `docker-compose` to run the app with `docker`, so you can run these commands:
|
||||
```bash
|
||||
cd [my-app-name]
|
||||
docker-compose up -d
|
||||
```
|
||||
After that, open `http://localhost:8080` in your browser.
|
||||
### 2. Configure the API Key
|
||||
|
||||
Run this command in the application directory to run the test suite
|
||||
Ensure you have an API key from OpenWeatherMap. Add it to your `.env` file:
|
||||
|
||||
```bash
|
||||
composer test
|
||||
OPENWEATHER_API_KEY=your_api_key_here
|
||||
```
|
||||
|
||||
That's it! Now go build something cool.
|
||||
### 3. Add Weather Route
|
||||
|
||||
In your application (e.g., in `routes.php` or equivalent), add the following route to handle weather requests:
|
||||
|
||||
```php
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Http\Request;
|
||||
use Ohrionmartin\Weather\Service\OpenWeatherClient;
|
||||
|
||||
$router->get('/weather', function (Request $request) {
|
||||
$apiKey = env('OPENWEATHER_API_KEY');
|
||||
if (!$apiKey) {
|
||||
return response()->json(['error' => 'OPENWEATHER_API_KEY is not configured'], 500);
|
||||
}
|
||||
|
||||
$client = new Client();
|
||||
$ow = new OpenWeatherClient($client, $apiKey);
|
||||
|
||||
$options = [];
|
||||
if ($request->has('units')) {
|
||||
$options['units'] = $request->get('units'); // 'standard'|'metric'|'imperial'
|
||||
}
|
||||
if ($request->has('lang')) {
|
||||
$options['lang'] = $request->get('lang');
|
||||
}
|
||||
if ($request->has('exclude')) {
|
||||
$options['exclude'] = $request->get('exclude'); // comma-separated segments
|
||||
}
|
||||
|
||||
try {
|
||||
if ($request->has('city')) {
|
||||
$data = $ow->oneCallByCity((string) $request->get('city'), $options);
|
||||
} else {
|
||||
if (!$request->has('lat') || !$request->has('lon')) {
|
||||
return response()->json(['error' => 'Provide either ?city=Name or ?lat=..&lon=..'], 400);
|
||||
}
|
||||
$lat = (float) $request->get('lat');
|
||||
$lon = (float) $request->get('lon');
|
||||
$data = $ow->oneCall($lat, $lon, $options);
|
||||
}
|
||||
|
||||
return response()->json($data);
|
||||
} catch (\RuntimeException $e) {
|
||||
return response()->json(['error' => $e->getMessage()], 502);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### 4. Usage Examples
|
||||
|
||||
You can access the weather endpoint with the following query parameters:
|
||||
|
||||
- **By city name**: `http://localhost:8080/weather?city=London`
|
||||
- **By coordinates**: `http://localhost:8080/weather?lat=51.5074&lon=-0.1278`
|
||||
- **With optional parameters**:
|
||||
- `units`: `standard`, `metric`, or `imperial` (e.g., `?units=metric`)
|
||||
- `lang`: Language code for response (e.g., `?lang=fr`)
|
||||
- `exclude`: Comma-separated segments to exclude (e.g., `?exclude=minutely,hourly`)
|
||||
|
||||
Example request:
|
||||
```bash
|
||||
curl "http://localhost:8080/weather?city=London&units=metric&lang=fr"
|
||||
```
|
||||
|
||||
This will return weather data in JSON format for London in metric units and French language.
|
@@ -3,7 +3,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Application\Settings\SettingsInterface;
|
||||
use App\Service\OpenWeatherClient;
|
||||
use Ohrionmartin\Weather\Service\OpenWeatherClient;
|
||||
use GuzzleHttp\Client;
|
||||
use DI\ContainerBuilder;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
|
@@ -3,7 +3,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Domain\User\UserRepository;
|
||||
use App\Infrastructure\Persistence\User\InMemoryUserRepository;
|
||||
use Ohrionmartin\Weather\Infrastructure\Persistence\User\InMemoryUserRepository;
|
||||
use DI\ContainerBuilder;
|
||||
|
||||
return function (ContainerBuilder $containerBuilder) {
|
||||
|
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
use App\Application\Actions\User\ListUsersAction;
|
||||
use App\Application\Actions\User\ViewUserAction;
|
||||
use App\Application\Actions\Weather\GetWeatherAction;
|
||||
use Ohrionmartin\Weather\Application\Actions\Weather\GetWeatherAction;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\App;
|
||||
|
@@ -1,63 +1,27 @@
|
||||
{
|
||||
"name": "slim/slim-skeleton",
|
||||
"description": "A Slim Framework skeleton application for rapid development",
|
||||
"keywords": [
|
||||
"microframework",
|
||||
"rest",
|
||||
"router",
|
||||
"psr7"
|
||||
],
|
||||
"homepage": "http://github.com/slimphp/Slim-Skeleton",
|
||||
"name": "ohrionmartin/weather",
|
||||
"description": "Reusable Slim components for fetching weather via OpenWeather One Call API 3.0",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Ohrionmartin\\Weather\\": "src/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Josh Lockhart",
|
||||
"email": "info@joshlockhart.com",
|
||||
"homepage": "http://www.joshlockhart.com/"
|
||||
},
|
||||
{
|
||||
"name": "Pierre Berube",
|
||||
"email": "pierre@lgse.com",
|
||||
"homepage": "http://www.lgse.com/"
|
||||
"name": "Oh Martin",
|
||||
"email": "oh@nampharm.com.na"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"php": "^7.4 || ^8.0",
|
||||
"ext-json": "*",
|
||||
"guzzlehttp/guzzle": "^7",
|
||||
"monolog/monolog": "^2.8",
|
||||
"php-di/php-di": "^6.4",
|
||||
"slim/psr7": "^1.5",
|
||||
"slim/slim": "^4.10",
|
||||
"vlucas/phpdotenv": "^5.6"
|
||||
},
|
||||
"require-dev": {
|
||||
"jangregor/phpstan-prophecy": "^1.0.0",
|
||||
"phpspec/prophecy-phpunit": "^2.0",
|
||||
"phpstan/extension-installer": "^1.2.0",
|
||||
"phpstan/phpstan": "^1.8",
|
||||
"phpunit/phpunit": "^9.5.26",
|
||||
"squizlabs/php_codesniffer": "^3.7"
|
||||
"guzzlehttp/guzzle": "^7.8"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"phpstan/extension-installer": true
|
||||
},
|
||||
"process-timeout": 0,
|
||||
"sort-packages": true
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/"
|
||||
"phpstan/extension-installer": false
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"start": "php -S localhost:8080 -t public",
|
||||
"test": "phpunit"
|
||||
}
|
||||
}
|
||||
|
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Application\Actions\Weather;
|
||||
namespace Ohrionmartin\Weather\Application\Actions\Weather;
|
||||
|
||||
use App\Service\OpenWeatherClient;
|
||||
use Ohrionmartin\Weather\Service\OpenWeatherClient;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Infrastructure\Persistence\User;
|
||||
namespace Ohrionmartin\Weather\Infrastructure\Persistence\User;
|
||||
|
||||
use App\Domain\User\User;
|
||||
use App\Domain\User\UserNotFoundException;
|
||||
|
46
src/Infrastructure/Provider/SlimWeatherProvider.php
Normal file
46
src/Infrastructure/Provider/SlimWeatherProvider.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ohrionmartin\Weather\Infrastructure\Provider;
|
||||
|
||||
use Ohrionmartin\Weather\Application\Actions\Weather\GetWeatherAction;
|
||||
use Ohrionmartin\Weather\Service\OpenWeatherClient;
|
||||
use DI\ContainerBuilder;
|
||||
use GuzzleHttp\Client;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\App;
|
||||
|
||||
final class SlimWeatherProvider
|
||||
{
|
||||
public static function register(ContainerBuilder $cb): void
|
||||
{
|
||||
$cb->addDefinitions([
|
||||
Client::class => function () {
|
||||
return new Client([
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
'User-Agent' => 'Slim-Weather/1.0',
|
||||
],
|
||||
]);
|
||||
},
|
||||
|
||||
OpenWeatherClient::class => function (ContainerInterface $c) {
|
||||
$apiKey = $_ENV['OPENWEATHER_API_KEY'] ?? $_SERVER['OPENWEATHER_API_KEY'] ?? '';
|
||||
$baseUrl = 'https://api.openweathermap.org/data/3.0';
|
||||
|
||||
if ($apiKey === '') {
|
||||
throw new \RuntimeException('OPENWEATHER_API_KEY is not configured');
|
||||
}
|
||||
|
||||
return new OpenWeatherClient($c->get(Client::class), $apiKey, $baseUrl);
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
public static function routes(App $app): void
|
||||
{
|
||||
// Expose a ready-to-use endpoint for consumers (optional)
|
||||
$app->get('/weather', GetWeatherAction::class);
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
namespace Ohrionmartin\Weather\Service;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
@@ -6,7 +6,7 @@ namespace Tests\Infrastructure\Persistence\User;
|
||||
|
||||
use App\Domain\User\User;
|
||||
use App\Domain\User\UserNotFoundException;
|
||||
use App\Infrastructure\Persistence\User\InMemoryUserRepository;
|
||||
use Ohrionmartin\Weather\Infrastructure\Persistence\User\InMemoryUserRepository;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InMemoryUserRepositoryTest extends TestCase
|
||||
|
Reference in New Issue
Block a user