aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/auth/db
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-08-08 04:47:02 +0300
committerFrederick Muriuki Muriithi2023-08-08 04:47:02 +0300
commitb2c23d6ddcb581a1dfd44f3a695aab15b35c9b63 (patch)
treeb7c049fb75afda32589847d8517c6218c7f29014 /gn_auth/auth/db
parentd88ef631a78c32baed34a126d79f4679a2f09d10 (diff)
downloadgn-auth-b2c23d6ddcb581a1dfd44f3a695aab15b35c9b63.tar.gz
Fix issues caught by linter
* Add a .pylintrc to silence annoying messages * Fix imports * Add missing `parse_db_url` function * Add a new `gn_auth.auth.db.redis` module
Diffstat (limited to 'gn_auth/auth/db')
-rw-r--r--gn_auth/auth/db/__init__.py1
-rw-r--r--gn_auth/auth/db/mariadb.py14
-rw-r--r--gn_auth/auth/db/redis.py22
-rw-r--r--gn_auth/auth/db/sqlite3.py2
4 files changed, 37 insertions, 2 deletions
diff --git a/gn_auth/auth/db/__init__.py b/gn_auth/auth/db/__init__.py
index eab58ef..7bdb38a 100644
--- a/gn_auth/auth/db/__init__.py
+++ b/gn_auth/auth/db/__init__.py
@@ -1 +1,2 @@
+"""Unified database connections module."""
from .protocols import DbCursor, DbConnection
diff --git a/gn_auth/auth/db/mariadb.py b/gn_auth/auth/db/mariadb.py
index a934fd9..69c1d8e 100644
--- a/gn_auth/auth/db/mariadb.py
+++ b/gn_auth/auth/db/mariadb.py
@@ -1,12 +1,22 @@
"""Connections to MariaDB"""
+import logging
import traceback
import contextlib
-from typing import Iterator
+from urllib.parse import urlparse
+from typing import Tuple, Iterator
import MySQLdb as mdb
from .protocols import DbConnection
+def parse_db_url(sql_uri: str) -> Tuple:
+ """Parse SQL_URI env variable note:there is a default value for SQL_URI so a
+ tuple result is always expected"""
+ parsed_db = urlparse(sql_uri)
+ return (
+ parsed_db.hostname, parsed_db.username, parsed_db.password,
+ parsed_db.path[1:], parsed_db.port)
+
@contextlib.contextmanager
def database_connection(sql_uri) -> Iterator[DbConnection]:
"""Connect to MySQL database."""
@@ -18,7 +28,7 @@ def database_connection(sql_uri) -> Iterator[DbConnection]:
port=port or 3306)
try:
yield connection
- except Exception as _exc: # TODO: Make the Exception class less general
+ except mdb.Error as _mdb_err:
logging.debug(traceback.format_exc())
connection.rollback()
finally:
diff --git a/gn_auth/auth/db/redis.py b/gn_auth/auth/db/redis.py
new file mode 100644
index 0000000..fdf1d6f
--- /dev/null
+++ b/gn_auth/auth/db/redis.py
@@ -0,0 +1,22 @@
+"""Connections for Redis."""
+import logging
+import traceback
+import contextlib
+from typing import Iterator
+
+from redis import Redis, RedisError, ConnectionError as RedisConnectionError
+
+@contextlib.contextmanager
+def connection(redis_uri) -> Iterator[Redis]:
+ """Connection to redis"""
+ rconn = Redis.from_url(redis_uri, decode_responses=True)
+ try:
+ if not rconn.ping():
+ raise RedisConnectionError("Could not connect to Redis.")
+ yield rconn
+ except RedisError as _rerr:
+ logging.debug(traceback.format_exc())
+ raise
+ finally:
+ rconn.disconnect()
+ rconn.close()
diff --git a/gn_auth/auth/db/sqlite3.py b/gn_auth/auth/db/sqlite3.py
index 3d94832..be9eb2f 100644
--- a/gn_auth/auth/db/sqlite3.py
+++ b/gn_auth/auth/db/sqlite3.py
@@ -6,6 +6,8 @@ from typing import Any, Callable, Iterator
import traceback
+from flask import current_app
+
from .protocols import DbCursor, DbConnection
@contextlib.contextmanager