Skip to content

Commit

Permalink
api_client: add a user-agent (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviau authored Sep 15, 2024
1 parent 645cb8a commit 75324f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions flareio/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import typing as t

from flareio.exceptions import TokenError
from flareio.version import __version__ as _flareio_version


class FlareApiClient:
Expand All @@ -34,6 +35,11 @@ def __init__(
def _create_session() -> requests.Session:
session = requests.Session()

# Set User-Agent
session.headers["User-Agent"] = (
f"python-flareio/{_flareio_version} requests/{requests.__version__}"
)

# Don't accept cookies.
session.cookies.set_policy(
policy=DefaultCookiePolicy(
Expand Down
19 changes: 19 additions & 0 deletions flareio/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import importlib.metadata

import typing as t


def _get_version() -> str:
package_name: str = __name__.split(".")[0]
try:
return importlib.metadata.version(package_name)
except importlib.metadata.PackageNotFoundError:
pass
return "unknown"


__version__: t.Final[str] = _get_version()

__all__ = [
"__version__",
]
16 changes: 16 additions & 0 deletions tests/test_api_client_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ def test_get_path_only() -> None:
assert mocker.last_request.url == "https://api.flare.io/hello/test"


def test_get_user_agent() -> None:
client = get_test_client()
with requests_mock.Mocker() as mocker:
mocker.register_uri(
"GET",
"https://api.flare.io/hello/test",
status_code=200,
)
client.get("/hello/test")
assert mocker.last_request.url == "https://api.flare.io/hello/test"
user_agent = mocker.last_request.headers["User-Agent"]

assert "python-flareio/" in user_agent
assert "requests/" in user_agent


def test_bad_domain() -> None:
client = get_test_client()

Expand Down

0 comments on commit 75324f2

Please sign in to comment.