Skip to content

Commit 8afb491

Browse files
authored
Merge pull request #15 from notificationapi-com/q9aa0Qjs/2191-libraries-query-log
Chore: add query_logs
2 parents a5d9bd7 + 76405ce commit 8afb491

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

notificationapi_python_server_sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
__author__ = """Sahand Seifi"""
44
__email__ = "[email protected]"
5-
__version__ = "1.1.0"
5+
__version__ = "1.2.0"

notificationapi_python_server_sdk/notificationapi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,8 @@ async def identify_user(params):
106106
custom_auth = 'Basic ' + base64.b64encode(f'{__client_id}:{user_id}:{hashed_user_id_base64}'.encode()).decode()
107107

108108
await request('POST', f'users/{urllib.parse.quote(user_id)}', params, custom_auth)
109+
110+
111+
async def query_logs(params):
112+
response = await request("POST", "logs/query", params)
113+
return response

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.1.0
2+
current_version = 1.2.0
33
commit = True
44
tag = True
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@
5252
test_suite="tests",
5353
tests_require=test_requirements,
5454
url="https://github.com/notificationapi-com/notificationapi_python_server_sdk",
55-
version="1.1.0",
55+
version="1.2.0",
5656
zip_safe=False,
5757
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
3+
"""Tests for `notificationapi_python_server_sdk` package."""
4+
5+
import pytest
6+
import json
7+
from httpx import Response
8+
from notificationapi_python_server_sdk import notificationapi
9+
10+
client_id = "client_id"
11+
client_secret = "client_secret"
12+
api_paths = {
13+
"query_logs": f"https://api.notificationapi.com/{client_id}/logs/query",
14+
}
15+
16+
query_logs_params = {
17+
"dateRangeFilter": {
18+
"startTime": 1719600830559,
19+
"endTime": 1719600840559
20+
},
21+
"notificationFilter": ["order_tracking"],
22+
"channelFilter": ["EMAIL"],
23+
"userFilter": ["abcd-1234"],
24+
"statusFilter": ["SUCCESS"],
25+
"trackingIds": ["172cf2f4-18cd-4f1f-b2ac-e50c7d71891c"],
26+
"requestFilter": ['request.mergeTags.item="Krabby Patty Burger"'],
27+
"envIdFilter": ["6ok6imq9unr2budgiebjdaa6oi"]
28+
}
29+
30+
31+
@pytest.mark.asyncio
32+
@pytest.mark.parametrize(
33+
"func,params",
34+
[
35+
("query_logs", query_logs_params),
36+
],
37+
)
38+
async def test_makes_one_post_api_call(respx_mock, func, params):
39+
route = respx_mock.post(api_paths[func]).mock(return_value=Response(200))
40+
notificationapi.init(client_id, client_secret)
41+
await getattr(notificationapi, func)(params)
42+
assert route.called
43+
44+
45+
@pytest.mark.asyncio
46+
@pytest.mark.parametrize(
47+
"func,params",
48+
[
49+
("query_logs", query_logs_params),
50+
],
51+
)
52+
async def test_passes_params_as_json_body(respx_mock, func, params):
53+
route = respx_mock.post(api_paths[func]).mock(return_value=Response(200))
54+
notificationapi.init(client_id, client_secret)
55+
await getattr(notificationapi, func)(params)
56+
assert json.loads(route.calls.last.request.content) == params
57+
58+
59+
@pytest.mark.asyncio
60+
@pytest.mark.parametrize(
61+
"func,params",
62+
[
63+
("query_logs", query_logs_params),
64+
],
65+
)
66+
async def test_logs_on_202(respx_mock, caplog, func, params):
67+
respx_mock.post(api_paths[func]).mock(return_value=Response(202))
68+
notificationapi.init(client_id, client_secret)
69+
await getattr(notificationapi, func)(params)
70+
assert "NotificationAPI request ignored." in caplog.text
71+
72+
73+
@pytest.mark.asyncio
74+
@pytest.mark.parametrize(
75+
"func,params",
76+
[
77+
("query_logs", query_logs_params),
78+
],
79+
)
80+
async def test_logs_and_throws_on_500(respx_mock, caplog, func, params):
81+
respx_mock.post(api_paths[func]).mock(return_value=Response(500, text="big oof 500"))
82+
notificationapi.init(client_id, client_secret)
83+
await getattr(notificationapi, func)(params)
84+
assert "NotificationAPI request failed. Response: big oof 500" in caplog.text

0 commit comments

Comments
 (0)