
csharp.libraries.generichost.README.client.mustache Maven / Gradle / Ivy
The newest version!
# Created with Openapi Generator
## Creating the library
Create a config.yaml file similar to what is below, then run the following powershell command to generate the library `java -jar "/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar" generate -c config.yaml`
```yaml
generatorName: csharp
inputSpec: {{inputSpec}}
outputDir: out
# https://openapi-generator.tech/docs/generators/csharp
additionalProperties:
packageGuid: '{{packageGuid}}'
# https://openapi-generator.tech/docs/integrations/#github-integration
# gitHost:
# gitUserId:
# gitRepoId:
# https://openapi-generator.tech/docs/globals
# globalProperties:
# https://openapi-generator.tech/docs/customization/#inline-schema-naming
# inlineSchemaOptions:
# https://openapi-generator.tech/docs/customization/#name-mapping
# modelNameMappings:
# nameMappings:
# https://openapi-generator.tech/docs/customization/#openapi-normalizer
# openapiNormalizer:
# templateDir: https://openapi-generator.tech/docs/templating/#modifying-templates
# releaseNote:
```
## Using the library in your project
```cs
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using {{packageName}}.Api;
using {{packageName}}.Client;
using {{packageName}}.Model;
using Org.OpenAPITools.Extensions;
namespace YourProject
{
public class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
{{#apiInfo}}
{{#apis}}
{{#-first}}
var api = host.Services.GetRequiredService<{{interfacePrefix}}{{classname}}>();
{{#operations}}
{{#-first}}
{{#operation}}
{{#-first}}
{{interfacePrefix}}{{operationId}}ApiResponse apiResponse = await api.{{operationId}}Async("todo");
{{#returnType}}{{{.}}}{{/returnType}}{{^returnType}}object{{/returnType}}{{nrt?}} model = apiResponse.Ok();
{{/-first}}
{{/operation}}
{{/-first}}
{{/operations}}
{{/-first}}
{{/apis}}
{{/apiInfo}}
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.Configure{{apiName}}((context, services, options) =>
{
{{#authMethods}}
{{#-first}}
// The type of token here depends on the api security specifications
// Available token types are ApiKeyToken, BasicToken, BearerToken, HttpSigningToken, and OAuthToken.
BearerToken token = new("");
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider, BearerToken>();
{{/-first}}
{{/authMethods}}
options.ConfigureJsonOptions((jsonOptions) =>
{
// your custom converters if any
});
options.Add{{apiName}}HttpClients(client =>
{
// client configuration
}, builder =>
{
builder
.AddRetryPolicy(2)
.AddTimeoutPolicy(TimeSpan.FromSeconds(5))
.AddCircuitBreakerPolicy(10, TimeSpan.FromSeconds(30));
// add whatever middleware you prefer
}
);
});
}
}
```
## Questions
- What about HttpRequest failures and retries?
Configure Polly in the IHttpClientBuilder
- How are tokens used?
Tokens are provided by a TokenProvider class. The default is RateLimitProvider which will perform client side rate limiting.
Other providers can be used with the UseProvider method.
- Does an HttpRequest throw an error when the server response is not Ok?
It depends how you made the request. If the return type is ApiResponse no error will be thrown, though the Content property will be null.
StatusCode and ReasonPhrase will contain information about the error.
If the return type is T, then it will throw. If the return type is TOrDefault, it will return null.
- How do I validate requests and process responses?
Use the provided On and After partial methods in the api classes.
## Api Information
- appName: {{appName}}
- appVersion: {{appVersion}}
- appDescription: {{appDescription}}
## Build
This C# SDK is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
- SDK version: {{packageVersion}}
{{^hideGenerationTimestamp}}
- Build date: {{generatedDate}}
{{/hideGenerationTimestamp}}
- Generator version: {{generatorVersion}}
- Build package: {{generatorClass}}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy