Skip to content

Commit 2cbee8a

Browse files
enh: add user agent to request headers (#29)
1 parent 08e2153 commit 2cbee8a

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

fourinsight/api/authenticate.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from requests.packages.urllib3 import Retry
1919
from requests_oauthlib import OAuth2Session
2020

21+
import fourinsight.api as fapi
22+
2123
from .appdirs import user_data_dir
2224

2325
log = logging.getLogger(__name__)
@@ -141,6 +143,10 @@ def __init__(self, client, auth_force=False, **kwargs):
141143
if self.token_updater:
142144
self.token_updater(token)
143145

146+
self.headers.update(
147+
{"user-agent": f"python-fourinsight-api/{fapi.__version__}"}
148+
)
149+
144150
def fetch_token(self):
145151
"""Fetch new access and refresh token."""
146152
args, kwargs = self._prepare_fetch_token_args()

tests/test_authenticator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pytest
77

8+
import fourinsight.api as fapi
89
from fourinsight.api import authenticate
910

1011
_CONSTANTS = authenticate._CONSTANTS
@@ -187,6 +188,9 @@ def test_init(self, mock_fetch, mock_refresh):
187188
include_client_id=True,
188189
)
189190
assert auth.auto_refresh_url == _CONSTANTS["CLIENT_TOKEN_URL"]
191+
assert (
192+
auth.headers["user-agent"] == f"python-fourinsight-api/{fapi.__version__}"
193+
)
190194

191195
def test_refresh_token(self, mock_fetch, mock_refresh):
192196
auth = authenticate.ClientSession("my_client_id", "my_client_secret")
@@ -244,6 +248,11 @@ def test_init_force_auth(self, mock_token, mock_fetch, mock_refresh):
244248

245249
auth = authenticate.UserSession(auth_force=True)
246250

251+
assert (
252+
auth.headers["user-agent"]
253+
== f"python-fourinsight-api/{fapi.__version__}"
254+
)
255+
247256
mock_fetch.assert_called_once_with(
248257
"my_token_url",
249258
code="my_code",

0 commit comments

Comments
 (0)