public static async Task <HttpResponseMessage> GetMyDogs(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req,
            TraceWriter log)
        {
            sw.Restart();

            var queryValues = req.GetQueryNameValuePairs();

            // As a client, you would already have your userId when calling typically.
            string userId = queryValues.FirstOrDefault(p => p.Key == "UserId").Value;

            string resourceToken = req.Headers?.GetValues("ResourceToken").FirstOrDefault();

            if (string.IsNullOrEmpty(resourceToken))
            {
                return(req.CreateErrorResponse(HttpStatusCode.Unauthorized, "ResourceToken is a required"));
            }

            // Set the resource token, to demonstrate usage from a 'Client'.
            repo.AuthKeyOrResourceToken(resourceToken);
            // Set the parition key, since our resource token is limited by partition key.  A client could just set this once initially.
            repo.PartitionKey(userId);

            var results = await repo.GetAllItemsAsync <Dog>();

            sw.Stop();

            log.Info($"Execution took: {sw.ElapsedMilliseconds}ms.");

            return(results == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Unable to find document(s) with the given type.")
                : req.CreateResponse(HttpStatusCode.OK, results));
        }
Example #2
0
        public static async Task <IActionResult> GetMyDogs([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            sw.Restart();

            // As a client, you would already have your userId when calling typically.
            string userId = req.Query["UserId"];

            string resourceToken = req.Headers["ResourceToken"];

            if (string.IsNullOrEmpty(resourceToken))
            {
                new BadRequestObjectResult("ResourceToken is required");
            }

            // Set the resource token, to demonstrate usage from a 'Client'.
            repo.AuthKeyOrResourceToken(resourceToken);
            // Set the parition key, since our resource token is limited by partition key.  A client could just set this once initially.
            repo.PartitionKey(userId);

            var results = await repo.GetAllItemsAsync <Dog>(new FeedOptions { PartitionKey = new PartitionKey("*****@*****.**") });

            sw.Stop();
            log.Info($"Execution took: {sw.ElapsedMilliseconds}ms.");

            return(results != null
                ? (ActionResult) new OkObjectResult(results)
                : new BadRequestObjectResult("Unable to find document(s) with the given type."));
        }
        public static async Task <IActionResult> TryGetAllGalleryTiles([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            // sw.Restart();

            // As a client, you would already have your userId when calling typically.
            string userId = req.Query["UserId"];

            string resourceToken = req.Headers["x-zumo-auth"];

            if (string.IsNullOrEmpty(resourceToken))
            {
                new BadRequestObjectResult("ResourceToken is required");
            }

            // Set the resource token, to demonstrate usage from a 'Client'.
            repo2.AuthKeyOrResourceToken(resourceToken);
            // Set the parition key, since our resource token is limited by partition key.  A client could just set this once initially.
            repo2.PartitionKey(userId);

            // BUG: This seems to fail on Azure Functions V2 due to the following:
            // https://github.com/Azure/azure-documentdb-dotnet/issues/202
            // https://github.com/Azure/azure-documentdb-dotnet/issues/312
            var results = await repo2.GetAllItemsAsync <GalleryTile>(new FeedOptions { EnableCrossPartitionQuery = true });

            // sw.Stop();
            // log.Info($"Execution took: {sw.ElapsedMilliseconds}ms.");

            return(results != null
                ? (ActionResult) new OkObjectResult(results)
                : new BadRequestObjectResult("Unable to find document(s) with the given type."));
        }