import logging
import azure.functions as func
import azure.cosmos.cosmos_client as cosmos_client
import azure.cosmos.errors as errors
import json
config = {
'ENDPOINT': 'https://testdatabase02.documents.azure.com:443/',
'PRIMARYKEY': 'bpYTtYyi9lEiOG1wNvg8koXq56nqyGwJgS5NEE8wZzuImRA5qFtKbkTU9tVbt4rF39XbXsQqgvuFwp977UQpJg==',
'DATABASE': 'ToDoList',
'CONTAINER': 'Items'
}
def main(
req: func.HttpRequest
) -> func.HttpResponse:
# Initialize the Cosmos client
client = cosmos_client.CosmosClient(url_connection=config['ENDPOINT'], auth={
'masterKey': config['PRIMARYKEY']})
database_link = 'dbs/' + config['DATABASE']
collection_link = database_link + '/colls/' + config['CONTAINER']
# Create a database
# db = client.ReadDatabase('dbs/' + config['DATABASE'])
msg = ""
# collection list
'''
collections = list(client.QueryContainers(
database_link,
{
"query": "SELECT * FROM r"
}))
'''
# document list (get all items)
'''
documentlist = list(client.ReadItems(collection_link, {
'maxItemCount': 10
}))
if (len(documentlist) > 0):
msg = documentlist[0]['category'] # get item
else:
msg = "암껏도 없나봐"
'''
# query doc
query_with_optional_parameters = "SELECT * FROM Items"
options = {}
options['enableCrossPartitionQuery'] = True
try:
results = list(client.QueryItems(
collection_link,
query_with_optional_parameters,
options
))
for doc in results:
msg += json.dumps(doc) + "\n"
except errors.HTTPFailure as e:
if e.status_code == 404:
msg = "Document doesn't exist"
elif e.status_code == 400:
# Can occur when we are trying to query on excluded paths
msg = "Bad Request exception occured: " + e.__str__()
pass
else:
raise
finally:
print()
# insert doc
'''
client.CreateItem(collection_link, {
'id': 'another id',
'category': '5'
})
'''
return func.HttpResponse(
msg,
status_code=400
)
-
-
Save Gumball12/00658a24d4ddaa9f295bdd49faac6575 to your computer and use it in GitHub Desktop.
sogaeding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import azure.cosmos.cosmos_client as cosmos_client | |
import azure.cosmos.errors as errors | |
config = { | |
'ENDPOINT': 'https://testdatabase02.documents.azure.com:443/', | |
'PRIMARYKEY': 'bpYTtYyi9lEiOG1wNvg8koXq56nqyGwJgS5NEE8wZzuImRA5qFtKbkTU9tVbt4rF39XbXsQqgvuFwp977UQpJg==', | |
'DATABASE': 'ToDoList', | |
'CONTAINER': 'Items' | |
} | |
# Initialize the Cosmos client | |
client = cosmos_client.CosmosClient(url_connection=config['ENDPOINT'], auth={ | |
'masterKey': config['PRIMARYKEY']}) | |
database_link = 'dbs/' + config['DATABASE'] | |
collection_link = database_link + '/colls/' + config['CONTAINER'] | |
# Create a database | |
# db = client.ReadDatabase('dbs/' + config['DATABASE']) | |
msg = "" | |
# collection list | |
''' | |
collections = list(client.QueryContainers( | |
database_link, | |
{ | |
"query": "SELECT * FROM r" | |
})) | |
''' | |
# document list (get all items) | |
''' | |
documentlist = list(client.ReadItems(collection_link, { | |
'maxItemCount': 10 | |
})) | |
if (len(documentlist) > 0): | |
msg = documentlist[0]['category'] # get item | |
else: | |
msg = "암껏도 없나봐" | |
''' | |
# query doc | |
query_with_optional_parameters = "SELECT * FROM Items" | |
options = {} | |
options['enableCrossPartitionQuery'] = True | |
try: | |
results = list(client.QueryItems( | |
collection_link, | |
query_with_optional_parameters, | |
options | |
)) | |
for doc in results: | |
msg += json.dumps(doc) + "\n" | |
except errors.HTTPFailure as e: | |
if e.status_code == 404: | |
msg = "Document doesn't exist" | |
elif e.status_code == 400: | |
# Can occur when we are trying to query on excluded paths | |
msg = "Bad Request exception occured: " + e.__str__() | |
pass | |
else: | |
raise | |
finally: | |
print() | |
# insert doc | |
''' | |
client.CreateItem(collection_link, { | |
'id': 'another id', | |
'category': '5' | |
}) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment