Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
fix: mongodb uri
Browse files Browse the repository at this point in the history
Co-authored-by: loyal812 <jh.chan0812@gmail.com>
  • Loading branch information
eureka320 and loyal812 committed Mar 27, 2024
1 parent 47720c3 commit 7da70d5
Show file tree
Hide file tree
Showing 35 changed files with 130 additions and 270 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
OPENAI_API_KEY=
MATHPIX_APP_ID=
MATHPIX_APP_KEY=
Mongo_URI=
MongoDB_USERNAME=
MongoDB_PASSWORD=
MONGODB_CLUSTER_NAME=
13 changes: 1 addition & 12 deletions chatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import gc
import argparse
from pathlib import Path
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.chatting.ChattingClass import ChattingClass
Expand All @@ -16,18 +15,8 @@ def chatting(args):
# Payload
payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"
mongo_uri = payload_data["mongo_uri"]

# Call class instance
mongodb = MongoDBClass(
Expand Down
13 changes: 1 addition & 12 deletions check_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import gc
import argparse
from pathlib import Path
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
Expand All @@ -17,18 +16,8 @@ def check_api_key(args):
# Payload
payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"
mongo_uri = payload_data["mongo_uri"]

# Call class instance
mongodb = MongoDBClass(
Expand Down
13 changes: 1 addition & 12 deletions create_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import argparse
from pathlib import Path
from datetime import datetime
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
Expand All @@ -19,18 +18,8 @@ def create_api_key(args):
# Payload
payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"
mongo_uri = payload_data["mongo_uri"]

# Call class instance
mongodb = MongoDBClass(
Expand Down
13 changes: 1 addition & 12 deletions delete_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import gc
import argparse
from pathlib import Path
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
Expand All @@ -16,18 +15,8 @@ def delete_api_key(args):
# Payload
payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"
mongo_uri = payload_data["mongo_uri"]

# Call class instance
mongodb = MongoDBClass(
Expand Down
13 changes: 1 addition & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time
import argparse
from pathlib import Path
from urllib.parse import quote_plus
import concurrent.futures
from datetime import datetime
import json
Expand All @@ -24,18 +23,8 @@ def total_process(args):

payload_data = read_json(args.payload_dir)

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"
mongo_uri = payload_data["mongo_uri"]

# Call class instance
mongodb = MongoDBClass(
Expand Down
154 changes: 95 additions & 59 deletions src/mongodb/MongoDBClass.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,117 @@
import re
import os
import datetime
import jwt
import json
import pytz
from dotenv import load_dotenv
from bson.objectid import ObjectId
from bson.json_util import dumps
from pymongo import MongoClient
from pymongo.errors import ServerSelectionTimeoutError
from urllib.parse import quote_plus

from src.models.api_model import APIModel

class MongoDBClass():
def __init__(self, db_name, collection_name, mongo_uri=""):
self.db_name = db_name
self.collection_name = collection_name
self.set_mongo_uri(mongo_uri)
self.set_mongo_uri()

def set_mongo_uri(self, mongo_uri):
if mongo_uri:
self.mongo_uri = mongo_uri
else:
load_dotenv()
self.mongo_uri = os.getenv("Mongo_URI")
def set_mongo_uri(self):
load_dotenv()
mongodb_username = os.getenv("MONGODB_USERNAME")
mongodb_password = os.getenv("MONGODB_PASSWORD")
mongodb_cluster_name = os.getenv("MONGODB_CLUSTER_NAME")
# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net"

self.mongo_uri = mongo_uri

if self.mongo_uri is not None:
os.environ["Mongo_URI"] = self.mongo_uri
return True
else:
# Handle the absence of the environment variable
# You might want to log an error, raise an exception, or provide a default value
# For example, setting a default value
os.environ["Mongo_URI"] = mongo_uri
return False

def mongo_connect(self):
# mongo config
client = MongoClient(self.mongo_uri)
if client is None:
# no connection, exit early
print("MongoDB Server Connection Error")
return {"result": False ,"message": "MongoDB Server Connection Error."}
else:
try:
# mongo config
print(self.mongo_uri)

client = MongoClient(self.mongo_uri)
# Test the connection by accessing a database (e.g., admin)
client.admin.command('ismaster')

print("MongoDB Server Connected.")
# confirm oridosai db exist
dblist = client.list_database_names()
if self.db_name in dblist:
print(f"The {self.db_name} database exists.")

# confirm apis collection exist
oridosai_db = client[self.db_name]
collist = oridosai_db.list_collection_names()
if self.collection_name in collist:
print(f"The {self.collection_name} collection exists.")
user_col = oridosai_db[self.collection_name]
return {"result": True, "message": user_col}
else:
print(f"The {self.collection_name} collection not exists.")
return {"result": False ,"message": f"The {self.collection_name} collection not exists."}
# Access the database
db = client[self.db_name]

# Check if the database exists
db_list = client.list_database_names()
if self.db_name in db_list:
print(f"The {self.db_name} database exists.")
else:
print(f"The {self.db_name} database does not exist.")
# create the database
client[self.db_name].create_collection(self.collection_name)
print(f"The {self.db_name} database has been created with {self.collection_name} collection.")

# confirm apis collection exist
oridosai_db = client[self.db_name]
collist = oridosai_db.list_collection_names()
if self.collection_name in collist:
print(f"The {self.collection_name} collection exists.")
user_col = oridosai_db[self.collection_name]
return {"result": True, "message": user_col}
else:
print(f"The {self.collection_name} collection not exists.")
return {"result": False ,"message": f"The {self.collection_name} collection not exists."}
print(f"Creating {self.db_name} database.")
db = client[self.db_name]

# Access the collection
collection_list = db.list_collection_names()
if self.collection_name in collection_list:
print(f"The {self.collection_name} collection exists.")
collection = db[self.collection_name]
else:
print(f"The {self.collection_name} collection does not exist.")
print(f"Creating {self.collection_name} collection.")
collection = db[self.collection_name]

return {"result": True, "message": collection}

except ServerSelectionTimeoutError as err:
print("MongoDB Server Connection Error:", err)
return {"result": False, "message": "MongoDB Server Connection Error: " + str(err)}
except Exception as e:
print("An error occurred:", e)
return {"result": False, "message": "An error occurred: " + str(e)}

# def mongo_connect(self):
# # mongo config
# client = MongoClient(self.mongo_uri)
# if client is None:
# # no connection, exit early
# print("MongoDB Server Connection Error")
# return {"result": False ,"message": "MongoDB Server Connection Error."}
# else:
# print("MongoDB Server Connected.")
# # confirm oridosai db exist
# dblist = client.list_database_names()
# print(dblist)
# if self.db_name in dblist:
# print(f"The {self.db_name} database exists.")

# # confirm apis collection exist
# oridosai_db = client[self.db_name]
# collist = oridosai_db.list_collection_names()
# if self.collection_name in collist:
# print(f"The {self.collection_name} collection exists.")
# user_col = oridosai_db[self.collection_name]
# return {"result": True, "message": user_col}
# else:
# print(f"The {self.collection_name} collection not exists.")
# return {"result": False ,"message": f"The {self.collection_name} collection not exists."}
# else:
# print(f"The {self.db_name} database does not exist.")
# # create the database
# client[self.db_name].create_collection(self.collection_name)
# print(f"The {self.db_name} database has been created with {self.collection_name} collection.")

# # confirm apis collection exist
# oridosai_db = client[self.db_name]
# collist = oridosai_db.list_collection_names()
# if self.collection_name in collist:
# print(f"The {self.collection_name} collection exists.")
# user_col = oridosai_db[self.collection_name]
# return {"result": True, "message": user_col}
# else:
# print(f"The {self.collection_name} collection not exists.")
# return {"result": False ,"message": f"The {self.collection_name} collection not exists."}

# return {"result": True, "message": f"The {self.db_name} database has been created with {self.collection_name} collection."}
# # return {"result": True, "message": f"The {self.db_name} database has been created with {self.collection_name} collection."}

def create_api(self, data:APIModel):
# Connect to MongoDB
Expand Down
1 change: 0 additions & 1 deletion src/test_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import time

import pytest
Expand Down
13 changes: 1 addition & 12 deletions src/utils/chatting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import gc
from pathlib import Path
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.chatting.ChattingClass import ChattingClass
Expand All @@ -14,18 +13,8 @@ def chatting(args):
# Payload
payload_data = read_json(args['payload_dir'])

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"
mongo_uri = payload_data["mongo_uri"]

# Call class instance
mongodb = MongoDBClass(
Expand Down
13 changes: 1 addition & 12 deletions src/utils/check_api_key.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import gc
from pathlib import Path
from urllib.parse import quote_plus

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
Expand All @@ -14,18 +13,8 @@ def check_api_key(args):
# Payload
payload_data = read_json(args['payload_dir'])

# Your MongoDB Atlas connection details
mongodb_username = payload_data["mongodb_username"]
mongodb_password = payload_data["mongodb_password"]
mongodb_cluster_name = payload_data["mongodb_cluster_name"]
mongodb_database_name = payload_data["mongodb_database_name"]

# Escape the mongodb_username and mongodb_password
mongodb_escaped_username = quote_plus(mongodb_username)
mongodb_escaped_password = quote_plus(mongodb_password)

# Construct the MongoDB Atlas URI
mongo_uri = f"mongodb+srv://{mongodb_escaped_username}:{mongodb_escaped_password}@{mongodb_cluster_name}.mongodb.net/{mongodb_database_name}"
mongo_uri = payload_data["mongo_uri"]

# Call class instance
mongodb = MongoDBClass(
Expand Down
Loading

0 comments on commit 7da70d5

Please sign in to comment.