Spaces:
Runtime error
Runtime error
File size: 1,833 Bytes
7181cca 489c84c f6a8e29 489c84c 60b18e1 6e66249 60b18e1 7181cca 60b18e1 2c3b9bf 60b18e1 2c3b9bf 60b18e1 7181cca 60b18e1 c006a72 6e66249 c006a72 6e66249 7181cca 6e66249 c006a72 cc0fd9e c006a72 |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from pymongo import MongoClient
from urllib.parse import quote_plus
import os
password = quote_plus(os.environ['pw'])
# CER_KEY = os.environ['cer']
# uri = "mongodb+srv://cluster0.tzvxqk3.mongodb.net/?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority"
# client = MongoClient(uri,
# tls=True,
# tlsCertificateKeyFile=CER_KEY)
# db = client['testDB']
# collection = db['testCol']
###
# from pymongo.mongo_client import MongoClient
# uri = f"mongodb+srv://deemaa2:{password}@cluster0.tzvxqk3.mongodb.net/?retryWrites=true&w=majority"
# # Create a new client and connect to the server
# client = MongoClient(uri)
# db = client['testDB']
# collection = db['testCol']
###
# name = quote_plus('cidar')
# password = quote_plus("cidar$2024")
uri = f"mongodb+srv://cidar:{password}@cluster0.yuhfxcj.mongodb.net/?retryWrites=true&w=majority"
# Create a new client and connect to the server
client = MongoClient(uri)
db = client['testDB']
collection = db['testCol']
###
def save_response(response_data):
"""Saves a response to the MongoDB collection."""
try:
# Insert the response data into the collection
print("right before insertion")
collection.insert_one(response_data)
print("right after insertion")
except Exception as e:
print(f"An error occurred: {e}")
exit(0)
def read_responses(filter_query=None):
"""Reads responses from the MongoDB collection based on an optional filter."""
try:
if filter_query is None:
filter_query = {} # An empty query will return all documents
responses = collection.find(filter_query)
return list(responses) # Convert cursor to list
except Exception as e:
print(f"An error occurred: {e}")
return [] |