-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Establish connection or delete connection between application and MongoDB, Redis.""" | ||
|
||
import os | ||
from pymongo import MongoClient | ||
from pymongo.errors import ConnectionFailure | ||
import redis | ||
|
||
def connect_to_database(): | ||
""" | ||
Connect to database. | ||
Return: | ||
object: connection to database. | ||
""" | ||
|
||
try: | ||
connection = MongoClient(os.getenv("MONGO_URI")) | ||
except ConnectionFailure as error: | ||
print(f"Error: MongoDB connection not established {error}") | ||
else: | ||
print("MongoDB: Connection established.") | ||
return connection | ||
|
||
def connect_to_redis(): | ||
""" | ||
Connect to Redis. | ||
Return: | ||
object: connection to Redis. | ||
""" | ||
|
||
try: | ||
connection = redis.Redis(host=os.getenv("REDIS_HOST"), | ||
port=os.getenv("REDIS_PORT"), | ||
encoding="utf8", | ||
decode_responses=True) | ||
except ConnectionError as error: | ||
print(f"Error: Redis connection not established {error}") | ||
else: | ||
print("Redis: Connection established.") | ||
return connection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters