Skip to content

Commit

Permalink
fixup! set auth/headers in httpx.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioSim committed Dec 5, 2023
1 parent 08f5fd9 commit d342c4c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
13 changes: 4 additions & 9 deletions src/ralph/backends/data/async_lrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def __init__(self, settings: Optional[LRSDataBackendSettings] = None) -> None:
def client(self) -> AsyncClient:
"""Create a `httpx.AsyncClient` if it doesn't exist."""
if not self._client:
self._client = AsyncClient()
headers = self.settings.HEADERS.dict(by_alias=True)
self._client = AsyncClient(auth=self.auth, headers=headers)
return self._client

async def status(self) -> DataBackendStatus:
Expand Down Expand Up @@ -263,11 +264,8 @@ async def close(self) -> None:

async def _fetch_statements(self, target, query_params: dict):
"""Fetch statements from a LRS."""
headers = self.settings.HEADERS.dict(by_alias=True)
while True:
response = await self.client.get(
target, params=query_params, headers=headers, auth=self.auth
)
response = await self.client.get(target, params=query_params)
response.raise_for_status()
statements_response = StatementResponse(**response.json())
statements = statements_response.statements
Expand All @@ -284,11 +282,8 @@ async def _fetch_statements(self, target, query_params: dict):

async def _post_and_raise_for_status(self, target, chunk, ignore_errors):
"""POST chunk of statements to `target` and return the number of insertions."""
headers = self.settings.HEADERS.dict(by_alias=True)
try:
request = await self.client.post(
target, json=chunk, auth=self.auth, headers=headers
)
request = await self.client.post(target, json=chunk)
request.raise_for_status()
return len(chunk)
except HTTPError as error:
Expand Down
13 changes: 4 additions & 9 deletions src/ralph/backends/data/lrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def __init__(self, settings: Optional[LRSDataBackendSettings] = None) -> None:
def client(self) -> Client:
"""Create a `httpx.Client` if it doesn't exist."""
if not self._client:
self._client = Client()
headers = self.settings.HEADERS.dict(by_alias=True)
self._client = Client(auth=self.auth, headers=headers)
return self._client

def status(self) -> DataBackendStatus:
Expand Down Expand Up @@ -287,11 +288,8 @@ def close(self) -> None:

def _fetch_statements(self, target, query_params: dict):
"""Fetch statements from a LRS."""
headers = self.settings.HEADERS.dict(by_alias=True)
while True:
response = self.client.get(
target, params=query_params, headers=headers, auth=self.auth
)
response = self.client.get(target, params=query_params)
response.raise_for_status()
statements_response = StatementResponse(**response.json())
statements = statements_response.statements
Expand All @@ -308,11 +306,8 @@ def _fetch_statements(self, target, query_params: dict):

def _post_and_raise_for_status(self, target, chunk, ignore_errors):
"""POST chunk of statements to `target` and return the number of insertions."""
headers = self.settings.HEADERS.dict(by_alias=True)
try:
request = self.client.post(
target, content=chunk, auth=self.auth, headers=headers
)
request = self.client.post(target, content=chunk)
request.raise_for_status()
return len(chunk)
except HTTPError as error:
Expand Down

0 comments on commit d342c4c

Please sign in to comment.