-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse HTTP connections across requests
- Loading branch information
Showing
5 changed files
with
68 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,65 @@ | ||
from typing import Optional | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Optional | ||
|
||
import httpx | ||
|
||
from brreg import BrregError, BrregRestError | ||
from brreg.enhetsregisteret._types import Enhet | ||
|
||
if TYPE_CHECKING: | ||
from types import TracebackType | ||
|
||
|
||
from ._types import Enhet | ||
class Client: | ||
client: httpx.Client | ||
|
||
BASE_URL = "https://data.brreg.no/enhetsregisteret/api" | ||
def __new__(cls) -> Client: | ||
self = super().__new__(cls) | ||
self.open() | ||
return self | ||
|
||
def __enter__(self) -> Client: | ||
return self | ||
|
||
def get_enhet(organisasjonsnummer: str) -> Optional[Enhet]: | ||
"""Get :class:`Enhet` given an organization number. | ||
def __exit__( | ||
self, | ||
exc_type: type[BaseException] | None = None, | ||
exc_value: BaseException | None = None, | ||
traceback: TracebackType | None = None, | ||
) -> None: | ||
self.close() | ||
|
||
Returns :class:`None` if Enhet is gone or not found | ||
Returns :class:`Enhet` if Enhet is found | ||
def open(self) -> None: | ||
self.client = httpx.Client( | ||
base_url="https://data.brreg.no/enhetsregisteret/api", | ||
) | ||
|
||
Raises :class:`BrregRestException` if a REST exception occures | ||
Raises :class:`BrregException` if an unhandled exception occures | ||
""" | ||
res: Optional[httpx.Response] = None | ||
try: | ||
res = httpx.get(f"{BASE_URL}/enheter/{organisasjonsnummer}") | ||
def close(self) -> None: | ||
self.client.close() | ||
|
||
if res.status_code in (404, 410): | ||
return None | ||
def get_enhet(self, organisasjonsnummer: str) -> Optional[Enhet]: | ||
"""Get :class:`Enhet` given an organization number. | ||
res.raise_for_status() | ||
Returns :class:`None` if Enhet is gone or not found. | ||
Returns :class:`Enhet` if Enhet is found. | ||
return Enhet.from_json(res.json()) | ||
except httpx.HTTPError as exc: | ||
raise BrregRestError( | ||
str(exc), | ||
method=(exc.request.method if exc.request else None), | ||
url=(str(exc.request.url) if exc.request else None), | ||
status_code=(res.status_code if res else None), | ||
) from exc | ||
except Exception as exc: | ||
raise BrregError(exc) from exc | ||
Raises :class:`BrregRestError` if a REST error occurs. | ||
Raises :class:`BrregError` if an unhandled exception occurs. | ||
""" | ||
res: Optional[httpx.Response] = None | ||
try: | ||
res = self.client.get(f"/enheter/{organisasjonsnummer}") | ||
if res.status_code in (404, 410): | ||
return None | ||
res.raise_for_status() | ||
return Enhet.from_json(res.json()) | ||
except httpx.HTTPError as exc: | ||
raise BrregRestError( | ||
str(exc), | ||
method=(exc.request.method if exc.request else None), | ||
url=(str(exc.request.url) if exc.request else None), | ||
status_code=(res.status_code if res else None), | ||
) from exc | ||
except Exception as exc: | ||
raise BrregError(exc) from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters