Skip to content

Added version compatibility for differnent services #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"8.1.6","version":"3.8.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"single_trait_insert_per_statement":true},"hashes":{"src\/Weather.php":3396563655,"src\/WeatherFormat.php":1780416015,"src\/WeatherClient.php":2559958320,"src\/WeatherServiceProvider.php":1976251433,"src\/Exceptions\/WeatherException.php":1149541561,"src\/Exceptions\/InvalidConfiguration.php":798640689,"src\/config\/openweather.php":75228949}}
{"php":"8.1.10","version":"3.11.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"single_trait_insert_per_statement":true},"hashes":{"src\/WeatherClient.php":3990504894,"src\/config\/openweather.php":2159394859,"src\/Weather.php":575039304,"src\/WeatherFormat.php":1780416015,"src\/Exceptions\/InvalidConfiguration.php":798640689,"src\/Exceptions\/WeatherException.php":1149541561,"src\/WeatherServiceProvider.php":1976251433}}
248 changes: 102 additions & 146 deletions src/Weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,157 +7,11 @@
*
* @package openweather-laravel-api
* @author Md. Rakibul Islam <[email protected]>
* @version 1.6
* @since 2021-01-09
*/

class Weather
{
/**
* current weather api endpoint : https://api.openweathermap.org/data/2.5/weather.
* See documentation : https://openweathermap.org/current.
*
* @var string
*/

protected $current = 'weather?';

/**
* onecall api endpoint : https://api.openweathermap.org/data/2.5/onecall.
* See documentation : https://openweathermap.org/api/one-call-api
*
* @var string
*/

protected $one_call = 'onecall?';

/**
* hourly forecast 5 Days 3 hour api endpoint : https://api.openweathermap.org/data/2.5/forecast.
* See documentation : https://openweathermap.org/forecast5.
*
* @var string
*/

protected $forecast = 'forecast?';

/**
* last 5 Days history api endpoint : https://api.openweathermap.org/data/2.5/onecall/timemachine.
* See documentation : https://openweathermap.org/api/one-call-api#history
*
* @var string
*/

protected $historical = 'onecall/timemachine?';

/**
* air pollution api endpoint : https://api.openweathermap.org/data/2.5/air_pollution.
* See documentation : https://openweathermap.org/api/air-pollution.
*
* @var string
*/

protected $air_pollution = 'air_pollution?';
/**
* temp_format available strings are c, f, k.
*
* @var string
*/


/**
* Access current weather data for any location on Earth including over 200,000 cities! Open Weathe Map API collect and process weather data from different sources such as global and local weather models, satellites, radars and vast network of weather stations.
* documentation : https://openweathermap.org/current.
*
* @param array $query
*
*/


private function getCurrent(array $query)
{
$data = (new WeatherClient)->client()->fetch($this->current, $query);

return (new WeatherFormat())->formatCurrent($data);
}

/**
* Make just one API call and get all your essential weather data for a specific location with OpenWeather One Call API.
* documentation : https://openweathermap.org/api/one-call-api.
*
* @param array $query
*
*/

private function getOneCall(array $query)
{
$data = (new WeatherClient)->client()->fetch($this->one_call, $query);

return (new WeatherFormat())->formatOneCall($data);
}

/**
* 5 day forecast is available at any location or city. It includes weather forecast data with 3-hour step.
* documentation : https://openweathermap.org/forecast5.
*
* @param array $query
*
*/

private function get3Hourly(array $query)
{
$data = (new WeatherClient)->client()->fetch($this->forecast, $query);

return (new WeatherFormat())->format3Hourly($data);
}

/**
* Historical weather data for the previous 5 days
* documentation : https://openweathermap.org/api/one-call-api#history.
*
* @param array $query
*
*/

private function getHistorical(array $query)
{
$data = (new WeatherClient)->client()->fetch($this->historical, $query);

return (new WeatherFormat())->formatHistorical($data);
}

/**
* Air Pollution API concept
* Air Pollution API provides current, forecast and historical air pollution data for any coordinates on the globe
* Besides basic Air Quality Index, the API returns data about polluting gases, such as Carbon monoxide (CO), Nitrogen monoxide (NO),
* Nitrogen dioxide (NO2), Ozone (O3),Sulphur dioxide (SO2), Ammonia (NH3), and particulates (PM2.5 and PM10).
* Air pollution forecast is available for 5 days with hourly granularity.
* documentation : https://openweathermap.org/api/air-pollution.
*
* @param array $query
*
*/

private function getAirPollution(array $query)
{
$data = (new WeatherClient)->client()->fetch($this->air_pollution, $query);

return (new WeatherFormat())->formatAirPollution($data);
}


/**
* Geocoding API is a simple tool that we have developed to ease the search for locations while working with geographic names and coordinates.
* documentation : https://openweathermap.org/api/geocoding-api.
*
* @param array $query
*
*/

private function getGeo(string $type, array $query)
{
return (new WeatherClient)->client('geo')->fetch($type, $query);
}

public function getCurrentByCity(string $city)
{
if (! is_numeric($city)) {
Expand Down Expand Up @@ -272,4 +126,106 @@ public function getGeoByCord(string $lat, string $lon, string $limit = null)

return $this->getGeo('reverse?', $params);
}

/**
* Access current weather data for any location on Earth including over 200,000 cities!
* Open Weathe Map API collect and process weather data from different sources such as global
* and local weather models, satellites, radars and vast network of weather stations.
*
* Documentation : https://openweathermap.org/current.
*
* @param array $query
*
*/
private function getCurrent(array $query)
{
$ep = 'data/' . config('openweather.weather_api_version', '2.5') . '/weather?';

$data = (new WeatherClient)->client()->fetch($ep, $query);

return (new WeatherFormat())->formatCurrent($data);
}

/**
* Make just one API call and get all your essential weather data for a specific location with OpenWeather One Call API.
* documentation : https://openweathermap.org/api/one-call-api.
*
* @param array $query
*
*/
private function getOneCall(array $query)
{
$ep = 'data/' . config('openweather.onecall_api_version', '2.5') . '/onecall?';
$data = (new WeatherClient)->client()->fetch($ep, $query);

return (new WeatherFormat())->formatOneCall($data);
}

/**
* 5 day forecast is available at any location or city. It includes weather forecast data with 3-hour step.
* documentation : https://openweathermap.org/forecast5.
*
* @param array $query
*
*/
private function get3Hourly(array $query)
{
$ep = 'data/' . config('openweather.forecast_api_version', '2.5') . '/forecast?';
$data = (new WeatherClient)->client()->fetch($ep, $query);

return (new WeatherFormat())->format3Hourly($data);
}

/**
* Historical weather data for the previous 5 days
* documentation : https://openweathermap.org/api/one-call-api#history.
*
* @param array $query
*
*/
private function getHistorical(array $query)
{
$ep = 'data/' . config('openweather.historical_api_version', '2.5') . '/onecall/timemachine?';
$data = (new WeatherClient)->client()->fetch($ep, $query);

return (new WeatherFormat())->formatHistorical($data);
}

/**
* Air Pollution API concept
* Air Pollution API provides current, forecast and historical air pollution data for any coordinates on the globe
* Besides basic Air Quality Index, the API returns data about polluting gases, such as Carbon monoxide (CO), Nitrogen monoxide (NO),
* Nitrogen dioxide (NO2), Ozone (O3),Sulphur dioxide (SO2), Ammonia (NH3), and particulates (PM2.5 and PM10).
* Air pollution forecast is available for 5 days with hourly granularity.
*
* Documentation : https://openweathermap.org/api/air-pollution.
*
* @param array $query
*
*/
private function getAirPollution(array $query)
{
$ep = 'data/' . config('openweather.pollution_api_version', '2.5') . '/air_pollution?';
$data = (new WeatherClient)->client()->fetch($ep, $query);

return (new WeatherFormat())->formatAirPollution($data);
}


/**
* Geocoding API is a simple tool that we have developed to ease the search for locations
* while working with geographic names and coordinates.
*
* Documentation : https://openweathermap.org/api/geocoding-api.
*
* @param string $type
* @param array $query
*
*/
private function getGeo(string $type, array $query)
{
$ep = 'geo/' . config('openweather.geo_api_version', '1.0') . '/' . $type;

return (new WeatherClient)->client()->fetch($ep, $query);
}
}
20 changes: 4 additions & 16 deletions src/WeatherClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,15 @@ class WeatherClient
* @var string
*/

protected $url = 'https://api.openweathermap.org/data/2.5/';

/**
* Geocoding API endpoint : http://api.openweathermap.org/geo/1.0/.
* See documentation : https://openweathermap.org/api/geocoding-api.
*
* @var string
*/

protected $geo_api_url = 'http://api.openweathermap.org/geo/1.0/';

protected $url = 'https://api.openweathermap.org/';

protected $service;

/**
* Units: available units are c, f, k.
*
* For temperature in Fahrenheit (f) and wind speed in miles/hour, use units=imperial
* For temperature in Celsius (c) and wind speed in meter/sec, use units=metric
* Temperature in Kelvin (k) and wind speed in meter/sec is used by default, so there is no need to use the units parameter in the API call if you want this
* For temperature in Celsius (c) and wind speed in meter/sec, use units=metric\
*
* @var array
*/
Expand Down Expand Up @@ -96,11 +85,10 @@ private function buildQueryString(array $params)
}


public function client($type = null)
public function client()
{
$url = $type == 'geo' ? $this->geo_api_url : $this->url;
$this->service = new Client([
'base_uri' => $url,
'base_uri' => $this->url,
'timeout' => 10.0,
]);

Expand Down
51 changes: 45 additions & 6 deletions src/config/openweather.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,64 @@
return [

/**
* Get a free Open Weather Map API key : https://openweathermap.org/price.
* Get a free Open Weather Map API key
* https://openweathermap.org/price.
*
*/

'api_key' => '', // get an API key
'api_key' => env('OPENWAETHER_API_KEY', ""),

/**
* Library https://openweathermap.org/current#multi
*
* Current weather API endpoint : https://api.openweathermap.org/data/2.5/weather.
* See documentation to get the correct version: https://openweathermap.org/current.
*/
'weather_api_version' => '2.5',

/**
* Onecall API endpoint : https://api.openweathermap.org/data/2.5/onecall. Version 3.0 is available now.
* See documentation : https://openweathermap.org/api/one-call-api
*/
'onecall_api_version' => '2.5',

/**
* last 5 Days history API endpoint : https://api.openweathermap.org/data/2.5/onecall/timemachine.
* See documentation : https://openweathermap.org/api/one-call-api#history
*/
'historical_api_version' => '2.5',

/**
* Hourly forecast API endpoint https://api.openweathermap.org/data/2.5/forecast.
* See documentation : https://openweathermap.org/forecast5.
*/
'forecast_api_version' => '2.5',

'lang' => 'en',
/**
* Air pollution api endpoint : https://api.openweathermap.org/data/2.5/air_pollution.
* See documentation : https://openweathermap.org/api/air-pollution.
*/
'polution_api_version' => '2.5',

/**
* Geocoding API: https://openweathermap.org/api/geocoding-api
*/
'geo_api_version' => '1.0',

/**
* Library Configuration
*
* https://openweathermap.org/current#multi
*
*/

'lang' => env('OPENWAETHER_API_LANG', 'en'),
'date_format' => 'm/d/Y',
'time_format' => 'h:i A',
'day_format' => 'l',

/**
* Units: available units are c, f, k. (k is default)
* Unit Configuration
* --------------------------------------
* Available units are c, f, k. (k is default)
*
* For temperature in Fahrenheit (f) and wind speed in miles/hour, use units=imperial
* For temperature in Celsius (c) and wind speed in meter/sec, use units=metric
Expand Down