Introduction
In today's microservice-evolving world, OpenAPI plays a major role as it is independent of platform and language for communication. i.e., a few APIs developed and deployed that can be integrated with any language client application. We can start using it.
Microsoft taking a step here to make client app developers' journey easy with a command line called Kiota. By Supporting many languages .Net, Java, Python, Go, PHP, Ruby, Typescript, etc.
In this article, we will see a C# Example to generate a client application using Kiota.
For this, install the Kiota tool first by referring to the link: Install Kiota.
After installing, to make sure kiota is installed successfully, Just type the below command.
kiota search graph
Normally this "kiota search graph" command will list out all the APIs that match the keyword "graph".
To know the witch and all languages, it will support, you can simply type the command.
kiota info
Here in the above snap, you will be able to see language supported by kiota and its maturity level.
Use the below sample openapi.json you can use for generating a client app using Microsoft kiotaota.
{
"openapi": "3.0.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/add": {
"get": {
"summary": "Add Numbers",
"operationId": "add_numbers_add_get",
"parameters": [
{
"name": "a",
"in": "query",
"required": true,
"schema": {
"title": "A"
}
},
{
"name": "b",
"in": "query",
"required": true,
"schema": {
"title": "B"
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/subtract": {
"get": {
"summary": "Subtract Numbers",
"operationId": "subtract_numbers_subtract_get",
"parameters": [
{
"name": "a",
"in": "query",
"required": true,
"schema": {
"title": "A"
}
},
{
"name": "b",
"in": "query",
"required": true,
"schema": {
"title": "B"
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/multiply": {
"get": {
"summary": "Multiply Numbers",
"operationId": "multiply_numbers_multiply_get",
"parameters": [
{
"name": "a",
"in": "query",
"required": true,
"schema": {
"title": "A"
}
},
{
"name": "b",
"in": "query",
"required": true,
"schema": {
"title": "B"
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/divide": {
"get": {
"summary": "Divide Numbers",
"operationId": "divide_numbers_divide_get",
"parameters": [
{
"name": "a",
"in": "query",
"required": true,
"schema": {
"title": "A"
}
},
{
"name": "b",
"in": "query",
"required": true,
"schema": {
"title": "B"
}
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"type": "array",
"title": "Detail"
}
},
"type": "object",
"title": "HTTPValidationError"
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
"type": "array",
"title": "Location"
},
"msg": {
"type": "string",
"title": "Message"
},
"type": {
"type": "string",
"title": "Error Type"
}
},
"type": "object",
"required": [
"loc",
"msg",
"type"
],
"title": "ValidationError"
}
}
}
}
Use the below command to generate the client app.
kiota generate -d .\OpenAPI.json -l Csharpq
With -d, set the path of OpenAPI.json, and with -l, use the language name to generate the client app.
It will generate the client in directory output like below.
Resolve the dependencies by installing the below decencies.
Microsoft.Kiota.Abstractions.Extensions;
Microsoft.Kiota.Abstractions;
Microsoft.Kiota.Serialization.Form;
Microsoft.Kiota.Serialization.Json;
Microsoft.Kiota.Serialization.Multipart;
Microsoft.Kiota.Serialization.Text;
Now client application will be ready to run.
Summary
I hope it helps you understand the Microsoft kiota framework at a high level. We will see more about this in the next article.