From 6413ae10e8b2255daee91e142bc0a23579c66f6c Mon Sep 17 00:00:00 2001 From: aaronfriedman Date: Thu, 19 Dec 2024 11:30:09 -0500 Subject: [PATCH] Parse site ID before hitting API --- CHANGELOG.md | 4 ++++ lib/shoppertrak_api_client.py | 3 ++- tests/test_shoppertrak_api_client.py | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7dd9ca..99189fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2024-12-19 -- v1.0.6 +### Fixed +- URL quote special characters in site IDs to escape them + ## 2024-11-21 -- v1.0.5 ### Fixed - Catch non-fatal XML errors and continue requesting. Only throw an error and stop when the API limit has been exceeded, when the request itself has failed, or when the ShopperTrak server cannot be reached even after retrying. diff --git a/lib/shoppertrak_api_client.py b/lib/shoppertrak_api_client.py index ec7240b..70e7dfd 100644 --- a/lib/shoppertrak_api_client.py +++ b/lib/shoppertrak_api_client.py @@ -9,6 +9,7 @@ from nypl_py_utils.functions.log_helper import create_log from requests.auth import HTTPBasicAuth from requests.exceptions import RequestException +from urllib.parse import quote ALL_SITES_ENDPOINT = "allsites" SINGLE_SITE_ENDPOINT = "site/" @@ -41,7 +42,7 @@ def query(self, endpoint, query_date, query_count=1): if the query was successful, b) returns APIStatus.ERROR if the query failed but others should be attempted, or c) waits and tries again if the API was busy. """ - full_url = self.base_url + endpoint + full_url = self.base_url + quote(endpoint) date_str = query_date.strftime("%Y%m%d") self.logger.info(f"Querying {endpoint} for {date_str} data") diff --git a/tests/test_shoppertrak_api_client.py b/tests/test_shoppertrak_api_client.py index b357c63..a3b54a1 100644 --- a/tests/test_shoppertrak_api_client.py +++ b/tests/test_shoppertrak_api_client.py @@ -111,7 +111,7 @@ def test_instance(self): def test_query(self, test_instance, requests_mock, mocker): requests_mock.get( - "https://test_shoppertrak_url/test_endpoint" + "https://test_shoppertrak_url/test%20-%20endpoint%3B%20one" "?date=20231231&increment=15&total_property=N", text=_TEST_API_RESPONSE, ) @@ -122,7 +122,7 @@ def test_query(self, test_instance, requests_mock, mocker): return_value=(APIStatus.SUCCESS, xml_root), ) - assert test_instance.query("test_endpoint", date(2023, 12, 31)) == xml_root + assert test_instance.query("test - endpoint; one", date(2023, 12, 31)) == xml_root mocked_check_response_method.assert_called_once_with(_TEST_API_RESPONSE) def test_query_request_exception(self, test_instance, requests_mock, mocker, caplog):