-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
28 lines (22 loc) · 798 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from pymongo import MongoClient
import certifi
import redis
import os
class MongoConnectionManager():
def __init__(self, database, collection):
self.client = MongoClient(os.environ.get('MONGO_URI'), tlsCAFile=certifi.where())
self.database = database
self.collection = collection
def __enter__(self):
self.database = self.client[self.database]
self.collection = self.database[self.collection]
return self.collection
def __exit__(self, exc_type, exc_value, exc_traceback):
self.client.close()
def get_redis_instance():
redis_instance = redis.Redis(
host=os.environ.get('REDIS_HOST'),
port=os.environ.get('REDIS_PORT'),
password=os.environ.get('REDIS_AUTH')
)
return redis_instance