/collabdays-bcn-2022

Materials for my session "Crea un SDK para cualquier API en minutos con Kiota", delivered during CollabDays Barcelona 2022 (22nd October)

Primary LanguageC#MIT LicenseMIT

CollabDays Barcelona (22nd October 2022)

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).

Session summary (spanish)

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.

Teamified 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

Generating a .NET SDK using Kiota

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
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.

SPFx permissions to the Teamified API

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'