Skip to content

Commit

Permalink
Merge pull request #59 from crdbrd/user-agent
Browse files Browse the repository at this point in the history
Add python-brreg with version to User-Agent header
  • Loading branch information
jodal authored Aug 18, 2024
2 parents b000d1a + 763dffe commit 7f16ce6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/brreg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""API client for Brønnøysundregistrene's open API."""

__version__ = "1.0.0a1"
from importlib.metadata import ( # pyright: ignore[reportMissingImports]
PackageNotFoundError, # pyright: ignore[reportUnknownVariableType]
version, # pyright: ignore[reportUnknownVariableType]
)

from brreg._exceptions import BrregError, BrregRestError

Expand All @@ -9,3 +12,8 @@
"BrregError",
"BrregRestError",
]

try:
__version__: str = version(__name__) # pyright: ignore[reportUnknownVariableType]
except PackageNotFoundError: # pragma: no cover
__version__ = "unknown"
7 changes: 7 additions & 0 deletions src/brreg/enhetsregisteret/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import httpx

import brreg
from brreg import BrregError, BrregRestError
from brreg.enhetsregisteret._pagination import Cursor, EnhetPage, UnderenhetPage
from brreg.enhetsregisteret._queries import EnhetQuery, UnderenhetQuery
Expand Down Expand Up @@ -59,6 +60,12 @@ def open(self) -> None:
"""
self._client = httpx.Client(
base_url="https://data.brreg.no/enhetsregisteret/api",
headers={
"user-agent": (
f"python-brreg/{brreg.__version__} "
f"python-httpx/{httpx.__version__}"
),
},
)

def close(self) -> None:
Expand Down
8 changes: 8 additions & 0 deletions tests/enhetsregisteret/test_get_enhet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from pytest_httpx import HTTPXMock

import brreg
from brreg import BrregError, BrregRestError, enhetsregisteret

DATA_DIR = Path(__file__).parent.parent / "data"
Expand All @@ -21,6 +22,13 @@ def test_get_enhet(httpx_mock: HTTPXMock) -> None:

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

requests = httpx_mock.get_requests() # pyright: ignore[reportUnknownMemberType]
assert len(requests) == 1
assert (
requests[0].headers["user-agent"]
== f"python-brreg/{brreg.__version__} python-httpx/{httpx.__version__}"
)

assert org is not None
assert org.organisasjonsnummer == "112233445"
assert org.navn == "SESAM STASJON"
Expand Down
9 changes: 9 additions & 0 deletions tests/enhetsregisteret/test_get_roller.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import date
from pathlib import Path

import httpx
import pytest
from pytest_httpx import HTTPXMock

import brreg
from brreg import enhetsregisteret

DATA_DIR = Path(__file__).parent.parent / "data"
Expand All @@ -20,6 +22,13 @@ def test_get_roller_with_person(httpx_mock: HTTPXMock) -> None:

rollegrupper = enhetsregisteret.Client().get_roller("810004622")

requests = httpx_mock.get_requests() # pyright: ignore[reportUnknownMemberType]
assert len(requests) == 1
assert (
requests[0].headers["user-agent"]
== f"python-brreg/{brreg.__version__} python-httpx/{httpx.__version__}"
)

assert rollegrupper

styret = rollegrupper[0]
Expand Down
9 changes: 9 additions & 0 deletions tests/enhetsregisteret/test_get_underenhet.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import date
from pathlib import Path

import httpx
import pytest
from pytest_httpx import HTTPXMock

import brreg
from brreg import enhetsregisteret

DATA_DIR = Path(__file__).parent.parent / "data"
Expand All @@ -20,6 +22,13 @@ def test_get_underenhet(httpx_mock: HTTPXMock) -> None:

org = enhetsregisteret.Client().get_underenhet("776655441")

requests = httpx_mock.get_requests() # pyright: ignore[reportUnknownMemberType]
assert len(requests) == 1
assert (
requests[0].headers["user-agent"]
== f"python-brreg/{brreg.__version__} python-httpx/{httpx.__version__}"
)

assert org is not None
assert org.organisasjonsnummer == "776655441"
assert org.navn == "SESAM STASJON"
Expand Down
11 changes: 10 additions & 1 deletion tests/enhetsregisteret/test_search_enhet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import date
from pathlib import Path

import httpx
from pytest_httpx import HTTPXMock

import brreg
from brreg import enhetsregisteret

DATA_DIR = Path(__file__).parent.parent / "data"
Expand Down Expand Up @@ -52,8 +54,15 @@ def test_search_enhet(httpx_mock: HTTPXMock) -> None:
konkurs=False,
),
)
page = next(cursor.pages)

requests = httpx_mock.get_requests() # pyright: ignore[reportUnknownMemberType]
assert len(requests) == 1
assert (
requests[0].headers["user-agent"]
== f"python-brreg/{brreg.__version__} python-httpx/{httpx.__version__}"
)

page = next(cursor.pages)
assert page.page_size == 1
assert page.page_number == 0
assert page.total_elements == 1
Expand Down
11 changes: 10 additions & 1 deletion tests/enhetsregisteret/test_search_underenhet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import date
from pathlib import Path

import httpx
from pytest_httpx import HTTPXMock

import brreg
from brreg import enhetsregisteret

DATA_DIR = Path(__file__).parent.parent / "data"
Expand Down Expand Up @@ -47,8 +49,15 @@ def test_search_underenhet(httpx_mock: HTTPXMock) -> None:
naeringskode=["90.012"],
),
)
page = next(cursor.pages)

requests = httpx_mock.get_requests() # pyright: ignore[reportUnknownMemberType]
assert len(requests) == 1
assert (
requests[0].headers["user-agent"]
== f"python-brreg/{brreg.__version__} python-httpx/{httpx.__version__}"
)

page = next(cursor.pages)
assert page.page_size == 1
assert page.page_number == 0
assert page.total_elements == 1
Expand Down

0 comments on commit 7f16ce6

Please sign in to comment.