Skip to content

Commit

Permalink
current and historical api calls work
Browse files Browse the repository at this point in the history
  • Loading branch information
LiskIsBest committed Mar 28, 2023
1 parent af12869 commit 5992fa6
Show file tree
Hide file tree
Showing 18 changed files with 821 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
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.
204 changes: 204 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pyproject.toml
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"
101 changes: 101 additions & 0 deletions weatherstackwrapper/BaseApi.py
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}
2 changes: 2 additions & 0 deletions weatherstackwrapper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import types
from .api import WeatherApi
55 changes: 55 additions & 0 deletions weatherstackwrapper/api.py
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,
)
Loading

0 comments on commit 5992fa6

Please sign in to comment.