-
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.
current and historical api calls work
- Loading branch information
1 parent
af12869
commit 5992fa6
Showing
18 changed files
with
821 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
WeatherStackWrapper | ||
--- | ||
In development wrapper library for [WeatherStack](https://weatherstack.com/) weather api. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
[tool.poetry] | ||
name = "weatherstackwrapper" | ||
version = "0.1.0" | ||
description = "Python wrapper for the weather stack api" | ||
authors = ["Darien Moore <LiskIsBest@gmail.com>"] | ||
license = "GPLv3" | ||
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.10.6" | ||
httpx = "^0.23.3" | ||
pydantic = "^1.10.7" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
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,101 @@ | ||
from .types import Units, Language, Response | ||
from .utility import c_TypeError | ||
|
||
|
||
class BaseWeatherStackApi: | ||
base_url = "https://api.weatherstack.com/" | ||
|
||
def __init__(self, access_key: str): | ||
self.__access_key = access_key | ||
|
||
def current( | ||
self, location: str, units: str | Units = None, language: str | Language = None | ||
) -> Response: | ||
params = {"access_key": self.__access_key} | ||
|
||
if not isinstance(location, str): | ||
raise c_TypeError( | ||
param_name="location", correct="str", wrong=type(location).__name__ | ||
) | ||
params["query"] = location | ||
|
||
if units: | ||
if isinstance(units, Units): | ||
units = units.value | ||
elif not isinstance(units, str): | ||
raise c_TypeError( | ||
param_name="units", correct="str", wrong=type(location).__name__ | ||
) | ||
params["units"] = units | ||
|
||
if language: | ||
if isinstance(language, Language): | ||
language = language.value | ||
elif not isinstance(units, str): | ||
raise c_TypeError( | ||
param_name="units", correct="str", wrong=type(location).__name__ | ||
) | ||
params["language"] = language | ||
|
||
return {"method": "GET", "url": self.base_url + "current", "params": params} | ||
|
||
def historical( | ||
self, | ||
location: str, | ||
historical_date: str, | ||
hourly: bool = None, | ||
interval: int = None, | ||
units: str | Units = None, | ||
language: str | Language = None, | ||
) -> Response: | ||
params = { | ||
"access_key": self.__access_key, | ||
} | ||
|
||
if not isinstance(location, str): | ||
raise c_TypeError( | ||
param_name="location", correct="str", wrong=type(location).__name__ | ||
) | ||
params["query"] = location | ||
|
||
if not isinstance(historical_date, str): | ||
raise c_TypeError( | ||
param_name="historical_date", | ||
correct="str", | ||
wrong=type(location).__name__, | ||
) | ||
params["historical_date"] = historical_date | ||
|
||
if hourly: | ||
if not isinstance(hourly, bool): | ||
raise c_TypeError( | ||
param_name=hourly, correct="bool", wrong=type(hourly).__name | ||
) | ||
params["hourly"] = hourly.__int__() | ||
|
||
if interval: | ||
if not isinstance(interval, int): | ||
raise c_TypeError( | ||
param_name="interval", correct="int", wrong=type(interval).__name__ | ||
) | ||
params["interval"] = interval | ||
|
||
if units: | ||
if isinstance(units, Units): | ||
units = units.value | ||
elif not isinstance(units, str): | ||
raise c_TypeError( | ||
param_name="units", correct="str", wrong=type(location).__name__ | ||
) | ||
params["units"] = units | ||
|
||
if language: | ||
if isinstance(language, Language): | ||
language = language.value | ||
elif not isinstance(units, str): | ||
raise c_TypeError( | ||
param_name="units", correct="str", wrong=type(location).__name__ | ||
) | ||
params["language"] = language | ||
|
||
return {"method": "GET", "url": self.base_url + "historical", "params": params} |
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,2 @@ | ||
import types | ||
from .api import WeatherApi |
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,55 @@ | ||
import functools | ||
|
||
import httpx | ||
from pydantic import parse_obj_as | ||
|
||
from .BaseApi import BaseWeatherStackApi | ||
from .types import Error, Response, Units, Language | ||
|
||
|
||
class WeatherApi(BaseWeatherStackApi): | ||
def __init__(self, access_key: str): | ||
self.Client = httpx.Client(timeout=15.0) | ||
super().__init__(access_key=access_key) | ||
|
||
def request(func): | ||
"""non-async http request""" | ||
|
||
@functools.wraps(func) | ||
def wrapper(self, *args, **kwargs): | ||
data = func(self, *args, **kwargs) | ||
response = self.Client.request( | ||
method=data["method"], | ||
url=data["url"], | ||
params=data["params"], | ||
).json() | ||
if "success" in response: | ||
return parse_obj_as(type_=Error, obj=response) | ||
return parse_obj_as(type_=Response, obj=response) | ||
|
||
return wrapper | ||
|
||
@request | ||
def current( | ||
self, location: str, units: str | Units = None, language: str | Language = None | ||
) -> Response: | ||
return super().current(location=location, units=units, language=language) | ||
|
||
@request | ||
def historical( | ||
self, | ||
location: str, | ||
historical_date: str, | ||
hourly: bool = None, | ||
interval: int = None, | ||
units: str | Units = None, | ||
language: str | Language = None, | ||
) -> Response: | ||
return super().historical( | ||
location=location, | ||
historical_date=historical_date, | ||
hourly=hourly, | ||
interval=interval, | ||
units=units, | ||
language=language, | ||
) |
Oops, something went wrong.