Skip to content

Instantly share code, notes, and snippets.

@adityaprakashgupta
Last active April 23, 2022 11:08
Show Gist options
  • Save adityaprakashgupta/2594933f0227052a5e0b8892d91b15f9 to your computer and use it in GitHub Desktop.
Save adityaprakashgupta/2594933f0227052a5e0b8892d91b15f9 to your computer and use it in GitHub Desktop.
This is mongodb API client in Python.
import requests
class Connection:
def __init__(self, app_id, api_key, dataSource, database, collection):
"""
app_id: str, It is a unique ID specific to each cluster.
api_key: str
dataSource: str, Name of the cluster.
database: str, Name of the database.
collection: str, Name of the collection.
"""
self.app_id = app_id
self.api_key = api_key
self.dataSource = dataSource
self.database = database
self.collection = collection
self.url = 'https://data.mongodb-api.com/app/' + \
self.app_id + '/endpoint/data/beta/action/'
self.headers = {'Content-Type': 'application/json', 'Accept': 'application/json',
'Access-Control-Request-Headers': '*', 'api-key': self.api_key}
self.data = {'dataSource': self.dataSource,
'database': self.database, 'collection': self.collection}
def findOne(self, filter={}, projection={}):
"""
filter: dictionary
projection: dictionary
"""
temp_dict = self.data.copy()
temp_dict['filter'] = filter
temp_dict['projection'] = projection
response = requests.post(
self.url+'findOne', headers=self.headers, json=temp_dict)
return response.json()
def find(self, filter={}, projection={}, sort={}, limit=1000, skip=0):
"""
filter: dictionary
projection: dictionary
sort: dictionary
limit: int
skip: int
"""
temp_dict = self.data.copy()
temp_dict['filter'] = filter
temp_dict['projection'] = projection
temp_dict['sort'] = sort
temp_dict['limit'] = limit
temp_dict['skip'] = skip
response = requests.post(
self.url+'find', headers=self.headers, json=temp_dict)
return self.error_handler(response)
def insertOne(self, document):
"""
document: dictionary
"""
temp_dict = self.data.copy()
temp_dict['document'] = document
response = requests.post(self.url+'insertOne',
headers=self.headers, json=temp_dict)
return self.error_handler(response)
def insertMany(self, documents):
"""
documents: list of dictionaries
"""
temp_dict = self.data.copy()
temp_dict['documents'] = documents
response = requests.post(
self.url+'insertMany', headers=self.headers, json=temp_dict)
return self.error_handler(response)
def updateOne(self, filter, update, upsert=False):
"""
filter: dictionary
update: dictionary
upsert: boolean
"""
temp_dict = self.data.copy()
temp_dict['filter'] = filter
temp_dict['update'] = update
temp_dict['upsert'] = upsert
response = requests.post(self.url+'updateOne',
headers=self.headers, json=temp_dict)
return self.error_handler(response)
def updateMany(self, filter, update, upsert=False):
"""
filter: dictionary
update: dictionary
upsert: boolean
"""
temp_dict = self.data.copy()
temp_dict['filter'] = filter
temp_dict['update'] = update
temp_dict['upsert'] = upsert
response = requests.post(
self.url+'updateMany', headers=self.headers, json=temp_dict)
return self.error_handler(response)
def deleteOne(self, filter):
"""
filter: dictionary
"""
temp_dict = self.data.copy()
temp_dict['filter'] = filter
response = requests.post(self.url+'deleteOne',
headers=self.headers, json=temp_dict)
return self.error_handler(response)
def deleteMany(self, filter):
"""
filter: dictionary
"""
temp_dict = self.data.copy()
temp_dict['filter'] = filter
response = requests.post(
self.url+'deleteMany', headers=self.headers, json=temp_dict)
return self.error_handler(response)
def replaceOne(self, filter, replacement, upsert=False):
"""
filter: dictionary
replacement: dictionary
upsert: boolean
"""
temp_dict = self.data.copy()
temp_dict['filter'] = filter
temp_dict['replacement'] = replacement
temp_dict['upsert'] = upsert
response = requests.post(
self.url+'replaceOne', headers=self.headers, json=temp_dict)
return self.error_handler(response)
def error_handler(self, response):
if response.status_code == 200:
return response.json()
elif response.status_code == 400:
message = 'Bad Request'
return {'message': message}
elif response.status_code == 401:
message = 'Unauthorized'
return {'message': message}
elif response.status_code == 404:
message = 'Not Found'
return {'message': message}
elif response.status_code == 500:
message = 'Internal Server Error'
return {'message': message}
else:
raise Exception('error')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment