Skip to content

Commit

Permalink
Merge pull request #5 from vemcogroup/feature/add-weatherkit
Browse files Browse the repository at this point in the history
Weatherkit
  • Loading branch information
mikkp17 authored Feb 15, 2023
2 parents f045bed + abd5023 commit 97e3f53
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.4]
php: [8.0]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand Down
66 changes: 6 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,72 +25,18 @@ To publish the config file to `config/weather.php` run:
php artisan vendor:publish --provider="Vemcogroup\Weather\WeatherServiceProvider"
```

This is the default contents of the configuration:
The default configuration can be seen [here](https://github.com/vemcogroup/laravel-weather/blob/master/config/weather.php)

```php
return [

/*
|--------------------------------------------------------------------------
| API Key
|--------------------------------------------------------------------------
|
| Here you define your API Key for weather provider.
|
*/

'api_key' => env('WEATHER_API_KEY'),

/*
|--------------------------------------------------------------------------
| Weather provider
|--------------------------------------------------------------------------
|
| Here you define provider you want to get weather information from.
|
*/

'provider' => env('WEATHER_PROVIDER'),

/*
|--------------------------------------------------------------------------
| Midday
|--------------------------------------------------------------------------
|
| Here you define what time is midday.
|
*/

'midday' => [
'hour' => '13',
'minute' => '59',
],

/*
|--------------------------------------------------------------------------
| Intervals
|--------------------------------------------------------------------------
|
| Here you define the intervals for forecast and historical data.
| !Only available for Weatherstack
|
*/
'intervals' => [
'forecast' => env('WEATHER_FORECAST_INTERVAL', 24),
'historical' => env('WEATHER_HISTORICAL_INTERVAL', 1),
],

];
```

## Usage

At the moment this package support the following weather services, you can update `WEATHER_PROVIDER` to one of the following

| Service | Provider name | Website | Geocoding | Remarks |
| :--- | :--- | :--- | :---: | :--- |
| Dark Sky | darksy | https://darksky.net | Manual | Deprecated, not able to acquire api key https://blog.darksky.net |
| Weatherstack | weatherstack | https://weatherstack.com | Auto | For historical data a minimum Standard license is required. For forecast data a minimum Professional license is required. |
| Service | Provider name | Website | Geocoding | Remarks |
|:-------------|:--------------|:----------------------------------------|:---------:|:--------------------------------------------------------------------------------------------------------------------------|
| Dark Sky | darksky | https://darksky.net | Manual | Deprecated, not able to acquire api key https://blog.darksky.net. Will continue to function until March 31st, 2023. |
| Weatherstack | weatherstack | https://weatherstack.com | Auto | For historical data a minimum Standard license is required. For forecast data a minimum Professional license is required. |
| WeatherKit | weatherkit | https://developer.apple.com/weatherkit/ | Auto | Needs an apple developer account. |

For other weather services fill free to create an issue or make a Pull Request.

Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
}
],
"require": {
"php": "^7.3|^8.0",
"php": "^8.0",
"ext-json": "*",
"firebase/php-jwt": "^6.4",
"laravel/framework": "^9.0|^10.0",
"spatie/geocoder": "^3.1",
"laravel/framework": "^7.0|^8.0|^9.0"
"ext-openssl": "*"
},
"require-dev": {
"orchestra/testbench": "^4.0|^5.0",
"phpunit/phpunit": "^8.0"
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
Expand Down
88 changes: 82 additions & 6 deletions config/weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,91 @@

/*
|--------------------------------------------------------------------------
| Intervals
| Providers
|--------------------------------------------------------------------------
|
| Here you define the intervals for forecast and historical data.
| Only available for Weatherstack:
| Here you define the provider-specific settings
|
*/
'intervals' => [
'forecast' => env('WEATHER_FORECAST_INTERVAL', 24),
'historical' => env('WEATHER_HISTORICAL_INTERVAL', 1),
'providers' => [

'weatherkit' => [

/*
|--------------------------------------------------------------------------
| Private key
|--------------------------------------------------------------------------
|
| Private key issued from your Apple Developer Account
|
*/
'private-key' => base64_decode(env('WEATHER_KIT_PRIVATE_KEY')),

/*
|--------------------------------------------------------------------------
| Algorithm
|--------------------------------------------------------------------------
|
| The algorithm with which to sign the token. Weatherkit only supports ES256.
|
*/
'alg' => env('WEATHER_KIT_ALGORITHM', 'ES256'),

/*
|--------------------------------------------------------------------------
| Key ID
|--------------------------------------------------------------------------
|
| Key identifier obtained from your Apple Developer Account
|
*/
'kid' => env('WEATHER_KIT_KID'),

/*
|--------------------------------------------------------------------------
| ID
|--------------------------------------------------------------------------
|
| An identifier that consists of your 10-character Team ID and Service ID, separated by a period.
|
*/
'id' => env('WEATHER_KIT_ID'),

/*
|--------------------------------------------------------------------------
| Issuer Claim Key
|--------------------------------------------------------------------------
|
| The issuer claim key. This value is your 10-character Team ID from your developer account.
|
*/
'iss' => env('WEATHER_KIT_ISS'),

/*
|--------------------------------------------------------------------------
| Subject Public Claim Key
|--------------------------------------------------------------------------
|
| The subject public claim key. This value is your registered Service ID.
|
*/
'sub' => env('WEATHER_KIT_SUB'),
],

'weatherstack' => [

/*
|--------------------------------------------------------------------------
| Intervals
|--------------------------------------------------------------------------
|
| Here you define the intervals for forecast and historical data.
|
*/
'intervals' => [
'forecast' => env('WEATHER_FORECAST_INTERVAL', 24),
'historical' => env('WEATHER_HISTORICAL_INTERVAL', 1),
],
]
],
];
4 changes: 3 additions & 1 deletion src/Exceptions/WeatherException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

class WeatherException extends Exception
{
public const NO_API_KEY_ERROR_CODE = 1001;

public static function noApiKey(): self
{
return new static('Missing WEATHER_API_KEY, please add it in .env', 1001);
return new static('Missing WEATHER_API_KEY, please add it in .env', self::NO_API_KEY_ERROR_CODE);
}

public static function noProvider(): self
Expand Down
20 changes: 18 additions & 2 deletions src/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Vemcogroup\Weather\Exceptions\WeatherException;
use function md5;
use function method_exists;

abstract class Provider
{
Expand Down Expand Up @@ -42,7 +44,7 @@ public function __construct()
{
$this->requests = [];

if (!$this->apiKey = config('weather.api_key')) {
if (!($this->apiKey = config('weather.api_key'))) {
throw WeatherException::noApiKey();
}

Expand Down Expand Up @@ -70,7 +72,11 @@ protected function processRequests(): void

yield $this->client->getAsync($request->getUrl())->then(function (GuzzleResponse $response) use ($request) {
$content = json_decode($response->getBody());
Cache::put(md5('laravel-weather-' . $request->getUrl()), $content, $request->getCacheTimeout());

if (!method_exists($this, 'verifyResponse') || (method_exists($this, 'verifyResponse') && $this->verifyResponse($content))) {
Cache::put(md5('laravel-weather-' . $request->getUrl()), $content, $request->getCacheTimeout());
}

$request->setResponse($content);
});
}
Expand All @@ -85,4 +91,14 @@ protected function processRequests(): void
]);
$eachPromise->promise()->wait();
}

protected function celsiusToFahrenheit(float $celsius): float
{
return ($celsius * 1.8) + 32;
}

protected function kmToMiles(float $km): float
{
return $km * 0.62137119;
}
}
Loading

0 comments on commit 97e3f53

Please sign in to comment.