forked from loafle/openapi-generator-original
Created with Openapi Generator
Run the following powershell command to generate the library
$properties = @(
'apiName=Api',
'targetFramework=netstandard2.0',
'validatable=true',
'nullableReferenceTypes=',
'hideGenerationTimestamp=true',
'packageVersion=1.0.0',
'packageAuthors=OpenAPI',
'packageCompany=OpenAPI',
'packageCopyright=No Copyright',
'packageDescription=A library generated from a OpenAPI doc',
'packageName=Org.OpenAPITools',
'packageTags=',
'packageTitle=OpenAPI Library'
) -join ","
$global = @(
'apiDocs=true',
'modelDocs=true',
'apiTests=true',
'modelTests=true'
) -join ","
java -jar "<path>/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar" generate `
-g csharp-netcore `
-i <your-swagger-file>.yaml `
-o <your-output-folder> `
--library generichost `
--additional-properties $properties `
--global-property $global `
--git-host "github.com" `
--git-repo-id "GIT_REPO_ID" `
--git-user-id "GIT_USER_ID" `
--release-note "Minor update"
# -t templates
Using the library in your project
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;
namespace YourProject
{
public class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
var api = host.Services.GetRequiredService<IAnotherFakeApi>();
Call123TestSpecialTagsApiResponse apiResponse = await api.Call123TestSpecialTagsAsync("todo");
ModelClient model = apiResponse.Ok();
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureApi((context, options) =>
{
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Authorization);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
options.ConfigureJsonOptions((jsonOptions) =>
{
// your custom converters if any
});
options.AddApiHttpClients(builder: 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? If supportsRetry is enabled, you can configure Polly in the ConfigureClients method.
- 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 methods in the Api class from the namespace Org.OpenAPITools.Rest.DefaultApi. Or provide your own class by using the generic ConfigureApi method.
Dependencies
- Microsoft.Extensions.Hosting - 5.0.0 or later
- Microsoft.Extensions.Http - 5.0.0 or later
- Microsoft.Extensions.Http.Polly - 5.0.1 or later
- CompareNETObjects - 4.61.0 or later
- System.ComponentModel.Annotations - 4.7.0 or later
Documentation for Authorization
Authentication schemes defined for the API:
petstore_auth
- Type: OAuth
- Flow: implicit
- Authorization URL: http://petstore.swagger.io/api/oauth/dialog
- Scopes:
- write:pets: modify pets in your account
- read:pets: read your pets
api_key
- Type: API key
- API key parameter name: api_key
- Location: HTTP header
api_key_query
- Type: API key
- API key parameter name: api_key_query
- Location: URL query string
http_basic_test
- Type: HTTP basic authentication
bearer_test
- Type: Bearer Authentication
http_signature_test
- Type: HTTP signature authentication
Build
- SDK version: 1.0.0
- Build package: org.openapitools.codegen.languages.CSharpClientCodegen
Api Information
- appName: OpenAPI Petstore
- appVersion: 1.0.0
- appDescription: This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: " \
OpenApi Global properties
- generateAliasAsModel:
- supportingFiles:
- models: omitted for brevity
- apis: omitted for brevity
- apiDocs: true
- modelDocs: true
- apiTests: true
- modelTests: true
- withXml:
OpenApi Generator Parameters
- allowUnicodeIdentifiers:
- apiName: Api
- caseInsensitiveResponseHeaders:
- conditionalSerialization: false
- disallowAdditionalPropertiesIfNotPresent: false
- gitHost: github.com
- gitRepoId: GIT_REPO_ID
- gitUserId: GIT_USER_ID
- hideGenerationTimestamp: true
- interfacePrefix: I
- library: generichost
- licenseId:
- modelPropertyNaming:
- netCoreProjectFile: false
- nonPublicApi: false
- nullableReferenceTypes:
- optionalAssemblyInfo:
- optionalEmitDefaultValues: false
- optionalMethodArgument: true
- optionalProjectFile:
- packageAuthors: OpenAPI
- packageCompany: OpenAPI
- packageCopyright: No Copyright
- packageDescription: A library generated from a OpenAPI doc
- packageGuid: {321C8C3F-0156-40C1-AE42-D59761FB9B6C}
- packageName: Org.OpenAPITools
- packageTags:
- packageTitle: OpenAPI Library
- packageVersion: 1.0.0
- releaseNote: Minor update
- returnICollection: false
- sortParamsByRequiredFlag:
- sourceFolder: src
- targetFramework: netstandard2.0
- useCollection: false
- useDateTimeOffset: false
- useOneOfDiscriminatorLookup: false
- validatable: true
This C# SDK is automatically generated by the OpenAPI Generator project.