diff --git a/src/ralph/backends/data/async_lrs.py b/src/ralph/backends/data/async_lrs.py index 7d74e4527..8c8102144 100644 --- a/src/ralph/backends/data/async_lrs.py +++ b/src/ralph/backends/data/async_lrs.py @@ -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: @@ -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 @@ -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: diff --git a/src/ralph/backends/data/lrs.py b/src/ralph/backends/data/lrs.py index 2dbf396f9..479321692 100644 --- a/src/ralph/backends/data/lrs.py +++ b/src/ralph/backends/data/lrs.py @@ -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: @@ -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 @@ -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: