Skip to content

Commit

Permalink
fix: async initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
brendancsmith committed Apr 26, 2024
1 parent 4e65c7e commit 8ce932f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
23 changes: 20 additions & 3 deletions src/diffbot_kg/clients/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,40 @@ class DiffbotSession:
"""

def __init__(self) -> None:
headers = {"accept": "application/json"}
timeout = aiohttp.ClientTimeout(total=60, sock_connect=5)
self._session = aiohttp.ClientSession(headers=headers, timeout=timeout)
self._headers = {"accept": "application/json"}
self._timeout = aiohttp.ClientTimeout(total=60, sock_connect=5)

self.is_open = False

async def open(self) -> Self:
self._session = aiohttp.ClientSession(headers=self._headers, timeout=self._timeout)
self._limiter = aiolimiter.AsyncLimiter(max_rate=5, time_period=1)

self.is_open = True
return self

async def get(self, url, **kwargs) -> BaseDiffbotResponse:
if not self.is_open:
await self.open()

# sourcery skip: inline-immediately-returned-variable
resp = await self._request(HTTPMethod.GET, url, **kwargs)
return resp

async def post(self, url, **kwargs) -> BaseDiffbotResponse:
if not self.is_open:
await self.open()

# sourcery skip: inline-immediately-returned-variable
resp = await self._request(HTTPMethod.POST, url, **kwargs)
return resp

async def close(self) -> None:
if not self._session.closed:
await self._session.close()

self.is_open = False

@retry(
retry=retry_if_exception_type(RetryableException),
reraise=True,
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/clients/test_enhance_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from diffbot_kg.clients.enhance import DiffbotEnhanceClient
from diffbot_kg.clients.session import DiffbotSession
from diffbot_kg.models.response import DiffbotEntitiesResponse
Expand Down Expand Up @@ -31,9 +30,9 @@ async def test_mocked_enhance(self, mocker, client):
response = await client.enhance(params)

# ASSERT
assert DiffbotSession.get.is_called_with(
DiffbotSession.get.assert_called_with(
DiffbotEnhanceClient.enhance_url,
params=params,
params={**params, "token": "valid_token"},
headers={"accept": "application/json"},
)
assert isinstance(response, DiffbotEntitiesResponse)
Expand All @@ -57,10 +56,11 @@ async def test_mocked_create_bulkjob(self, mocker, client):
response = await client.create_bulkjob(params)

# ASSERT
assert DiffbotSession.post.is_called_with(
DiffbotSession.post.assert_called_with(
DiffbotEnhanceClient.bulk_job_url,
params=params,
headers={"accept": "application/json"},
params={"token": "valid_token"},
headers={"content-type": "application/json", "accept": "application/json"},
json=params
)
assert isinstance(response, DiffbotBulkJobCreateResponse)
assert response.status == 202
1 change: 0 additions & 1 deletion tests/unit/clients/test_search_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from diffbot_kg.clients.search import DiffbotSearchClient
from diffbot_kg.clients.session import DiffbotSession
from diffbot_kg.models.response import DiffbotEntitiesResponse
Expand Down

0 comments on commit 8ce932f

Please sign in to comment.