Skip to content

Commit

Permalink
Merge pull request #3841 from microsoft/specs/kiota.config
Browse files Browse the repository at this point in the history
Introducing kiota.config spec
  • Loading branch information
sebastienlevert authored Jan 24, 2024
2 parents 56c42d0 + a76aaca commit 6b61fd1
Show file tree
Hide file tree
Showing 9 changed files with 771 additions and 0 deletions.
120 changes: 120 additions & 0 deletions specs/cli/client-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# kiota client add

## Description

`kiota client add` allows a developer to add a new API client to the `kiota-config.json` file. If no `kiota-config.json` file is found, a new `kiota-config.json` file would be created in thr current working directory. The command will add a new entry to the `clients` section of the `kiota-config.json` file. Once this is done, a local copy of the OpenAPI description is generated and kept in the `.kiota/descriptions` folder.

When executing, a new API entry will be added and will use the `--client-name` parameter as the key for the map. When loading the OpenAPI description, it will store the location of the description in the `descriptionLocation` property. If `--include-path` or `--exclude-path` are provided, they will be stored in the `includePatterns` and `excludePatterns` properties respectively.

Every time an API client is added, a copy of the OpenAPI description file will be stored in the `./.kiota/{client-name}` folder. The files will be named using the API client name. This will allow the CLI to detect changes in the description and avoid downloading the description again if it hasn't changed.

