Skip to content

Commit 718745c

Browse files
committed
Change url and session to be property on AWSClient
1 parent a3148c1 commit 718745c

File tree

5 files changed

+21
-41
lines changed

5 files changed

+21
-41
lines changed

conf/test_settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,9 @@
6969

7070

7171
SIMPLE_JWT['VERIFYING_KEY'] = SIMPLE_JWT['PRIVATE_KEY'].public_key()
72+
73+
URL_SHORTENER_HOST = 'not-really-aws.com'
74+
URL_SHORTENER_URL = 'not-really-aws.com'
75+
76+
IOT_AWS_HOST = 'not-really-aws.com'
77+
IOT_GATEWAY_STAGE = 'test'

src/shipchain_common/aws.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030

3131

3232
class AWSClient:
33+
@property
34+
def url(self):
35+
raise NotImplementedError
36+
37+
@property
38+
def session(self):
39+
raise NotImplementedError
3340

3441
METHOD_POST = 'post'
3542
METHOD_PUT = 'put'
@@ -121,6 +128,8 @@ def _get_generic_endpoint_for_metric(self, http_method, endpoint):
121128

122129

123130
class URLShortenerClient(AWSClient):
131+
url = settings.URL_SHORTENER_URL
132+
session = requests.session()
124133

125134
def __init__(self):
126135
aws_auth = BotoAWSRequestsAuth(
@@ -129,8 +138,6 @@ def __init__(self):
129138
aws_service='execute-api'
130139
)
131140

132-
self.url = settings.URL_SHORTENER_URL
133-
self.session = requests.session()
134141
self.session.headers = {'content-type': 'application/json'}
135142
self.session.auth = aws_auth
136143

src/shipchain_common/iot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828

2929
class AWSIoTClient(AWSClient):
30+
url = f'https://{settings.IOT_AWS_HOST}/{settings.IOT_GATEWAY_STAGE}'
31+
session = requests.session()
3032

3133
def __init__(self):
3234
aws_auth = BotoAWSRequestsAuth(
@@ -35,8 +37,6 @@ def __init__(self):
3537
aws_service='execute-api'
3638
)
3739

38-
self.url = f'https://{settings.IOT_AWS_HOST}/{settings.IOT_GATEWAY_STAGE}'
39-
self.session = requests.session()
4040
self.session.headers = {'content-type': 'application/json'}
4141
self.session.auth = aws_auth
4242

tests/test_aws.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,18 @@
22
from unittest import mock
33

44
import requests
5-
from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
6-
from django.conf import settings
75

86
from src.shipchain_common.exceptions import AWSIoTError
97
from src.shipchain_common.aws import URLShortenerClient
108
from src.shipchain_common.test_utils import mocked_rpc_response
119

1210

13-
@pytest.fixture(scope='module')
14-
def iot_settings():
15-
settings.URL_SHORTENER_HOST = 'not-really-aws.com'
16-
settings.URL_SHORTENER_URL = 'not-really-aws.com'
17-
18-
1911
@pytest.fixture()
20-
def aws_url_client(iot_settings):
12+
def aws_url_client():
2113
return URLShortenerClient()
2214

2315

24-
class FakeBotoAWSRequestsAuth(BotoAWSRequestsAuth):
25-
def __init__(self, *args, **kwargs):
26-
pass
27-
28-
def get_aws_request_headers_handler(self, r):
29-
return {}
30-
31-
32-
def test_init(iot_settings, aws_url_client):
16+
def test_init(aws_url_client):
3317
assert aws_url_client.session.auth.aws_host == 'not-really-aws.com'
3418
assert aws_url_client.session.auth.aws_region == 'us-east-1'
3519
assert aws_url_client.session.auth.service == 'execute-api'

tests/test_iot.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,19 @@
22
from unittest import mock
33

44
import requests
5-
from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
6-
from django.conf import settings
75

86
from src.shipchain_common.exceptions import AWSIoTError
97
from src.shipchain_common.iot import AWSIoTClient
108
from src.shipchain_common.test_utils import mocked_rpc_response
119

1210

13-
@pytest.fixture(scope='module')
14-
def iot_settings():
15-
settings.IOT_AWS_HOST = 'not-really-aws.com'
16-
settings.IOT_GATEWAY_STAGE = 'test'
17-
18-
1911
@pytest.fixture()
20-
def aws_iot_client(iot_settings):
12+
def aws_iot_client():
2113
return AWSIoTClient()
2214

2315

24-
class FakeBotoAWSRequestsAuth(BotoAWSRequestsAuth):
25-
def __init__(self, *args, **kwargs):
26-
pass
27-
28-
def get_aws_request_headers_handler(self, r):
29-
return {}
30-
31-
32-
def test_init(iot_settings, aws_iot_client):
16+
def test_init(aws_iot_client):
3317

34-
assert settings.IOT_GATEWAY_STAGE == 'test'
3518
assert aws_iot_client.session.auth.aws_host == 'not-really-aws.com'
3619
assert aws_iot_client.session.auth.aws_region == 'us-east-1'
3720
assert aws_iot_client.session.auth.service == 'execute-api'

0 commit comments

Comments
 (0)