Skip to content

Commit 153e075

Browse files
authored
Merge pull request #21 from notificationapi-com/IJetJhKv/2974-not-all-send-code-samples-change-for-the-eu-region
Update SDK to work with CA, EU and US (default) regions
2 parents f43e007 + 636eabb commit 153e075

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

notificationapi_python_server_sdk/__init__.py

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

33
__author__ = """Sahand Seifi"""
44
__email__ = "[email protected]"
5-
__version__ = "1.2.0"
5+
__version__ = "2.0.0"
6+
7+
# Region constants
8+
US_REGION = "https://api.notificationapi.com"
9+
EU_REGION = "https://api.eu.notificationapi.com"
10+
CA_REGION = "https://api.ca.notificationapi.com"

notificationapi_python_server_sdk/notificationapi.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import hashlib
44
import base64
55
import urllib.parse
6+
from . import US_REGION
67

78
__client_id = ""
89
__client_secret = ""
10+
__base_url = US_REGION
911

1012

11-
def init(client_id, client_secret):
13+
def init(client_id, client_secret, base_url=None):
1214
if not client_id:
1315
raise Exception("Bad client_id")
1416

@@ -19,10 +21,15 @@ def init(client_id, client_secret):
1921
__client_id = client_id
2022
global __client_secret
2123
__client_secret = client_secret
24+
global __base_url
25+
if base_url:
26+
__base_url = base_url
27+
else:
28+
__base_url = US_REGION
2229

2330

2431
async def request(method, uri, data=None, custom_auth=None, queryStrings=None):
25-
api_url = "https://api.notificationapi.com/" + __client_id + "/" + uri
32+
api_url = f"{__base_url}/{__client_id}/{uri}"
2633

2734
headers = {}
2835
if custom_auth:
@@ -46,6 +53,7 @@ async def request(method, uri, data=None, custom_auth=None, queryStrings=None):
4653
response.text,
4754
data,
4855
)
56+
return response
4957

5058

5159
async def send(params):

setup.cfg

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

@@ -23,4 +23,3 @@ test = pytest
2323

2424
[tool:pytest]
2525
collect_ignore = ['setup.py']
26-

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.2.0",
55+
version="2.0.0",
5656
zip_safe=False,
5757
)

tests/test_notificationapi_regions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
"""Tests for region support in `notificationapi_python_server_sdk` package."""
4+
5+
import pytest
6+
import httpx
7+
from notificationapi_python_server_sdk import notificationapi, US_REGION, EU_REGION, CA_REGION
8+
9+
client_id = "client_id"
10+
client_secret = "client_secret"
11+
12+
13+
def test_init_with_default_region():
14+
notificationapi.init(client_id, client_secret)
15+
# Access the private variable directly - it's defined at module level
16+
assert notificationapi.__base_url == US_REGION
17+
18+
19+
def test_init_with_eu_region():
20+
notificationapi.init(client_id, client_secret, EU_REGION)
21+
assert notificationapi.__base_url == EU_REGION
22+
23+
24+
def test_init_with_ca_region():
25+
notificationapi.init(client_id, client_secret, CA_REGION)
26+
assert notificationapi.__base_url == CA_REGION
27+
28+
29+
@pytest.mark.asyncio
30+
async def test_request_uses_correct_region_url(respx_mock):
31+
# Test with EU region
32+
eu_api_url = f"{EU_REGION}/{client_id}/sender"
33+
route = respx_mock.post(eu_api_url).mock(return_value=httpx.Response(200))
34+
35+
notificationapi.init(client_id, client_secret, EU_REGION)
36+
await notificationapi.send({"notificationId": "test", "user": {"id": "user1"}})
37+
38+
assert route.called
39+
assert route.calls.last.request.url == eu_api_url

tests/test_notificationapi_send.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
import json
77
from httpx import Response
8-
from notificationapi_python_server_sdk import notificationapi
8+
from notificationapi_python_server_sdk import notificationapi, US_REGION
99

1010
client_id = "client_id"
1111
client_secret = "client_secret"
@@ -17,7 +17,7 @@
1717
userId = "userId"
1818
notification_id = "notification_id"
1919
api_paths = {
20-
"send": f"https://api.notificationapi.com/{client_id}/sender",
20+
"send": f"{US_REGION}/{client_id}/sender",
2121
}
2222

2323

0 commit comments

Comments
 (0)