Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API-98] Send User-Agent header on API requests #14

Merged
merged 10 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ ENV/

# OS generated files
.DS_Store
.idea/
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.2
1.1.3
2 changes: 2 additions & 0 deletions bynder_sdk/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def __init__(self, bynder_domain, *args, **kwargs):

super().__init__(*args, **kwargs)

self._set_ua_header()

def authorization_url(self):
state = ''.join([
random.choice(string.ascii_letters + string.digits)
Expand Down
4 changes: 3 additions & 1 deletion bynder_sdk/permanent_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ def __init__(self, bynder_domain, permanent_token):

self.bynder_domain = bynder_domain
self.headers.update({
'Authorization': 'Bearer {}'.format(permanent_token)
'Authorization': 'Bearer {}'.format(permanent_token),
})

self._set_ua_header()
13 changes: 12 additions & 1 deletion bynder_sdk/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
with open('VERSION') as fh:
SDK_VERSION = fh.read().strip()

UA_HEADER = {
'User-Agent': 'bynder-python-sdk/{}'.format(SDK_VERSION)
}


def api_endpoint_url(session, endpoint):
return 'https://{}/api{}'.format(session.bynder_domain, endpoint)

Expand All @@ -12,7 +20,6 @@ def parse_json_for_response(response):
class SessionMixin:
betacar marked this conversation as resolved.
Show resolved Hide resolved
def wrapped_request(self, func, endpoint, *args, **kwargs):
endpoint = api_endpoint_url(self, endpoint)

response = func(endpoint, *args, **kwargs)
response.raise_for_status()

Expand All @@ -26,10 +33,14 @@ def post(self, url, *args, **kwargs):
# Do not send the Authorization header to S3
kwargs['headers'] = {'Authorization': None}
return super().post(url, *args, **kwargs)

return self.wrapped_request(super().post, url, *args, **kwargs)

def put(self, url, *args, **kwargs):
return self.wrapped_request(super().put, url, *args, **kwargs)

def delete(self, url, *args, **kwargs):
return self.wrapped_request(super().delete, url, *args, **kwargs)

def _set_ua_header(self):
self.headers.update(UA_HEADER)
6 changes: 5 additions & 1 deletion test/oauth2_session_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import mock, TestCase

from bynder_sdk.oauth2 import BynderOAuth2Session, oauth2_url
from bynder_sdk.util import api_endpoint_url
from bynder_sdk.util import api_endpoint_url, UA_HEADER


TEST_DOMAIN = 'test.getbynder.com'
Expand Down Expand Up @@ -45,3 +45,7 @@ def test_fetch_token(self, mocked_func):
include_client_id=True,
code='code',
)

def test_user_agent_header(self):
# The UA header is contained within the session headers
assert UA_HEADER.items() <= self.session.headers.items()