import pymongo
from pymongo import MongoClient
import certifi
import sys
import os
import csv
import traceback

DB_NAME = "aeg"
MONGODB_USER= "user1234"
MONGODB_PASSWORD= "pass1234"
MONGODB_URL_PREFIX= "cluster0.wre5l"

class AppDbContext():
    def __init__(self):        
        self.mongo_url = "mongodb+srv://user1234:pass1234@cluster0.wre5l.mongodb.net/?retryWrites=true&w=majority"
        
    
    def getDbContext(self):
        client = self.connect(self.mongo_url)                             # connect to database      
        db = client.aeg     
        return db           

    # Try to connect to database, otherwise, raise exception
    def connect(self, url):
        ca = certifi.where()
        try:
            return MongoClient(url, tlsCAFile=ca)
        except:
            sys.stderr.write('ERROR: Connection to Atlas was not successfully')
            print(traceback.format_exc())
