Consuming OpenAPI (Swagger) services
The OpenAPI (formerly known as Swagger) is a way to provide a formal specification of your REST API. Radzen Blazor Studio makes it easy to consume such API. It scaffolds for you:
- Service classes which make HTTP requests to the REST service and optionally authenticate.
- Model classes for all entities.
- Various pages that allow to to quickly make a full CRUD application from the service.
Connect to OpenAPI service
In the following tutorial we will show how to:
- Create an OpenAPI data source in Radzen Blazor Studio.
- Generate code for it.
- Display data.
The tutorial uses the public PetStore service available at https://petstore3.swagger.io
.
Configure the OpenAPI connection
- Create a new Blazor application in Radzen Blazor Studio or open an existing one.
- Click the button.
- Select OpenAPI as the data source type in the Create or update data source dialog. This opens the OpenAPI connection editor.
- Enter the PetStore connection details
- Set Name to
PetStore
. The name must be a valid C# identifier. It is used for namespaces and service class names. - Set Specification Url or file to
https://petstore3.swagger.io/api/v3/openapi.json
.
- Set Name to
- Click Next. Radzen Blazor Studio will infer the metadata and display all REST endpoints (paths).
- Check the items from the service that you want to generate code for (models and methods). Click Next.
- Click Finish
This procedure will generate the following code:
- A service class that can invoke the service methods
PetStoreService.cs
- Various model classes: (
Pet.cs
,Category.cs
et. al.)
Display data returned from the service
In this step we will display the Pet
entities returned by the findPetsByStatus method.
- Right click the
Pages
directory and select New Page…. - Pick CRUD from the available templates.
- Expand the Entities tree and select
Pet
. This will display the Specify CRUD methods dialog. - Set Read method to
FindPetsByStatus
. The generated page will use theFindPetsByStatus()
method of thePetStoreService
generated in the previous step. - Click OK
- Uncheck Alow editing, Alow inserting and Alow editing. We don’t need those pages for this demo.
- Click Finish.
- Open the Pets.razor page and go to methods design mode.
- Select the
OnOnitializedAsync
method from the dropdown. - Select the
Invoke
statement and set thestatus
parameter to"available"
. - Run the application and click the Pets item in the left-hand navigation menu.
You should see something like this: