Skip to content

Instantly share code, notes, and snippets.

@ealsur
Last active January 22, 2018 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ealsur/44cd9fd7803ccfb35343411a11140207 to your computer and use it in GitHub Desktop.
Save ealsur/44cd9fd7803ccfb35343411a11140207 to your computer and use it in GitHub Desktop.
Azure Cosmos DB + Functions Cookbook: static client (wrong scenario)
#r "Microsoft.Azure.Documents.Client"
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System.Net;
private static string endpointUrl = ConfigurationManager.AppSettings["cosmosDBAccountEndpoint"];
private static string authorizationKey = ConfigurationManager.AppSettings["cosmosDBAccountKey"];
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
// Avoid doing this!
using (DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey)){
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