At the same time, an [API Manifest](https://www.ietf.org/archive/id/draft-miller-api-manifest-01.html#section-2.5-3) file will be generated (if non existing) or edited (if already existing) to represent the surface of the API being used. This file will be named `apimanifest.json` and next to the `kiota-config.json`. This file will be used to generate the code files.

Once the `kiota-config.json` file is generated and the OpenAPI description file is saved locally, the code generation will be executed and then the API Manifest would become available.

## Parameters

| Parameters | Required | Example | Description |
| -- | -- | -- | -- |
| `--client-name \| --cn` | Yes | graphDelegated | Name of the client and the client class. Unique within the parent API. Defaults to `Client` |
| `--openapi \| -d` | Yes | https://aka.ms/graph/v1.0/openapi.yaml | The location of the OpenAPI description in JSON or YAML format to use to generate the SDK. Accepts a URL or a local path. |
| `--search-key \| --sk` | No | github::microsoftgraph/msgraph-metadata/graph.microsoft.com/v1.0 | The search key used to locate the OpenAPI description. |
| `--include-path \| -i` | No | /me/chats#GET | A glob pattern to include paths from generation. Accepts multiple values. Defaults to no value which includes everything. |
| `--exclude-path \| -e` | No | \*\*/users/\*\* | A glob pattern to exclude paths from generation. Accepts multiple values. Defaults to no value which excludes nothing. |
| `--language \| -l` | Yes | csharp | The target language for the generated code files or for the information. |
| `--namespace-name \| -n` | No | Contoso.GraphApp | The namespace of the client class. Defaults to `Microsoft.Graph`. |
| `--backing-store \| -b` | No | | Defaults to `false` |
| `--exclude-backward-compatible \| --ebc` | No | | Whether to exclude the code generated only for backward compatibility reasons or not. Defaults to `false`. |
| `--structured-media-types \| -m` | No | `application/json` |Any valid media type which will match a request body type or a response type in the OpenAPI description. Default are documented [here](https://learn.microsoft.com/en-us/openapi/kiota/using#--structured-mime-types--m). |
| `--skip-generation \| --sg` | No | true | When specified, the generation would be skipped. Defaults to false. |
| `--output \| -o` | No | ./generated/graph/csharp | The output directory or file path for the generated code files. This is relative to the location of `kiota-config.json`. Defaults to `./output`. |

> [!NOTE]
> It is not required to use the CLI to add new clients. It is possible to add a new client by adding a new entry in the `clients` section of the `kiota-config.json` file. See the [kiota-config.json schema](../schemas/kiota-config.json) for more information. Using `kiota client generate --client-name myClient` would be required to generate the code files.
## Using `kiota client add`

```bash
kiota client add --client-name "graphDelegated" --openapi "https://aka.ms/graph/v1.0/openapi.yaml" --include-path "**/users/**" --language csharp --class-name "GraphClient" --namespace-name "Contoso.GraphApp" --backing-store --exclude-backward-compatible --serializer "Contoso.Json.CustomSerializer" --deserializer "Contoso.Json.CustomDeserializer" -structured-mime-types "application/json" --output "./generated/graph/csharp"
```

_The resulting `kiota-config.json` file will look like this:_

```jsonc
{
"version": "1.0.0",
"clients": {
"graphDelegated": {
"descriptionLocation": "https://aka.ms/graph/v1.0/openapi.yaml",
"includePatterns": ["**/users/**"],
"excludePatterns": [],
"language": "csharp",
"outputPath": "./generated/graph/csharp",
"clientClassName": "GraphClient",
"clientNamespaceName": "Contoso.GraphApp",
"structuredMediaTypes": [
"application/json"
],
"usesBackingStore": true,
"includeAdditionalData": true
}
}
}
```

_The resulting `apimanifest.json` file will look like this:_

```jsonc
{
"apiDependencies": {
"graphDelegated": {
"x-ms-kiotaHash": "9EDF8506CB74FE44...",
"x-ms-kiotaVersion": "1.11.0",
"apiDescriptionUrl": "https://aka.ms/graph/v1.0/openapi.yaml",
"apiDeploymentBaseUrl": "https://graph.microsoft.com",
"apiDescriptionVersion": "v1.0",
"requests": [
{
"method": "GET",
"uriTemplate": "/users"
},
{
"method": "POST",
"uriTemplate": "/users"
},
{
"method": "GET",
"uriTemplate": "/users/$count"
},
{
"method": "GET",
"uriTemplate": "/users/{user-id}"
},
{
"method": "PATCH",
"uriTemplate": "/users/{user-id}"
},
{
"method": "DELETE",
"uriTemplate": "/users/{user-id}"
}
]
}
}
}
```

## File structure
```bash
/
└─.kiota
└─definitions
└─graphDelegated.yaml
└─generated
└─graph
└─csharp
└─... # Generated code files
└─GraphClient.cs
└─apimanifest.json
└─kiota-config.json
```
116 changes: 116 additions & 0 deletions specs/cli/client-edit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# kiota client edit

## Description

`kiota client update` allows a developer to edit an existing API client int the `kiota-config.json` file. If either the `kiota-config.json` file or if the `--client-name` client can't be found within the `kiota-config.json` file, the command should error out and let the developer know.

When executing, the API entry defined by the `--client-name` parameter will be modified. All parameters should be supported and the only required one is `--client-name`. All others are optional as they would only modify the configuration of the API client. If the OpenAPI description location changed or any properties of the client entry in `kiota-config.json`, a new hash will be generated and and would trigger an update to the [API Manifest](https://www.ietf.org/archive/id/draft-miller-api-manifest-01.html#section-2.5-3).

Once the `kiota-config.json` file and the API Manifest are updated, the code generation will be executed based on the newly updated API client configuration.

## Parameters

| Parameters | Required | Example | Description |
| -- | -- | -- | -- |
| `--client-name \| --cn` | Yes | graphDelegated | Name of the client. Unique within the parent API. If not provided, defaults to --class-name or its default. |
| `--openapi \| -d` | No | https://aka.ms/graph/v1.0/openapi.yaml | The location of the OpenAPI description in JSON or YAML format to use to generate the SDK. Accepts a URL or a local path. |
| `--include-path \| -i` | No | /me/chats#GET | A glob pattern to include paths from generation. Accepts multiple values. Defaults to no value which includes everything. |
| `--exclude-path \| -e` | No | \*\*/users/\*\* | A glob pattern to exclude paths from generation. Accepts multiple values. Defaults to no value which excludes nothing. |
| `--language \| -l` | No | csharp | The target language for the generated code files or for the information. |
| `--class-name \| -c` | No | GraphClient | The name of the client class. Defaults to `Client`. |
| `--namespace-name \| -n` | No | Contoso.GraphApp | The namespace of the client class. Defaults to `Microsoft.Graph`. |
| `--backing-store \| -b` | No | | Defaults to `false` |
| `--exclude-backward-compatible \| --ebc` | No | | Whether to exclude the code generated only for backward compatibility reasons or not. Defaults to `false`. |
| `--structured-media-types \| -m` | No | `application/json` |Any valid media type which will match a request body type or a response type in the OpenAPI description. Default are documented [here](https://learn.microsoft.com/en-us/openapi/kiota/using#--structured-mime-types--m). |
| `--skip-generation \| --sg` | No | true | When specified, the generation would be skipped. Defaults to false. |
| `--output \| -o` | No | ./generated/graph/csharp | The output directory or file path for the generated code files. Defaults to `./output`. |

> [!NOTE]
> It is not required to use the CLI to edit clients. It is possible to edit a client by modifying its entry in the `clients` section of the `kiota-config.json` file. See the [kiota-config.json schema](../schemas/kiota-config.json.md) for more information.
## Using `kiota client edit`

```bash
kiota client edit --client-name "graphDelegated" --class-name "GraphServiceClient" --exclude-path "/users/$count"
```

_The resulting `kiota-config.json` file will look like this:_

```jsonc
{
"version": "1.0.0",
"clients": {
"graphDelegated": {
"descriptionLocation": "https://aka.ms/graph/v1.0/openapi.yaml",
"includePatterns": ["**/users/**"],
"excludePatterns": ["/users/$count"],
"language": "csharp",
"outputPath": "./generated/graph/csharp",
"clientClassName": "GraphServiceClient",
"clientNamespaceName": "Contoso.GraphApp",
"structuredMediaTypes": [
"application/json"
],
"usesBackingStore": true,
"includeAdditionalData": true
}
}
}
```

_The resulting `apimanifest.json` file will look like this:_

```jsonc
{
"publisher": {
"name": "Microsoft Graph",
"contactEmail": "[email protected]"
},
"apiDependencies": {
"graphDelegated": {
"x-ms-kiotaHash": "9EDF8506CB74FE44...",
"x-ms-kiotaVersion": "1.11.0",
"apiDescriptionUrl": "https://aka.ms/graph/v1.0/openapi.yaml",
"apiDeploymentBaseUrl": "https://graph.microsoft.com",
"apiDescriptionVersion": "v1.0",
"requests": [
{
"method": "GET",
"uriTemplate": "/users"
},
{
"method": "POST",
"uriTemplate": "/users"
},
{
"method": "GET",
"uriTemplate": "/users/{user-id}"
},
{
"method": "PATCH",
"uriTemplate": "/users/{user-id}"
},
{
"method": "DELETE",
"uriTemplate": "/users/{user-id}"
}
]
}
}
}
```

## File structure
```bash
/
└─.kiota
└─definitions
└─graphDelegated.yaml
└─generated
└─graph
└─csharp
└─... # Generated code files
└─GraphClient.cs
└─apimanifest.json
└─kiota-config.json
```
36 changes: 36 additions & 0 deletions specs/cli/client-generate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# kiota client generate

## Description

Now that we have a `kiota-config.json` file, all the parameters required to generate the code are stored in the file. The `kiota client generate` command will read the `kiota-config.json` file and generate the code for each of the available clients.

It's also possible to specify for which API and client the code should be generated. This is useful when a project contains multiple clients. The `kiota client generate --client-name "MyClient"` command will read the `kiota-config.json` file and generate the code for the specified client. If it can't find the specified API or client, it will throw an error.

In general cases, the `kiota client generate` command will generate the code for all the clients in the `kiota-config.json` file based on the cached OpenAPI description. If the `--refresh` parameter is provided, the command will refresh the cached OpenAPI description(s), update the different `x-ms-kiotaHash` in the API Manifest and then generate the code for the specified clients.

## Parameters

| Parameters | Required | Example | Description |
| -- | -- | -- | -- |
| `--client-name \| --cn` | No | graphDelegated | Name of the client. Unique within the parent API. |
| `--refresh \| -r` | No | true | Provided when refreshing the description(s) is required. |

## Usage

### Using `kiota client generate` for a single API client

```bash
kiota client generate --client-name "graphDelegated"
```

### Using `kiota client generate` for all API clients

```bash
kiota client generate
```

### Using `kiota client generate` for all API clients and refresh their descriptions

```bash
kiota client generate
```
45 changes: 45 additions & 0 deletions specs/cli/client-remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# kiota client remove

## Description

`kiota client remove` allows a developer to remove an existing client from the `kiota-config.json` file. The command will remove the entry from the `clients` section of `kiota-config.json` file. The command has a single required parameters; the name of the client.

The command also has one optional parameter, the ability to remove the generated client. If provided, kiota will delete the folder and its content specified at the `outputPath` from the client configuration. It will also remove the local version of the OpenAPI description file (specified by the `descriptionHash` property). The API Manifest is also updated to remove the dependency from the list of dependencies.

| Parameters | Required | Example | Description |
| -- | -- | -- | -- |
| `--client-name \| --cn` | Yes | graphDelegated | Name of the client |
| `--clean-output \| --co` | No | | Cleans the generated client |

#### Using kiota client remove

```bash
kiota client remove --client-name "graphDelegated" --clean-output
```

The resulting `kiota-config.json` file will look like this:

```jsonc
{
"version": "1.0.0",
"clients": { }
}
```

_The resulting `apimanifest.json` file will look like this:_

```jsonc
{
"apiDependencies": { }
}
```

## File structure
```bash
/
└─.kiota
└─generated
└─graph
└─kiota-config.json
└─apimanifest.json
```
35 changes: 35 additions & 0 deletions specs/cli/config-init.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# `kiota config init`

## Description

`kiota config init` creates a new kiota-config.json file with the provided parameters. If the file already exists, it should error out and report it to the user. As the file gets created, it should be adding a `version` property with the value of the `kiota-config.json` current schema version.

When `kiota config init` is executed, a `kiota-config.json` file would be created in the current directory where the command is being executed. If the user wants to create the file in a different directory, they should use the `--config-file` global parameter.

> [!NOTE]
> If a project only needs a single API, using `kiota config init` is not mandatory as generating code using the `kiota client generate` command could generate a `kiota-config.json` file with values coming from the `kiota client generate` command (if no `kiota-config.json` is present). See [kiota client generate](./client-generate.md) for more information.
## Parameters

None.

## Using `kiota config init`

```bash
kiota config init
```

_The resulting `kiota-config.json` file will look like this:_

```jsonc
{
"version": "1.0.0",
}
```

## File structure
```bash
/
└─.kiota
└─kiota-config.json
```
Loading

0 comments on commit 6b61fd1

Please sign in to comment.