Skip to content

Commit

Permalink
Improve API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Nov 16, 2023
1 parent 5d7a6c3 commit 39833f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
15 changes: 9 additions & 6 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,27 @@ Enhetsregisteret

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

.. autoclass:: brreg.enhetsregisteret.Enhet
:members:
:undoc-members:
:exclude-members: model_config, model_fields

.. autoclass:: brreg.enhetsregisteret.Underenhet
:members:
:exclude-members: model_config, model_fields

.. autoclass:: brreg.enhetsregisteret.Adresse
:members:
:undoc-members:
:exclude-members: model_config, model_fields

.. autoclass:: brreg.enhetsregisteret.InstitusjonellSektorkode
:members:
:undoc-members:
:exclude-members: model_config, model_fields

.. autoclass:: brreg.enhetsregisteret.Naeringskode
:members:
:undoc-members:
:exclude-members: model_config, model_fields

.. autoclass:: brreg.enhetsregisteret.Organisasjonsform
:members:
:undoc-members:
:exclude-members: model_config, model_fields
24 changes: 24 additions & 0 deletions src/brreg/enhetsregisteret/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ def error_handler() -> Generator[None, Any, None]:


class Client:
"""Client for the Enhetregisteret API.
Ensures that HTTP connections are reused across requests.
It can be used as a context manager::
with Client() as client:
enhet = client.get_enhet("915501680")
Or by manually opening and closing the client::
client = Client()
enhet = client.get_enhet("915501680")
client.close()
"""

_client: httpx.Client

def __init__(self) -> None:
Expand All @@ -46,11 +62,19 @@ def __exit__(
self.close()

def open(self) -> None:
"""Prepare the client for use.
This is called automatically when the client is created.
"""
self._client = httpx.Client(
base_url="https://data.brreg.no/enhetsregisteret/api",
)

def close(self) -> None:
"""Close the client and any open HTTP connections.
This is called automatically if the client is used as a context manager.
"""
self._client.close()

def get_enhet(self, organisasjonsnummer: str) -> Optional[Enhet]:
Expand Down

0 comments on commit 39833f7

Please sign in to comment.