Here you can find the materials for my session "Crea un SDK para cualquier API en minutos con Kiota" (spanish), delivered during CollabDays Barcelona 2022 (22nd October).
En esta sesi�n te ense�ar� qu� es Kiota, la nueva herramienta OpenSource creada por Microsoft, que te permitir� crear un SDK en casi cualquier lenguaje (C#, Java, Typescript, PHP, Ruby, Go) para cualquier API descrita con OpenAPI. Veremos como utilizarlo para crearte tu propio �subset� SDK de MS Graph, as� como de tu propia custom API.
This is the API used to generate the SDK using Kiota. This is small wrapper around MS Graph Teams endpoint. It allows the users:
- Get a list with the Teams in the Tenant (not using paging, so only showing the default Top Teams)
- Get a specific Team by its group Id, showing basic information, plus Owners, Members and Channels
- Provision a MS Teams team with a specific Channel
This API is described using OpenAPI (Swagger library), which is a requirement to generate the SDK using Kiota.
The API is secured by Azure AD, using Microsoft.Identity.Web library. An Azure AD App must be created, requiring permissions for consuming Graph:
- Group.ReadWrite.All (Delegated)
- Directory.Read.All (Delegated)
You must also Expose the API creating an specific scope:
- api://{client_id}/Teams.Manage
You must create a Secret (or Certificate), so the API can call MS GRaph API.
You will also need a second Azure AD App, that will be used by the API Client (the one that will use the SDK generated by Kiota) to consume the API. See here about how to register the App: https://microsoft.github.io/kiota/get-started/register-app.html
See here for more information: https://microsoft.github.io/kiota/get-started/
In a nutshell:
- Install the .net tool:
dotnet tool install --global --prerelease Microsoft.OpenApi.Kiota
- Run the API project and navigate to the swagger url: https://localhost:7295/swagger/v1/swagger.json
- Save the json file to your local disk (c:/temp/TeamifiedOpenApiDescription.json)
- Run the command:
kiota --openapi TeamifiedOpenApiDescription.json --language csharp --class-name TeamifiedApiClient --namespace-name Teamified.Sdk --output ./TeamifiedClient --log-level debug
Now you can add the generated files to your project, and use the generated client. In the demo source code, the generated files are in the project: Teamified.Sdk, and the client is being used in the project Teamified.BatchTeamsProvider
You can see there how you can do Authentication, if your API is secured by Azure AD, as in this demo.
As part of the demo, there is an SPFx project that calls the API with the TypeScript SDK generated by Kiota. As a requirement, you must grant permissions to the Teamified API AAD App. To do so, I recommend to use the M365 CLI:
m365 spo serviceprincipal grant add --resource 'Teamified_Services' --scope 'Teams.Manage'