Skip to content

Commit d6375ca

Browse files
authored
[TOOLSLIBS-2831] Adds support for arbitrary base_url (#210)
* adds support for arbitrary base_url * adds tests for urls
1 parent 2a6e301 commit d6375ca

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

tests/client/test_urls.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import unittest
2+
3+
from urbanairship.urls import Urls
4+
5+
6+
class TestAirshipUrls(unittest.TestCase):
7+
def setUp(self):
8+
self.test_url = "https://something.tld"
9+
return super().setUp()
10+
11+
def test_no_location(self):
12+
urls = Urls()
13+
# use of apid_url is arbitrary
14+
self.assertIn("urbanairship.com", urls.apid_url)
15+
16+
def test_no_location_oauth(self):
17+
urls = Urls(oauth_base=True)
18+
self.assertIn("asnapius.com", urls.apid_url)
19+
20+
def test_us_location(self):
21+
urls = Urls(location="us")
22+
self.assertIn("urbanairship.com", urls.apid_url)
23+
24+
def test_us_location_oauth(self):
25+
urls = Urls(location="us", oauth_base=True)
26+
self.assertIn("asnapius.com", urls.apid_url)
27+
28+
def test_eu_location(self):
29+
urls = Urls(location="eu")
30+
self.assertIn("airship.eu", urls.apid_url)
31+
32+
def test_eu_location_oauth(self):
33+
urls = Urls(location="eu", oauth_base=True)
34+
self.assertIn("asnapieu.com", urls.apid_url)
35+
36+
def test_base_url(self):
37+
urls = Urls(base_url=self.test_url)
38+
self.assertEqual(urls.base_url, self.test_url)
39+
self.assertIn(self.test_url, urls.apid_url)

urbanairship/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class BaseClient:
3232
May be 'us' or 'eu'. Defaults to 'us'.
3333
:param timeout: [optional] An integer specifying the number of seconds used
3434
for a response timeout threshold
35+
:param base_url: [optional] A string defining an arbitrary base_url to use
36+
for requests to the Airship API. To be used in place of location.
3537
:param retries: [optional] An integer specifying the number of times to retry a
3638
failed request. Retried requests use exponential backoff between requests.
3739
Defaults to 0, no retry.
@@ -43,12 +45,14 @@ def __init__(
4345
location: str = "us",
4446
timeout: int = DEFAULT_REQ_TIMEOUT_S,
4547
retries: int = 0,
48+
base_url: Optional[str] = None,
4649
) -> None:
4750
self.key = key
4851
self.location = location
4952
self.timeout = timeout
5053
self.retries = retries
51-
self.urls: Urls = Urls(self.location)
54+
self.base_url = base_url
55+
self.urls: Urls = Urls(location=self.location, base_url=self.base_url)
5256
self.session = requests.Session()
5357

5458
@property

urbanairship/core.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(
2525
token: Optional[str] = None,
2626
location: str = "us",
2727
timeout: Optional[int] = None,
28+
base_url: Optional[str] = None,
2829
retries: int = 0,
2930
):
3031
"""Main client class for interacting with the Airship API.
@@ -37,14 +38,16 @@ def __init__(
3738
with. Possible values: 'us', 'eu'. Defaults to 'us'.
3839
:param: timeout: [optional] An integer specifying the number of seconds used
3940
for a response timeout threshold
41+
:param base_url: [optional] A string defining an arbitrary base_url to use
42+
for requests to the Airship API. To be used in place of location.
4043
:param retries: [optional] An integer specifying the number of times to retry a
4144
failed request. Retried requests use exponential backoff between requests.
4245
Defaults to 0, no retry.
4346
"""
4447
warnings.warn(
4548
category=DeprecationWarning,
4649
message="The Airship client class is deprecated. Use a client class from \
47-
the client module. This class will be removed in version 7.0",
50+
the client module. This class will be removed in version 8.0",
4851
)
4952

5053
self.key: str = key
@@ -53,7 +56,7 @@ def __init__(
5356
self.location: str = location
5457
self.timeout: Optional[int] = timeout
5558
self.retries = retries
56-
self.urls: Urls = Urls(self.location)
59+
self.urls: Urls = Urls(location=self.location, base_url=base_url)
5760

5861
if all([secret, token]):
5962
raise ValueError("One of token or secret must be used, not both")

urbanairship/urls.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
class Urls:
55
def __init__(
6-
self, location: Optional[str] = None, oauth_base: bool = False
6+
self,
7+
location: Optional[str] = None,
8+
base_url: Optional[str] = None,
9+
oauth_base: bool = False,
710
) -> None:
8-
if not location or location.lower() == "us":
11+
if base_url:
12+
self.base_url = base_url
13+
elif not location or location.lower() == "us":
914
if oauth_base:
1015
self.base_url = "https://api.asnapius.com/api/"
1116
else:

0 commit comments

Comments
 (0)