Skip to content

Commit

Permalink
Parse site ID before hitting API
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfriedman6 committed Dec 19, 2024
1 parent d61d199 commit 6413ae1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/shoppertrak_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_shoppertrak_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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):
Expand Down

0 comments on commit 6413ae1

Please sign in to comment.