@@ -19,7 +19,7 @@ class DBConn:
19
19
def __init__ (self ) -> None :
20
20
"""."""
21
21
self .log = MODULE_LOGGER
22
- self .pool : asyncpg .Pool = None
22
+ self .pool : asyncpg .Pool | None = None
23
23
24
24
async def open (self ) -> None :
25
25
"""Gracefully open the database."""
@@ -32,12 +32,14 @@ async def open(self) -> None:
32
32
port = int (os .environ .get ("REQUEST_DB_PORT" , 5432 )),
33
33
ssl = os .environ .get ("REQUEST_DB_SSL" , "prefer" ),
34
34
database = os .environ .get ("REQUEST_DB_NAME" , "swiftbrowserdb" ),
35
- min_size = os .environ .get ("REQUEST_DB_MIN_CONNECTIONS" , 0 ),
36
- max_size = os .environ .get ("REQUEST_DB_MAX_CONNECTIONS" , 49 ),
37
- timeout = os .environ .get ("REQUEST_DB_TIMEOUT" , 120 ),
38
- command_timeout = os .environ .get ("REQUEST_DB_COMMAND_TIMEOUT" , 180 ),
39
- max_inactive_connection_lifetime = os .environ .get (
40
- "REQUEST_DB_MAX_INACTIVE_CONN_LIFETIME" , 0
35
+ min_size = int (os .environ .get ("REQUEST_DB_MIN_CONNECTIONS" , 0 )),
36
+ max_size = int (os .environ .get ("REQUEST_DB_MAX_CONNECTIONS" , 49 )),
37
+ timeout = int (os .environ .get ("REQUEST_DB_TIMEOUT" , 120 )),
38
+ command_timeout = int (
39
+ os .environ .get ("REQUEST_DB_COMMAND_TIMEOUT" , 180 )
40
+ ),
41
+ max_inactive_connection_lifetime = int (
42
+ os .environ .get ("REQUEST_DB_MAX_INACTIVE_CONN_LIFETIME" , 0 )
41
43
),
42
44
)
43
45
except (ConnectionError , OSError ):
@@ -60,11 +62,14 @@ async def close(self) -> None:
60
62
61
63
def erase (self ) -> None :
62
64
"""Immediately erase the connection."""
63
- self .pool .terminate ()
64
- self .pool = None
65
+ if self .pool is not None :
66
+ self .pool .terminate ()
67
+ self .pool = None
65
68
66
69
@staticmethod
67
- async def parse_query (query : typing .List [asyncpg .Record ]) -> typing .List [dict ]:
70
+ async def parse_query (
71
+ query : typing .List [asyncpg .Record ],
72
+ ) -> typing .List [typing .Dict [str , typing .Any ]]:
68
73
"""Parse a database query list to JSON serializable form."""
69
74
return [
70
75
{
@@ -78,79 +83,95 @@ async def parse_query(query: typing.List[asyncpg.Record]) -> typing.List[dict]:
78
83
79
84
async def add_request (self , user : str , container : str , owner : str ) -> bool :
80
85
"""Add an access request to the database."""
81
- async with self .pool .acquire () as conn :
82
- async with conn .transaction ():
83
- await conn .execute (
84
- """
85
- INSERT INTO Requests(
86
+ if self .pool is not None :
87
+ async with self .pool .acquire () as conn :
88
+ async with conn .transaction ():
89
+ await conn .execute (
90
+ """
91
+ INSERT INTO Requests(
92
+ container,
93
+ container_owner,
94
+ recipient,
95
+ created
96
+ ) VALUES (
97
+ $1, $2, $3, NOW()
98
+ );
99
+ """ ,
86
100
container ,
87
- container_owner,
88
- recipient,
89
- created
90
- ) VALUES (
91
- $1, $2, $3, NOW()
92
- );
93
- """ ,
94
- container ,
95
- owner ,
96
- user ,
97
- )
98
- return True
99
-
100
- async def get_request_owned (self , user : str ) -> typing .List :
101
+ owner ,
102
+ user ,
103
+ )
104
+ return True
105
+ return False
106
+
107
+ async def get_request_owned (
108
+ self , user : str
109
+ ) -> typing .List [typing .Dict [str , typing .Any ]]:
101
110
"""Get the requests owned by the getter."""
102
- query = await self .pool .fetch (
103
- """
104
- SELECT *
105
- FROM Requests
106
- WHERE container_owner = $1
107
- ;
108
- """ ,
109
- user ,
110
- )
111
- return await self .parse_query (query )
112
-
113
- async def get_request_made (self , user : str ) -> typing .List :
111
+ if self .pool is not None :
112
+ query = await self .pool .fetch (
113
+ """
114
+ SELECT *
115
+ FROM Requests
116
+ WHERE container_owner = $1
117
+ ;
118
+ """ ,
119
+ user ,
120
+ )
121
+ return await self .parse_query (query )
122
+ return []
123
+
124
+ async def get_request_made (
125
+ self , user : str
126
+ ) -> typing .List [typing .Dict [str , typing .Any ]]:
114
127
"""Get the requests made by the getter."""
115
- query = await self .pool .fetch (
116
- """
117
- SELECT *
118
- FROM Requests
119
- WHERE recipient = $1
120
- ;
121
- """ ,
122
- user ,
123
- )
124
- return await self .parse_query (query )
125
-
126
- async def get_request_container (self , container : str ) -> typing .List :
128
+ if self .pool is not None :
129
+ query = await self .pool .fetch (
130
+ """
131
+ SELECT *
132
+ FROM Requests
133
+ WHERE recipient = $1
134
+ ;
135
+ """ ,
136
+ user ,
137
+ )
138
+ return await self .parse_query (query )
139
+ return []
140
+
141
+ async def get_request_container (
142
+ self , container : str
143
+ ) -> typing .List [typing .Dict [str , typing .Any ]]:
127
144
"""Get the requests made for a container."""
128
- query = await self .pool .fetch (
129
- """
130
- SELECT *
131
- FROM Requests
132
- WHERE container = $1
133
- ;
134
- """ ,
135
- container ,
136
- )
137
- return await self .parse_query (query )
145
+ if self .pool is not None :
146
+ query = await self .pool .fetch (
147
+ """
148
+ SELECT *
149
+ FROM Requests
150
+ WHERE container = $1
151
+ ;
152
+ """ ,
153
+ container ,
154
+ )
155
+ return await self .parse_query (query )
156
+ return []
138
157
139
158
async def delete_request (self , container : str , owner : str , recipient : str ) -> bool :
140
159
"""Delete an access request from the database."""
141
- async with self .pool .acquire () as conn :
142
- async with conn .transaction ():
143
- await conn .execute (
144
- """
145
- DELETE FROM Requests
146
- WHERE
147
- container = $1 AND
148
- container_owner = $2 AND
149
- recipient = $3
150
- ;
151
- """ ,
152
- container ,
153
- owner ,
154
- recipient ,
155
- )
156
- return True
160
+ if self .pool is not None :
161
+ async with self .pool .acquire () as conn :
162
+ async with conn .transaction ():
163
+ await conn .execute (
164
+ """
165
+ DELETE FROM Requests
166
+ WHERE
167
+ container = $1 AND
168
+ container_owner = $2 AND
169
+ recipient = $3
170
+ ;
171
+ """ ,
172
+ container ,
173
+ owner ,
174
+ recipient ,
175
+ )
176
+ return True
177
+ return False
0 commit comments