public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "public-api-read")]
            HttpRequest req, ExecutionContext context, ILogger log)
        {
            log.LogInformation("Read public API function processed a request.");

            var container = await PublicBlobContainer.GetAsync(context, log);

            var helper = new PublicApiHelper(container);

            return(await helper.ReadData(req, async (count, data) =>
            {
                var sb = new StringBuilder();
                sb.AppendLine("[");
                var delimiter = " ";
                await foreach (var body in GetBodiesAsync(data))
                {
                    sb.Append(delimiter);
                    delimiter = ",";
                    sb.AppendLine(body);
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("]");
                return new OkObjectResult(sb.ToString());
            }));
        }
예제 #2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "public-api-download")]
            HttpRequest req, ExecutionContext context, ILogger log)
        {
            log.LogInformation("Download public API function processed a request.");

            var container = await PublicBlobContainer.GetAsync(context, log);

            var helper = new PublicApiHelper(container);

            return(await helper.ReadData(req, async (count, data) =>
            {
                if (count == 1)
                {
                    var enumerator = data.GetAsyncEnumerator();
                    await enumerator.MoveNextAsync();
                    var name = enumerator.Current.Name;
                    var stream = enumerator.Current.Stream;
                    return new FileStreamResult(stream, "application/json")
                    {
                        FileDownloadName = $"{name}.json"
                    };
                }
                else
                {
                    var stream = await GetZipAsync(data);
                    return new FileStreamResult(stream, "application/zip")
                    {
                        FileDownloadName = "public_apis.zip"
                    };
                }
            }));
        }
예제 #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "blobs-clear")]
            HttpRequest req, ExecutionContext context, ILogger log)
        {
            log.LogInformation("Clear blobs function processed a request.");

            var container = await PublicBlobContainer.GetAsync(context, log);

            await container.DeleteIfExistsAsync();

            return(new OkResult());
        }
예제 #4
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "public-api-metadata")]
            HttpRequest req, ExecutionContext context, ILogger log)
        {
            log.LogInformation("Clear blobs function processed a request.");

            var container = await PublicBlobContainer.GetAsync(context, log);

            var helper = new PublicApiHelper(container);

            return(new OkObjectResult(await helper.GetMetadataAsync()));
        }
예제 #5
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "blobs-list")]
            HttpRequest req, ExecutionContext context, ILogger log)
        {
            log.LogInformation("List blobs function processed a request.");

            var container = await PublicBlobContainer.GetAsync(context, log);

            var list = new List <string>();

            await foreach (var blobItem in container.GetAllBlobs())
            {
                list.Add(container.Uri.MakeRelativeUri(blobItem.Uri).ToString());
            }
            return(new OkObjectResult(list));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = "public-api-copy")]
            HttpRequest req, ExecutionContext context, ILogger log)
        {
            log.LogInformation("Copy public API function processed a request.");

            var container = await PublicBlobContainer.GetAsync(context, log);

            var helper = new PublicApiHelper(container);

            return(await helper.ReadData(req, async (count, data) =>
            {
                var errorsList = new List <dynamic>();

                await foreach (var pair in data)
                {
                    if (pair.IsFailed)
                    {
                        errorsList.Add(new { name = pair.Name, error = pair.Exception.Message });
                        continue;
                    }
                    var blobName = $"{pair.Name}.json";
                    var blob = container.GetBlockBlobReference(blobName);
                    blob.Properties.ContentType = "application/json";
                    var stream = pair.Stream;
                    await using (stream)
                    {
                        await blob.UploadFromStreamAsync(stream);
                    }
                }

                var sb = new StringBuilder($"{count - errorsList.Count} blobs uploaded to container '{container.Name}' successfully.");
                if (errorsList.Count != 0)
                {
                    sb.AppendLine($" {errorsList.Count} blobs failed, see details bellow.");
                    foreach (var error in errorsList)
                    {
                        sb.AppendLine($"    {error.name} : {error.error}");
                    }
                }
                return new OkObjectResult(sb.ToString());
            }));
        }