Skip to content

Commit

Permalink
feat: add user-agent headers on each APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryshu authored and unicodeveloper committed Feb 26, 2024
1 parent 3482900 commit adeb5a5
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 140 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ repos:
pass_filenames: false
additional_dependencies:
- types-requests
- types-setuptools
8 changes: 6 additions & 2 deletions novu/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from json.decoder import JSONDecodeError
from typing import Generic, List, Optional, Type, TypeVar, Union

import pkg_resources
import requests

from novu.config import NovuConfig
Expand All @@ -13,7 +14,7 @@

LOGGER = logging.getLogger(__name__)


__version__ = pkg_resources.get_distribution("novu").version
_C_co = TypeVar("_C_co", bound=CamelCaseDto, covariant=True)


Expand Down Expand Up @@ -101,7 +102,10 @@ def __init__(
api_key = api_key or config.api_key

self._url = url
self._headers = {"Authorization": f"ApiKey {api_key}"}
self._headers = {
"Authorization": f"ApiKey {api_key}",
"User-Agent": f"novu/python@{__version__}",
}

self.requests_timeout = requests_timeout or int(os.getenv("NOVU_PYTHON_REQUESTS_TIMEOUT", "5"))
self.session = session
Expand Down
23 changes: 12 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pytest = "^7.2.0"
sentry-sdk = "^1.14.0"
toml = "^0.10.2"
types-requests = "^2.28.11.12"
types-setuptools = "^69.0.0.0"

[tool.poetry.group.docs.dependencies]
Sphinx = "^7.0.0"
Expand Down
33 changes: 28 additions & 5 deletions tests/api/test_base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from unittest import TestCase, mock

import pkg_resources
from requests.exceptions import HTTPError

from novu.api.base import Api
from novu.config import NovuConfig
from tests.factories import MockResponse

__version__ = pkg_resources.get_distribution("novu").version


class ApiTests(TestCase):
@classmethod
Expand All @@ -23,7 +26,11 @@ def test_handle_request_with_header_override(self, mock_request: mock.MagicMock)
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com",
headers={"Authorization": "ApiKey api-key", "MyHeader": "value"},
headers={
"Authorization": "ApiKey api-key",
"User-Agent": f"novu/python@{__version__}",
"MyHeader": "value",
},
json=None,
params=None,
timeout=5,
Expand All @@ -40,7 +47,11 @@ def test_handle_request_raise_with_details(self, mock_request: mock.MagicMock) -
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com",
headers={"Authorization": "ApiKey api-key", "MyHeader": "value"},
headers={
"Authorization": "ApiKey api-key",
"User-Agent": f"novu/python@{__version__}",
"MyHeader": "value",
},
json=None,
params=None,
timeout=5,
Expand All @@ -57,7 +68,11 @@ def test_handle_request_raise_without_details(self, mock_request: mock.MagicMock
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com",
headers={"Authorization": "ApiKey api-key", "MyHeader": "value"},
headers={
"Authorization": "ApiKey api-key",
"User-Agent": f"novu/python@{__version__}",
"MyHeader": "value",
},
json=None,
params=None,
timeout=5,
Expand All @@ -74,7 +89,11 @@ def test_override_requests_timeout(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com",
headers={"Authorization": "ApiKey api-key", "MyHeader": "value"},
headers={
"Authorization": "ApiKey api-key",
"User-Agent": f"novu/python@{__version__}",
"MyHeader": "value",
},
json=None,
params=None,
timeout=60,
Expand All @@ -93,7 +112,11 @@ def test_use_requests_session(self, mock_request: mock.MagicMock) -> None:
session_mock.request.assert_called_once_with(
method="GET",
url="sample.novu.com",
headers={"Authorization": "ApiKey api-key", "MyHeader": "value"},
headers={
"Authorization": "ApiKey api-key",
"User-Agent": f"novu/python@{__version__}",
"MyHeader": "value",
},
json=None,
params=None,
timeout=5,
Expand Down
8 changes: 6 additions & 2 deletions tests/api/test_blueprint.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from unittest import TestCase, mock

import pkg_resources

from novu.api import BlueprintApi
from novu.config import NovuConfig
from novu.dto import BlueprintDto, GroupedBlueprintDto
from tests.factories import MockResponse

__version__ = pkg_resources.get_distribution("novu").version


class BlueprintApiTests(TestCase):
api: BlueprintApi
Expand Down Expand Up @@ -69,7 +73,7 @@ def test_get_blueprint_by_id(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/blueprints/63dafeda7779f59258e38450",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand All @@ -86,7 +90,7 @@ def test_get_grouped_blueprints(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/blueprints/group-by-category",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand Down
14 changes: 9 additions & 5 deletions tests/api/test_change.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import types
from unittest import TestCase, mock

import pkg_resources

from novu.api import ChangeApi
from novu.config import NovuConfig
from novu.dto.change import ChangeDetailDto, ChangeDto, PaginatedChangeDto
from tests.factories import MockResponse

__version__ = pkg_resources.get_distribution("novu").version


class ChangeApiTests(TestCase):
@classmethod
Expand Down Expand Up @@ -592,7 +596,7 @@ def test_list(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/changes",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params={"promoted": "false"},
timeout=5,
Expand All @@ -609,7 +613,7 @@ def test_list_with_pagination(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/changes",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params={"page": 1, "limit": 10, "promoted": "false"},
timeout=5,
Expand All @@ -625,7 +629,7 @@ def test_count(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/changes/count",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand All @@ -642,7 +646,7 @@ def test_apply(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="POST",
url="sample.novu.com/v1/changes/63e59af2105a61b054458218/apply",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand Down Expand Up @@ -783,7 +787,7 @@ def test_bulk_apply(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="POST",
url="sample.novu.com/v1/changes/bulk/apply",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json={"changeIds": ["63e59af2105a61b054458218", "63e003995fd0df473199a46c"]},
params=None,
timeout=5,
Expand Down
16 changes: 10 additions & 6 deletions tests/api/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import types
from unittest import TestCase, mock

import pkg_resources

from novu.api import EnvironmentApi
from novu.config import NovuConfig
from novu.dto import EnvironmentApiKeyDto, EnvironmentDto, EnvironmentWidgetDto
from tests.factories import MockResponse

__version__ = pkg_resources.get_distribution("novu").version


class EnvironmentApiTests(TestCase):
@classmethod
Expand Down Expand Up @@ -56,7 +60,7 @@ def test_list_environments(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/environments",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand All @@ -73,7 +77,7 @@ def test_create_environment(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="POST",
url="sample.novu.com/v1/environments",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json={"name": "test"},
params=None,
timeout=5,
Expand All @@ -90,7 +94,7 @@ def test_create_environment_with_parent_id(self, mock_request: mock.MagicMock) -
mock_request.assert_called_once_with(
method="POST",
url="sample.novu.com/v1/environments",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json={"name": "test", "parentId": "parent_id"},
params=None,
timeout=5,
Expand All @@ -107,7 +111,7 @@ def test_get_current_environment(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/environments/me",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand All @@ -124,7 +128,7 @@ def test_list_environment_api_keys(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="GET",
url="sample.novu.com/v1/environments/api-keys",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand All @@ -141,7 +145,7 @@ def test_regenerate_api_key(self, mock_request: mock.MagicMock) -> None:
mock_request.assert_called_once_with(
method="POST",
url="sample.novu.com/v1/environments/api-keys/regenerate",
headers={"Authorization": "ApiKey api-key"},
headers={"Authorization": "ApiKey api-key", "User-Agent": f"novu/python@{__version__}"},
json=None,
params=None,
timeout=5,
Expand Down
Loading

0 comments on commit adeb5a5

Please sign in to comment.