Skip to content
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

feat: make api independent from each other #28

Merged
merged 1 commit into from
Apr 7, 2024
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
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