Skip to content

Commit

Permalink
Reuse HTTP connections across requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Nov 15, 2023
1 parent 66ea1c9 commit d010da9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 39 deletions.
8 changes: 5 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ API reference
Exceptions
==========

.. autoclass:: BrregException
.. autoclass:: BrregError
:members:
:undoc-members:


.. autoclass:: BrregRestException
.. autoclass:: BrregRestError
:members:
:undoc-members:

Expand All @@ -22,7 +22,9 @@ Enhetsregisteret

.. automodule:: brreg.enhetsregisteret

.. autofunction:: brreg.enhetsregisteret.get_enhet
.. autoclass:: brreg.enhetsregisteret.Client
:members:
:undoc-members:

.. autoclass:: brreg.enhetsregisteret.Enhet
:members:
Expand Down
5 changes: 3 additions & 2 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ Organization details by organization number

To get details about an organization ("enhet") given its organization number:

>>> from brreg import enhetsregisteret
>>> enhet = enhetsregisteret.get_enhet('915501680')
>>> from brreg.enhetsregisteret import Client
>>> client = Client()
>>> enhet = client.get_enhet('915501680')
>>> enhet.organisasjonsnummer
'915501680'
>>> enhet.navn
Expand Down
4 changes: 2 additions & 2 deletions src/brreg/enhetsregisteret/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
See https://data.brreg.no/enhetsregisteret/api/docs/index.html for API details.
"""

from brreg.enhetsregisteret._client import get_enhet
from brreg.enhetsregisteret._client import Client
from brreg.enhetsregisteret._types import (
Adresse,
Enhet,
Expand All @@ -14,7 +14,7 @@

__all__ = [
# From _client module:
"get_enhet",
"Client",
# From _types module:
"Adresse",
"Enhet",
Expand Down
78 changes: 52 additions & 26 deletions src/brreg/enhetsregisteret/_client.py
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
12 changes: 6 additions & 6 deletions tests/test_enhetsregisteret.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_get_enhet(
content=organization_details_response,
)

org = enhetsregisteret.get_enhet("818511752")
org = enhetsregisteret.Client().get_enhet("818511752")

assert org is not None
assert org.organisasjonsnummer == "818511752"
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_get_enhet_when_deleted(
content=deleted_organization_details_response,
)

org = enhetsregisteret.get_enhet("815597222")
org = enhetsregisteret.Client().get_enhet("815597222")

assert org is not None
assert org.organisasjonsnummer == "815597222"
Expand All @@ -86,7 +86,7 @@ def test_get_enhet_when_gone(
headers={"content-type": "application/json"},
)

org = enhetsregisteret.get_enhet("818511752")
org = enhetsregisteret.Client().get_enhet("818511752")

assert org is None

Expand All @@ -101,7 +101,7 @@ def test_get_enhet_when_not_found(
headers={"content-type": "application/json"},
)

org = enhetsregisteret.get_enhet("818511752")
org = enhetsregisteret.Client().get_enhet("818511752")

assert org is None

Expand All @@ -117,7 +117,7 @@ def test_get_enhet_when_http_error(
)

with pytest.raises(BrregRestError) as exc_info:
enhetsregisteret.get_enhet("818511752")
enhetsregisteret.Client().get_enhet("818511752")

assert "REST API exception" in str(exc_info.value)
assert "Bad Request" in str(exc_info.value)
Expand All @@ -138,7 +138,7 @@ def test_get_organization_by_number_when_http_timeout(
)

with pytest.raises(BrregRestError) as exc_info:
enhetsregisteret.get_enhet("818511752")
enhetsregisteret.Client().get_enhet("818511752")

assert "REST API exception" in str(exc_info.value)
assert "Connection refused" in str(exc_info.value)
Expand Down

0 comments on commit d010da9

Please sign in to comment.