Azure

How To Configure Swagger In Azure Capabilities APIs

This text demonstrates how we are able to combine swagger for API documentation for Azure Operate APIs. As we all know, Swagger UI affords a web-based UI that gives details about REST APIs service (In our case HTTP set off Azure capabilities).

Let’s see step-by-step to know integration of Swagger UI.

Step 1 – Create Azure Capabilities Challenge and Set up Required Nuget

Open Visual Studio 2019 and create new Azure operate venture with empty template. Let’s set up the under packages –

<PackageReference Embody="AzureExtensions.Swashbuckle" Model="3.3.2" />
<PackageReference Embody="Microsoft.NET.Sdk.Capabilities" Model="3.0.11" />

Please notice that these packages might range based mostly in your .NET model and Azure Operate model.

Step 2 – Add Swagger and Swagger UI capabilities

Let’s add HTTP set off operate for swagger and swagger UI.

public static class SwaggerFunctions {
    [SwaggerIgnore]
    [FunctionName("Swagger")]
    public static Activity < HttpResponseMessage > Swagger(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/json")] HttpRequestMessage req,
            [SwashBuckleClient] ISwashBuckleClient swasBuckleClient) {
            return Activity.FromResult(swasBuckleClient.CreateSwaggerJsonDocumentResponse(req));
        }
        [SwaggerIgnore]
        [FunctionName("SwaggerUI")]
    public static Activity < HttpResponseMessage > SwaggerUI(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/ui")] HttpRequestMessage req,
        [SwashBuckleClient] ISwashBuckleClient swasBuckleClient) {
        return Activity.FromResult(swasBuckleClient.CreateSwaggerUIResponse(req, "swagger/json"));
    }
}

Step 3 – Add Startup code for Swashbuckle

Add or modify startup code and add required swagger configuration as per under.

[assembly: WebJobsStartup(typeof(AzFuncWithSwagger.SwashbuckleStartup))]
namespace AzFuncWithSwagger {
    public class SwashbuckleStartup: FunctionsStartup {
        public override void Configure(IFunctionsHostBuilder builder) {
            builder.AddSwashBuckle(Meeting.GetExecutingAssembly(), opts => {
                opts.AddCodeParameter = true;
                opts.Paperwork = new [] {
                    new SwaggerDocument {
                        Identify = "v1",
                            Title = "Swagger doc",
                            Description = "Combine Swagger UI With Azure Capabilities",
                            Model = "v2"
                    }
                };
                opts.ConfigureSwaggerGen = x => {
                    x.CustomOperationIds(apiDesc => {
                        return apiDesc.TryGetMethodInfo(out MethodInfo mInfo) ? mInfo.Identify : default (Guid).ToString();
                    });
                };
            });
        }
    }
}

Step 4 – Add a HTTP set off operate to Check

Let’s add pattern HTTP set off operate to see the API definition from swagger and Check the operate. In case you’ve got Http Set off Capabilities that you do not need so as to add to Swagger, then we are able to use SwaggerIgnoreAttribute.

public static class TainingFunctions {
    [FunctionName("training-save")]
    [ProducesResponseType(typeof(TrainingResponse), (int) HttpStatusCode.OK)]
    public static async Activity < IActionResult > Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]
        [RequestBodyType(typeof(TrainingRequest), "request")] HttpRequest req, ILogger log) {
        log.LogInformation("C# HTTP set off operate processed a request.");
        string requestBody = await new StreamReader(req.Physique).ReadToEndAsync();
        TrainingRequest information = JsonConvert.DeserializeObject < TrainingRequest > (requestBody);
        var responseMessage = new TrainingResponse {
            Id = Guid.NewGuid(), TrainingData = information
        };
        return new OkObjectResult(responseMessage);
    }
}

Right here, I used request and response mannequin for my HTTP set off capabilities for demonstration goal.

Step 5 – Run and Check Azure Capabilities domestically

If we run the azure operate domestically, we are going to see azure operate swagger UI URL http://localhost:7071/api/swagger/ui

If we open this URL in browser it should show like this and we are able to make HTTP request.

Step 6 – Deploy Capabilities in Azure and Check

As soon as azure operate is created in azure then we have to obtain the publish profile. Utilizing this profile, we are able to publish our code straight from visible studio 2019.

How To Configure Swagger In Azure Functions APIs

As soon as publish is profitable in Azure then we are able to make discover and duplicate swagger URL and take a look at the HTTP set off operate.

How To Configure Swagger In Azure Functions APIs

Superior! We’re ready see API definition and make HTTP request from Azure Operate swagger UI.

Please notice, for the simplicity functions, we used AuthorizationLevel.Nameless. However this isn’t a safe approach. To make it safe, we have to Authorize these APIs with Bearer token.

Pleased Studying!

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button