Skip to content

Commit

Permalink
Make API independent from each other (#28)
Browse files Browse the repository at this point in the history
This will make all API could be called using `async with` style rather
than dependency with the `BMKG` class. This also make API more flexible.
  • Loading branch information
kiraware authored Apr 7, 2024
1 parent 3baf0ab commit 3dd019c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
18 changes: 18 additions & 0 deletions src/bmkg/api/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from traceback import TracebackException
from types import TracebackType
from typing import Self

from aiohttp import ClientSession

__all__ = ["API"]
Expand All @@ -12,3 +16,17 @@ def __init__(self, session: ClientSession | None = None) -> None:
self._session = (
session if session is not None else ClientSession("https://data.bmkg.go.id")
)

async def __aenter__(self) -> Self:
return self

async def __aexit__(
self,
exc_type: Exception,
exc_val: TracebackException,
traceback: TracebackType,
) -> None:
await self.close()

async def close(self) -> None:
await self._session.close()
22 changes: 3 additions & 19 deletions src/bmkg/api/bmkg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from traceback import TracebackException
from types import TracebackType
from typing import Self
from aiohttp import ClientSession

from .api import API
from .earthquake import Earthquake
Expand All @@ -18,22 +16,8 @@ class BMKG(API):
weather_forecast (WeatherForecast): weather forecast api interface.
"""

def __init__(self) -> None:
API.__init__(self)
def __init__(self, session: ClientSession | None = None) -> None:
API.__init__(self, session)

self.earthquake = Earthquake(self._session)
self.weather_forecast = WeatherForecast(self._session)

async def __aenter__(self) -> Self:
return self

async def __aexit__(
self,
exc_type: Exception,
exc_val: TracebackException,
traceback: TracebackType,
) -> None:
await self.close()

async def close(self) -> None:
await self._session.close()
2 changes: 1 addition & 1 deletion src/bmkg/api/earthquake.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def get_latest_earthquake(self) -> schemas.LatestEarthquake:
latest_earthquake = parse_latest_earthquake_data(await response.json()) # type: ignore

latest_earthquake.shakemap = Shakemap(
self._session, latest_earthquake.shakemap.file_name
latest_earthquake.shakemap.file_name, self._session
)

return latest_earthquake
Expand Down
2 changes: 1 addition & 1 deletion src/bmkg/api/shakemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Shakemap(API, schemas.Shakemap):

url = "/DataMKG/TEWS"

def __init__(self, session: ClientSession, file_name: str) -> None:
def __init__(self, file_name: str, session: ClientSession | None = None) -> None:
API.__init__(self, session)
schemas.Shakemap.__init__(self, file_name)

Expand Down

0 comments on commit 3dd019c

Please sign in to comment.