-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from nspalo/feature/BWA-5-weather-app-api-serv…
…ice-refactor feature/BWA-5-weather-app-api-service-refactor
- Loading branch information
Showing
13 changed files
with
282 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
abstract class Resource extends JsonResource | ||
{ | ||
/** | ||
* Return this resource as an array | ||
* | ||
* @noinspection PhpMissingParentCallCommonInspection | ||
* | ||
* @param Request $request | ||
* | ||
* @return string[] | ||
*/ | ||
public function toArray($request): array | ||
{ | ||
return $this->getResponse(); | ||
} | ||
|
||
abstract public function getResponse(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/app/Services/GeoapifyApi/Resources/GeolocationResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\GeoapifyApi\Resources; | ||
|
||
use App\Http\Resources\Resource; | ||
|
||
class GeolocationResource extends Resource | ||
{ | ||
public function __construct($resource) | ||
{ | ||
$resource = $resource[0]; | ||
|
||
parent::__construct($resource); | ||
} | ||
|
||
public function getResponse(): array | ||
{ | ||
return [ | ||
'city' => $this->resource['city'], | ||
'country' => $this->resource['country'], | ||
'country_code' => $this->resource['country_code'], | ||
'formatted' => $this->resource['formatted'], | ||
'coordinate' => [ | ||
'lon' => $this->resource['lon'], | ||
'lat' => $this->resource['lat'], | ||
], | ||
]; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/app/Services/OpenWeatherApi/AbstractOpenWeatherApiService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\OpenWeatherApi; | ||
|
||
use App\Enums\WeatherTypeEnum; | ||
use App\Http\Resources\Resource; | ||
use App\Services\ConfigurationMapper\Interfaces\ServiceConfigurationMapperInterface; | ||
use App\Services\GeoapifyApi\Resources\GeolocationResource; | ||
use App\Services\OpenWeatherApi\Interfaces\WeatherResourceInterface; | ||
use App\Services\UrlQueryStringBuilder\Interfaces\UrlQueryStringBuilderServiceInterface; | ||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
abstract class AbstractOpenWeatherApiService | ||
{ | ||
private ServiceConfigurationMapperInterface $serviceConfigurationMapper; | ||
|
||
private UrlQueryStringBuilderServiceInterface $urlQueryStringBuilderService; | ||
|
||
public function __construct( | ||
ServiceConfigurationMapperInterface $serviceConfigurationMapper, | ||
UrlQueryStringBuilderServiceInterface $urlQueryStringBuilderService | ||
) { | ||
$this->serviceConfigurationMapper = $serviceConfigurationMapper; | ||
$this->urlQueryStringBuilderService = $urlQueryStringBuilderService; | ||
|
||
$this->serviceConfigurationMapper->loadConfig('services.api.open_weather'); | ||
} | ||
|
||
protected function buildOpenWeatherApiQueryString(GeolocationResource $geolocation, WeatherTypeEnum $weatherApiServiceType): string | ||
{ | ||
$mapping = [ | ||
'units' => 'unit', | ||
'lang' => 'lang', | ||
'appid' => 'key', | ||
]; | ||
|
||
if ($weatherApiServiceType === WeatherTypeEnum::Forecast) { | ||
$mapping['cnt'] = 'max_output'; | ||
} | ||
|
||
$queryParam = $this->serviceConfigurationMapper->map($mapping); | ||
$queryParam = array_merge( | ||
$queryParam, [ | ||
'lon' => $geolocation['lon'], | ||
'lat' => $geolocation['lat'], | ||
] | ||
); | ||
|
||
return $this->urlQueryStringBuilderService->build($queryParam); | ||
} | ||
|
||
protected function sendRequest(GeolocationResource $geolocation, WeatherTypeEnum $apiServiceType): Response | ||
{ | ||
$apiServiceUri = $this->serviceConfigurationMapper->getByKey('uri'); | ||
$apiServiceQueryParam = $this->buildOpenWeatherApiQueryString($geolocation, $apiServiceType); | ||
|
||
$openWeatherApiServiceUrl = 'https://' . $apiServiceUri . '/' . $apiServiceType->value . '?' . $apiServiceQueryParam; | ||
|
||
return Http::get($openWeatherApiServiceUrl); | ||
} | ||
|
||
abstract public function getWeatherData(GeolocationResource $geolocation): WeatherResourceInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/app/Services/OpenWeatherApi/Interfaces/WeatherResourceInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\OpenWeatherApi\Interfaces; | ||
|
||
interface WeatherResourceInterface | ||
{ | ||
} |
46 changes: 46 additions & 0 deletions
46
src/app/Services/OpenWeatherApi/Resources/WeatherResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\OpenWeatherApi\Resources; | ||
|
||
use App\Http\Resources\Resource; | ||
use App\Services\OpenWeatherApi\Interfaces\WeatherResourceInterface; | ||
|
||
class WeatherResource extends Resource implements WeatherResourceInterface | ||
{ | ||
public function getResponse(): array | ||
{ | ||
return [ | ||
// Location Info | ||
'country_code' => $this->resource['sys']['country'], | ||
'country_city' => $this->resource['name'], | ||
'timestamp' => $this->resource['dt'], | ||
'timezone' => $this->resource['timezone'], | ||
'coordinate' => [ | ||
'lon' => $this->resource['coord']['lon'], | ||
'lat' => $this->resource['coord']['lat'], | ||
], | ||
|
||
// Current Weather Data | ||
'type' => $this->resource['weather'][0]['main'], // (Rain, Snow, Clouds etc.) | ||
'description' => $this->resource['weather'][0]['description'], | ||
'icon' => $this->resource['weather'][0]['icon'], | ||
|
||
// Main | ||
'temp' => $this->resource['main']['temp'], | ||
'temp_min' => $this->resource['main']['temp_min'], | ||
'temp_max' => $this->resource['main']['temp_max'], | ||
'feels_like' => $this->resource['main']['feels_like'], | ||
'humidity' => $this->resource['main']['humidity'], | ||
|
||
// Wind | ||
'wind_speed' => $this->resource['wind']['speed'], | ||
'wind_direction' => $this->resource['wind']['deg'], | ||
|
||
// sys | ||
'sunrise' => $this->resource['sys']['sunrise'], | ||
'sunset' => $this->resource['sys']['sunset'], | ||
]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/app/Services/OpenWeatherApi/Resources/WeatherResources.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Services\OpenWeatherApi\Resources; | ||
|
||
use App\Http\Resources\Resource; | ||
use App\Services\OpenWeatherApi\Interfaces\WeatherResourceInterface; | ||
|
||
class WeatherResources extends Resource implements WeatherResourceInterface | ||
{ | ||
public function getResponse(): array | ||
{ | ||
$weatherForecast = $this->resource['list']; | ||
|
||
$forecast = []; | ||
foreach ($weatherForecast as $weather) { | ||
$forecast[] = new WeatherResource([ | ||
'coord' => $this->resource['city']['coord'], | ||
'weather' => $weather['weather'], | ||
'main' => $weather['main'], | ||
'wind' => $weather['wind'], | ||
'dt' => $weather['dt'], | ||
'sys' => [ | ||
'country' => $this->resource['city']['country'], | ||
'sunrise' => $this->resource['city']['sunrise'], | ||
'sunset' => $this->resource['city']['sunset'], | ||
], | ||
'timezone' => $this->resource['city']['timezone'], | ||
'name' => $this->resource['city']['name'], | ||
]); | ||
} | ||
|
||
return $forecast; | ||
} | ||
} |
Oops, something went wrong.