diff --git a/Dockerfile b/Dockerfile index 4c0fab4..111c0f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7-alpine3.12 as BACKEND +FROM python:3.8-alpine3.12 as BACKEND RUN apk add --update \ && apk add --no-cache build-base curl-dev linux-headers bash git\ @@ -13,7 +13,7 @@ RUN pip install --upgrade pip\ && pip install -r /root/swift_sharing/requirements.txt \ && pip install /root/swift_sharing -FROM python:3.7-alpine3.12 +FROM python:3.8-alpine3.12 RUN apk add --no-cache --update bash @@ -21,7 +21,7 @@ LABEL maintainer "CSC Developers" LABEL org.label-schema.schema-version="1.0" LABEL org.label-schema.vcs-url="https://github.com/CSCFI/swift-x-account-sharing" -COPY --from=BACKEND /usr/local/lib/python3.7 /usr/local/lib/python3.7/ +COPY --from=BACKEND /usr/local/lib/python3.8 /usr/local/lib/python3.8/ COPY --from=BACKEND /usr/local/bin/gunicorn /usr/local/bin/ diff --git a/requirements.txt b/requirements.txt index e1369b1..e2c284f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ aiohttp uvloop asyncpg gunicorn>=20.0.1 +certifi \ No newline at end of file diff --git a/setup.py b/setup.py index e6184e6..bb2088b 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ "aiohttp", "uvloop", "asyncpg", + "certifi" ], extras_require={ "test": ["tox", "pytest", "pytest-cov", "coverage", "flake8", @@ -36,8 +37,8 @@ "Topic :: Internet :: WWW/HTTP :: HTTP Servers", "License :: OSI Approved :: MIT License", - - "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", ], ) diff --git a/swift_x_account_sharing/__init__.py b/swift_x_account_sharing/__init__.py index 97e5415..1065eec 100644 --- a/swift_x_account_sharing/__init__.py +++ b/swift_x_account_sharing/__init__.py @@ -2,6 +2,6 @@ __name__ = "swift_x_account_sharing" -__version__ = "0.5.8" +__version__ = "0.5.9" __author__ = "CSC Developers" __license__ = "MIT License" diff --git a/swift_x_account_sharing/bindings/bind.py b/swift_x_account_sharing/bindings/bind.py index bcee515..6834c4f 100644 --- a/swift_x_account_sharing/bindings/bind.py +++ b/swift_x_account_sharing/bindings/bind.py @@ -3,11 +3,18 @@ import json import typing - +import logging import aiohttp from .signature import sign_api_request +import ssl +import certifi + + +ssl_context = ssl.create_default_context() +ssl_context.load_verify_locations(certifi.where()) + class SwiftXAccountSharing: """Swift X Account Sharing backend client.""" @@ -28,6 +35,26 @@ async def __aexit__(self, *excinfo: BaseException) -> None: """.""" await self.session.close() + async def _handler_response( + self, + resp: aiohttp.ClientResponse + ) -> typing.Any: + """Handle API response.""" + if resp.status == 200: + try: + return json.loads(await resp.text()) + except json.decoder.JSONDecodeError: + logging.error("Decoding JSON error \ + response was not possible.") + raise + except Exception as e: + logging.error(f"Unknown exception \ + occured with content: {e}.") + raise + else: + logging.error(f"response status: {resp.status}.") + raise Exception(f"response status: {resp.status}.") + @staticmethod def parse_list_to_string( to_parse: typing.List[str] @@ -48,8 +75,11 @@ async def get_access( params = sign_api_request(path) - async with self.session.get(url, params=params) as resp: - return json.loads(await resp.text()) + async with self.session.get(url, + params=params, + ssl=ssl_context) as resp: + + return await self._handler_response(resp) async def get_access_details( self, @@ -64,8 +94,10 @@ async def get_access_details( params = sign_api_request(path) params.update({"owner": owner}) - async with self.session.get(url, params=params) as resp: - return json.loads(await resp.text()) + async with self.session.get(url, + params=params, + ssl=ssl_context) as resp: + return await self._handler_response(resp) async def get_share( self, @@ -77,8 +109,10 @@ async def get_share( params = sign_api_request(path) - async with self.session.get(url, params=params) as resp: - return json.loads(await resp.text()) + async with self.session.get(url, + params=params, + ssl=ssl_context) as resp: + return await self._handler_response(resp) async def get_share_details( self, @@ -91,8 +125,10 @@ async def get_share_details( params = sign_api_request(path) - async with self.session.get(url, params=params) as resp: - return json.loads(await resp.text()) + async with self.session.get(url, + params=params, + ssl=ssl_context) as resp: + return await self._handler_response(resp) async def share_new_access( self, @@ -114,8 +150,10 @@ async def share_new_access( "address": address }) - async with self.session.post(url, params=params) as resp: - return json.loads(await resp.text()) + async with self.session.post(url, + params=params, + ssl=ssl_context) as resp: + return await self._handler_response(resp) async def share_edit_access( self, @@ -134,8 +172,10 @@ async def share_edit_access( "access": self.parse_list_to_string(accesslist), }) - async with self.session.patch(url, params=params) as resp: - return json.loads(await resp.text()) + async with self.session.patch(url, + params=params, + ssl=ssl_context) as resp: + return await self._handler_response(resp) async def share_delete_access( self, @@ -152,5 +192,7 @@ async def share_delete_access( "user": self.parse_list_to_string(userlist), }) - async with self.session.delete(url, params=params) as resp: + async with self.session.delete(url, + params=params, + ssl=ssl_context) as resp: return bool(resp.status == 204) diff --git a/tests/test_bindings_bind.py b/tests/test_bindings_bind.py index 244209f..1155a86 100644 --- a/tests/test_bindings_bind.py +++ b/tests/test_bindings_bind.py @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs): "text": asynctest.CoroutineMock( return_value="[]" ), - "status": 204 + "status": 200 }) async def __aenter__(self, *args, **kwargs):