Exemple #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "preferences")] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("Entering save_preferences operation for the Personalization API.");

            //  get config values
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var    documentClient = MySingletonDocClient.GetDocumentClient(config);
            string requestBody    = await new StreamReader(req.Body).ReadToEndAsync();

            if (requestBody == null)
            {
                return(new BadRequestObjectResult("Please pass JSON string in the request body"));
            }
            else
            {
                CosmosData data = JsonConvert.DeserializeObject <CosmosData>(requestBody);
                string     id   = "" + data.id;

                if (id == "")
                {
                    return(new BadRequestObjectResult("Please pass valid id in the request body"));
                }
                else
                {
                    Uri collectionUri = UriFactory.CreateDocumentCollectionUri(config["COSMOS_DB_PERSONALIZATION_DATABASE"], config["COSMOS_DB_PERSONALIZATION_COLLECTION"]);

                    DateTime start = DateTime.Now;
                    await documentClient.UpsertDocumentAsync(collectionUri, data, null, true);

                    TimeSpan time = DateTime.Now - start;

                    // add timing & location properties
                    CosmosDataWithTimings o = JsonConvert.DeserializeObject <CosmosDataWithTimings>(requestBody);
                    o.actualReadOrWriteEndPoint = documentClient.WriteEndpoint.ToString();
                    o.preferredCosmosDBLocation = config["COSMOS_DB_PREFERRED_LOCATION"];
                    o.duration = time.Milliseconds;

                    return((ActionResult) new OkObjectResult(o));
                }
            }
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "preferences/{id}")] HttpRequest req,
            ILogger log, string id, ExecutionContext context)
        {
            log.LogInformation("Entering get_preferences operation for Personalization");

            //  get config values
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var documentClient = MySingletonDocClient.GetDocumentClient(config);

            if (id == null)
            {
                log.LogInformation($"parameter {id} not found");
                return(new BadRequestObjectResult("Please pass an id parameter"));
            }

            try
            {
                Uri documentUri = UriFactory.CreateDocumentUri(config["COSMOS_DB_PERSONALIZATION_DATABASE"], config["COSMOS_DB_PERSONALIZATION_COLLECTION"], id);

                DateTime start = DateTime.Now;
                Document doc   = await documentClient.ReadDocumentAsync(documentUri);

                TimeSpan time = DateTime.Now - start;

                // add timing & location properties
                CosmosDataWithTimings o = JsonConvert.DeserializeObject <CosmosDataWithTimings>(doc.ToString());
                o.actualReadOrWriteEndPoint = documentClient.ReadEndpoint.ToString();
                o.preferredCosmosDBLocation = config["COSMOS_DB_PREFERRED_LOCATION"];
                o.duration = time.Milliseconds;

                return((ActionResult) new OkObjectResult(o));
            }
            catch (Exception e)
            {
                log.LogInformation(e.Message);
                log.LogInformation($"data for {id} not found");
                return(new NotFoundResult());
            }
        }