Skip to content

Commit

Permalink
Add user agent header to requests (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
vegard-solum-4ss authored Jan 10, 2023
1 parent efb0a21 commit 72825d0
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 46 deletions.
9 changes: 7 additions & 2 deletions fourinsight/campaigns/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pandas as pd

import fourinsight.campaigns as fc


def _dict_rename(dict_org, dict_map):
"""
Expand Down Expand Up @@ -154,6 +156,9 @@ class CampaignsAPI:

def __init__(self, session):
self._session = session
self._headers = {
"user-agent": f"{session.headers['user-agent']} python-fourinsight-campaigns/{fc.__version__}"
}

def _url(self, relative_url, api_version=None):
if api_version is None:
Expand All @@ -168,7 +173,7 @@ def _get_payload(self, url, *args, **kwargs):
next_link = url
payload = []
while next_link:
response = self._session.get(next_link)
response = self._session.get(next_link, headers=self._headers)
response.raise_for_status()
json_response = response.json(*args, **kwargs)
payload.extend(json_response["value"])
Expand All @@ -178,7 +183,7 @@ def _get_payload(self, url, *args, **kwargs):
def _get_payload_legacy(
self, url, *args, **kwargs
): # remove when v1.1 has all endpoints
response = self._session.get(url)
response = self._session.get(url, headers=self._headers)
response.raise_for_status()
return response.json(*args, **kwargs)

Expand Down
11 changes: 9 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ def json_side_effect(*args, **kwargs):


@pytest.fixture
def auth_session(response):
def session_headers():
return {"user-agent": "python-fourinsight-api/v0.0.1"}


@pytest.fixture
def auth_session(response, session_headers):
auth_session = Mock()
auth_session._api_base_url = "test/api/base/url"
auth_session.headers = session_headers

def get_side_effect(url, *args, **kwargs):
response._get_called_with_url = url
Expand All @@ -106,9 +112,10 @@ def get_side_effect(url, *args, **kwargs):


@pytest.fixture
def auth_session_camelcase(response_camelcase):
def auth_session_camelcase(response_camelcase, session_headers):
auth_session = Mock()
auth_session._api_base_url = "test/api/base/url"
auth_session.headers = session_headers

def get_side_effect(url, *args, **kwargs):
response_camelcase._get_called_with_url = url
Expand Down
Loading

0 comments on commit 72825d0

Please sign in to comment.