44import os
55import json
66import typing
7-
7+ import logging
88import aiohttp
99
1010from .signature import sign_api_request
1111
12+ import ssl
13+ import certifi
14+
15+
16+ ssl_context = ssl .create_default_context ()
17+ ssl_context .load_verify_locations (certifi .where ())
18+
1219
1320class SwiftSharingRequest :
1421 """Swift Sharing Request backend client."""
@@ -29,6 +36,26 @@ async def __aexit__(self, *excinfo: BaseException) -> None:
2936 """."""
3037 await self .session .close ()
3138
39+ async def _handler_response (
40+ self ,
41+ resp : aiohttp .ClientResponse
42+ ) -> typing .Any :
43+ """Handle API response."""
44+ if resp .status == 200 :
45+ try :
46+ return json .loads (await resp .text ())
47+ except json .decoder .JSONDecodeError :
48+ logging .error ("Decoding JSON error \
49+ response was not possible." )
50+ raise
51+ except Exception as e :
52+ logging .error (f"Unknown exception \
53+ occured with content: { e } ." )
54+ raise
55+ else :
56+ logging .error (f"response status: { resp .status } ." )
57+ raise Exception (f"response status: { resp .status } ." )
58+
3259 async def add_access_request (
3360 self ,
3461 user : str ,
@@ -47,8 +74,11 @@ async def add_access_request(
4774 "signature" : signature ["signature" ],
4875 }
4976
50- async with self .session .post (url , params = params ) as resp :
51- return json .loads (await resp .text ())
77+ async with self .session .post (url ,
78+ params = params ,
79+ ssl = ssl_context ) as resp :
80+
81+ return await self ._handler_response (resp )
5282
5383 async def list_made_requests (
5484 self ,
@@ -65,8 +95,11 @@ async def list_made_requests(
6595 "signature" : signature ["signature" ],
6696 }
6797
68- async with self .session .get (url , params = params ) as resp :
69- return json .loads (await resp .text ())
98+ async with self .session .get (url ,
99+ params = params ,
100+ ssl = ssl_context ) as resp :
101+
102+ return await self ._handler_response (resp )
70103
71104 async def list_owned_requests (
72105 self ,
@@ -83,8 +116,11 @@ async def list_owned_requests(
83116 "signature" : signature ["signature" ],
84117 }
85118
86- async with self .session .get (url , params = params ) as resp :
87- return json .loads (await resp .text ())
119+ async with self .session .get (url ,
120+ params = params ,
121+ ssl = ssl_context ) as resp :
122+
123+ return await self ._handler_response (resp )
88124
89125 async def list_container_requests (
90126 self ,
@@ -106,7 +142,9 @@ async def list_container_requests(
106142 if project :
107143 params ["project" ] = project
108144
109- async with self .session .get (url , params = params ) as resp :
145+ async with self .session .get (url ,
146+ params = params ,
147+ ssl = ssl_context ) as resp :
110148 return json .loads (await resp .text ())
111149
112150 async def share_delete_access (
@@ -127,5 +165,7 @@ async def share_delete_access(
127165 "signature" : signature ["signature" ],
128166 }
129167
130- async with self .session .delete (url , params = params ) as resp :
168+ async with self .session .delete (url ,
169+ params = params ,
170+ ssl = ssl_context ) as resp :
131171 return bool (resp .status == 200 )
0 commit comments