Skip to content

Commit 7a1642b

Browse files
committed
Merge branch 'qa' into production
2 parents 7a71593 + c0a7257 commit 7a1642b

File tree

3 files changed

+36
-44
lines changed

3 files changed

+36
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## v1.6.3 1/27/25
3+
- Add capability to pull cloudLibrary events by the millisecond
4+
25
## v1.6.2 12/2/24
36
- Add record_num capability to patron_data_helper
47

src/nypl_py_utils/classes/cloudlibrary_client.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import hmac
44
import requests
55

6-
from datetime import datetime, timedelta, timezone
6+
from datetime import datetime, timezone
77
from nypl_py_utils.functions.log_helper import create_log
88
from requests.adapters import HTTPAdapter, Retry
99

@@ -35,28 +35,25 @@ def get_library_events(self, start_date=None,
3535
optional timeframe. Pulls past 24 hours of events by default.
3636
3737
start_date and end_date are optional parameters, and must be
38-
formatted either YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS
38+
formatted either YYYY-MM-DD, YYYY-MM-DD:SS, or YYYY-MM-DDTHH:MM:SS.fff
3939
"""
40-
date_format = "%Y-%m-%dT%H:%M:%S"
41-
today = datetime.now(timezone.utc)
42-
yesterday = today - timedelta(1)
43-
start_date = datetime.strftime(
44-
yesterday, date_format) if start_date is None else start_date
45-
end_date = datetime.strftime(
46-
today, date_format) if end_date is None else end_date
47-
48-
if (datetime.strptime(start_date, date_format) >
49-
datetime.strptime(end_date, date_format)):
50-
error_message = (f"Start date {start_date} greater than end date "
51-
f"{end_date}, cannot retrieve library events")
52-
self.logger.error(error_message)
53-
raise CloudLibraryClientError(error_message)
54-
55-
self.logger.info(
56-
(f"Fetching all library events in "
57-
f"time frame {start_date} to {end_date}..."))
40+
path = "data/cloudevents"
41+
if None not in (start_date, end_date):
42+
if (self._parse_event_date(start_date) >
43+
self._parse_event_date(end_date)):
44+
error_message = (f"Start date {start_date} greater than end "
45+
f"date {end_date}, cannot retrieve library "
46+
f"events")
47+
self.logger.error(error_message)
48+
raise CloudLibraryClientError(error_message)
49+
50+
path += f"?startdate={start_date}&enddate={end_date}"
51+
self.logger.info(f"Fetching all library events in "
52+
f"time frame {start_date} to {end_date}...")
53+
else:
54+
self.logger.info("Fetching all library events "
55+
"from the past day...")
5856

59-
path = f"data/cloudevents?startdate={start_date}&enddate={end_date}"
6057
response = self.request(path=path, method_type="GET")
6158
return response
6259

@@ -66,7 +63,7 @@ def create_request_body(self, request_type,
6663
Helper function to generate request body when performing item
6764
and/or patron-specific functions (ex. checking out a title).
6865
"""
69-
request_template = "<%(request_type)s><ItemId>%(item_id)s</ItemId><PatronId>%(patron_id)s</PatronId></%(request_type)s>" # noqa
66+
request_template = "<%(request_type)s><ItemId>%(item_id)s</ItemId><PatronId>%(patron_id)s</PatronId></%(request_type)s>" # noqa
7067
return request_template % {
7168
"request_type": request_type,
7269
"item_id": item_id,
@@ -113,6 +110,16 @@ def request(self, path, method_type="GET",
113110

114111
return response
115112

113+
def _parse_event_date(self, event_date) -> datetime:
114+
for fmt in ("%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S.%f"):
115+
try:
116+
return datetime.strptime(event_date, fmt)
117+
except ValueError:
118+
pass
119+
error_message = (f"Invalid date format found: {event_date}")
120+
self.logger.error(error_message)
121+
raise CloudLibraryClientError(error_message)
122+
116123
def _build_headers(self, method_type, path) -> dict:
117124
time, authorization = self._build_authorization(
118125
method_type, path)

tests/test_cloudlibrary_client.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ def test_instance(self):
4141

4242
def test_get_library_events_success_no_args(
4343
self, test_instance, mocker):
44-
start = "2024-11-10T10:00:00"
45-
end = "2024-11-11T10:00:00"
4644
mock_request = mocker.patch(
47-
"nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa
48-
return_value=_TEST_LIBRARY_EVENTS_RESPONSE)
49-
response = test_instance.get_library_events()
45+
"nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request") # noqa
46+
test_instance.get_library_events()
5047

5148
mock_request.assert_called_once_with(
52-
path=f"data/cloudevents?startdate={start}&enddate={end}",
49+
path="data/cloudevents",
5350
method_type="GET")
54-
assert response == _TEST_LIBRARY_EVENTS_RESPONSE
5551

5652
def test_get_library_events_success_with_start_and_end_date(
5753
self, test_instance, mocker):
@@ -67,20 +63,6 @@ def test_get_library_events_success_with_start_and_end_date(
6763
method_type="GET")
6864
assert response == _TEST_LIBRARY_EVENTS_RESPONSE
6965

70-
def test_get_library_events_success_with_no_end_date(
71-
self, test_instance, mocker):
72-
start = "2024-11-01T09:00:00"
73-
end = "2024-11-11T10:00:00"
74-
mock_request = mocker.patch(
75-
"nypl_py_utils.classes.cloudlibrary_client.CloudLibraryClient.request", # noqa
76-
return_value=_TEST_LIBRARY_EVENTS_RESPONSE)
77-
response = test_instance.get_library_events(start)
78-
79-
mock_request.assert_called_once_with(
80-
path=f"data/cloudevents?startdate={start}&enddate={end}",
81-
method_type="GET")
82-
assert response == _TEST_LIBRARY_EVENTS_RESPONSE
83-
8466
def test_get_library_events_exception_when_start_date_greater_than_end(
8567
self, test_instance, caplog):
8668
start = "2024-11-11T09:00:00"
@@ -103,7 +85,7 @@ def test_get_library_events_exception_when_connection_timeout(
10385
url, exc=ConnectTimeout)
10486

10587
with pytest.raises(CloudLibraryClientError):
106-
test_instance.get_library_events()
88+
test_instance.get_library_events(start, end)
10789
assert (f"Failed to retrieve response from {url}") in caplog.text
10890

10991
def test_get_request_success(self, test_instance, requests_mock):

0 commit comments

Comments
 (0)