Last active
October 14, 2018 19:15
-
-
Save ealsur/cac4a2d7498f5995c14e4dbf13e4b2f3 to your computer and use it in GitHub Desktop.
Azure Cosmos DB + Functions Cookbook: static client
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
#r "Microsoft.Azure.Documents.Client" | |
using Microsoft.Azure.Documents; | |
using Microsoft.Azure.Documents.Client; | |
using System.Net; | |
using System.Configuration; | |
private static string endpointUrl = ConfigurationManager.AppSettings["cosmosDBAccountEndpoint"]; | |
private static string authorizationKey = ConfigurationManager.AppSettings["cosmosDBAccountKey"]; | |
private static DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey); | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
string id = req.GetQueryNameValuePairs() | |
.FirstOrDefault(q => string.Compare(q.Key, "id", true) == 0) | |
.Value; | |
if (string.IsNullOrEmpty(id)){ | |
return req.CreateResponse(HttpStatusCode.BadRequest); | |
} | |
Uri documentUri = UriFactory.CreateDocumentUri("name of database","name of collection",id); | |
Document doc = await client.ReadDocumentAsync(documentUri); | |
if (doc == null){ | |
return req.CreateResponse(HttpStatusCode.NotFound); | |
} | |
return req.CreateResponse(HttpStatusCode.OK, doc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment