11# Created with Openapi Generator
22
33<a id =" cli" ></a >
4- ## Run the following powershell command to generate the library
5-
6- ```ps1
7- $properties = @(
8- 'apiName={ {apiName} }',
9- 'targetFramework={ {targetFramework} }',
10- 'validatable={ {validatable} }',
11- 'nullableReferenceTypes={ {nullableReferenceTypes} }',
12- 'hideGenerationTimestamp={ {hideGenerationTimestamp} }',
13- 'packageVersion={ {packageVersion} }',
14- 'packageAuthors={ {packageAuthors} }',
15- 'packageCompany={ {packageCompany} }',
16- 'packageCopyright={ {packageCopyright} }',
17- 'packageDescription={ {packageDescription} }',{ {#licenseId} }
18- 'licenseId={ {.} }',{ {/licenseId} }
19- 'packageName={ {packageName} }',
20- 'packageTags={ {packageTags} }',
21- 'packageTitle={ {packageTitle} }'
22- ) -join ","
23-
24- $global = @(
25- 'apiDocs={ {generateApiDocs} }',
26- 'modelDocs={ {generateModelDocs} }',
27- 'apiTests={ {generateApiTests} }',
28- 'modelTests={ {generateModelTests} }'
29- ) -join ","
30-
31- java -jar "<path >/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar" generate `
32- -g csharp-netcore `
33- -i <your-swagger-file >.yaml `
34- -o <your-output-folder > `
35- --library generichost `
36- --additional-properties $properties `
37- --global-property $global `
38- --git-host "{ {gitHost} }" `
39- --git-repo-id "{ {gitRepoId} }" `
40- --git-user-id "{ {gitUserId} }" `
41- --release-note "{ {releaseNote} }"
42- # -t templates
4+ ## Creating the library
5+ Create a config.yaml file similar to what is below, then run the following powershell command to generate the library `java -jar "<path >/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar" generate -c config.yaml`
6+
7+ ```yaml
8+ generatorName: csharp
9+ inputSpec: { {inputSpec} }
10+ outputDir: out
11+
12+ # https://openapi-generator.tech/docs/generators/csharp
13+ additionalProperties:
14+ packageGuid: '{ {packageGuid} }'
15+
16+ # https://openapi-generator.tech/docs/integrations/#github-integration
17+ # gitHost:
18+ # gitUserId:
19+ # gitRepoId:
20+
21+ # https://openapi-generator.tech/docs/globals
22+ # globalProperties:
23+
24+ # https://openapi-generator.tech/docs/customization/#inline-schema-naming
25+ # inlineSchemaOptions:
26+
27+ # https://openapi-generator.tech/docs/customization/#name-mapping
28+ # modelNameMappings:
29+ # nameMappings:
30+
31+ # https://openapi-generator.tech/docs/customization/#openapi-normalizer
32+ # openapiNormalizer:
33+
34+ # templateDir: https://openapi-generator.tech/docs/templating/#modifying-templates
35+
36+ # releaseNote:
4337```
4438
4539<a id =" usage" ></a >
4640## Using the library in your project
4741
4842```cs
49- using System;
50- using System.Threading.Tasks;
5143using Microsoft.Extensions.Hosting;
5244using Microsoft.Extensions.DependencyInjection;
5345using { {packageName} }.Api;
5446using { {packageName} }.Client;
5547using { {packageName} }.Model;
48+ using Org.OpenAPITools.Extensions;
5649
5750namespace YourProject
5851{
5952 public class Program
6053 {
6154 public static async Task Main(string[] args)
6255 {
63- var host = CreateHostBuilder(args).Build();{{#apiInfo} }{ {#apis} }{ {#-first} }
56+ var host = CreateHostBuilder(args).Build();
57+ {{#apiInfo} }
58+ { {#apis} }
59+ { {#-first} }
6460 var api = host.Services.GetRequiredService<{ {interfacePrefix} }{ {classname} }>();
6561 { {#operations} }
6662 { {#-first} }
6763 { {#operation} }
6864 { {#-first} }
69- { {operationId} }ApiResponse apiResponse = await api.{ {operationId} }Async("todo");
70- { {#returnType} }{ {{.} }}{ {/returnType} }{ {^returnType} }object{ {/returnType} } model = apiResponse.Ok();
65+ { {interfacePrefix } } { { operationId} }ApiResponse apiResponse = await api.{ {operationId} }Async("todo");
66+ { {#returnType} }{ {{.} }}{ {/returnType} }{ {^returnType} }object{ {/returnType} }{ {nrt? } } model = apiResponse.Ok();
7167 { {/-first} }
7268 { {/operation} }
7369 { {/-first} }
@@ -78,16 +74,17 @@ namespace YourProject
7874 }
7975
8076 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
81- .Configure{ {apiName} }((context, options) =>
77+ .Configure{ {apiName} }((context, services, options) =>
8278 {
8379 {{#authMethods} }
8480 { {#-first} }
85- // the type of token here depends on the api security specifications
86- ApiKeyToken token = new("<your token >", ClientUtils.ApiKeyHeader.Authorization);
81+ // The type of token here depends on the api security specifications
82+ // Available token types are ApiKeyToken, BasicToken, BearerToken, HttpSigningToken, and OAuthToken.
83+ BearerToken token = new("<your token >");
8784 options.AddTokens(token);
8885
8986 // optionally choose the method the tokens will be provided with, default is RateLimitProvider
90- options.UseProvider<RateLimitProvider <ApiKeyToken >, ApiKeyToken >();
87+ options.UseProvider<RateLimitProvider <BearerToken >, BearerToken >();
9188
9289 { {/-first} }
9390 { {/authMethods} }
@@ -96,11 +93,17 @@ namespace YourProject
9693 // your custom converters if any
9794 } );
9895
99- options.Add{ {apiName} }HttpClients(builder: builder => builder
100- .AddRetryPolicy(2)
101- .AddTimeoutPolicy(TimeSpan.FromSeconds(5))
102- .AddCircuitBreakerPolicy(10, TimeSpan.FromSeconds(30))
103- // add whatever middleware you prefer
96+ options.Add{ {apiName} }HttpClients(client =>
97+ {
98+ // client configuration
99+ } , builder =>
100+ {
101+ builder
102+ .AddRetryPolicy(2)
103+ .AddTimeoutPolicy(TimeSpan.FromSeconds(5))
104+ .AddCircuitBreakerPolicy(10, TimeSpan.FromSeconds(30));
105+ // add whatever middleware you prefer
106+ }
104107 );
105108 });
106109 }
@@ -110,136 +113,28 @@ namespace YourProject
110113## Questions
111114
112115- What about HttpRequest failures and retries?
113- If supportsRetry is enabled, you can configure Polly in the ConfigureClients method.
116+ Configure Polly in the IHttpClientBuilder
114117- How are tokens used?
115118 Tokens are provided by a TokenProvider class. The default is RateLimitProvider which will perform client side rate limiting.
116119 Other providers can be used with the UseProvider method.
117120- Does an HttpRequest throw an error when the server response is not Ok?
118- It depends how you made the request. If the return type is ApiResponse<T > no error will be thrown, though the Content property will be null.
121+ It depends how you made the request. If the return type is ApiResponse<T > no error will be thrown, though the Content property will be null.
119122 StatusCode and ReasonPhrase will contain information about the error.
120123 If the return type is T, then it will throw. If the return type is TOrDefault, it will return null.
121124- How do I validate requests and process responses?
122- Use the provided On and After methods in the Api class from the namespace { {packageName} }.Rest.DefaultApi.
123- Or provide your own class by using the generic Configure{ {apiName} } method.
124-
125- <a id =" dependencies" ></a >
126- ## Dependencies
127-
128- - [Microsoft.Extensions.Hosting](https://www.nuget.org/packages/Microsoft.Extensions.Hosting/) - 5.0.0 or later
129- - [Microsoft.Extensions.Http](https://www.nuget.org/packages/Microsoft.Extensions.Http/) - 5.0.0 or later{ {#supportsRetry} }
130- - [Microsoft.Extensions.Http.Polly](https://www.nuget.org/packages/Microsoft.Extensions.Http.Polly/) - 5.0.1 or later{ {/supportsRetry} }{ {#useCompareNetObjects} }
131- - [CompareNETObjects](https://www.nuget.org/packages/CompareNETObjects) - 4.61.0 or later{ {/useCompareNetObjects} }{ {#validatable} }
132- - [System.ComponentModel.Annotations](https://www.nuget.org/packages/System.ComponentModel.Annotations) - 4.7.0 or later{ {/validatable} }{ {#apiDocs} }
133-
134- <a id =" documentation-for-api-endpoints" ></a >
135- ## Documentation for API Endpoints
136-
137- All URIs are relative to *{ {{basePath} }}*
138-
139- Class | Method | HTTP request | Description
140- ------------ | ------------- | ------------- | -------------{ {#apiInfo} }{ {#apis} }{ {#operations} }{ {#operation} }
141- *{ {classname} }* | [**{ {operationId} }**]({ {apiDocPath} }{ {classname} }.md#{ {operationIdLowerCase} }) | **{ {httpMethod} }** { {path} } | { {#summary} }{ {{summary} }}{ {/summary} }{ {/operation} }{ {/operations} }{ {/apis} }{ {/apiInfo} }{ {/apiDocs} }{ {#modelDocs} }
142-
143- <a id =" documentation-for-models" ></a >
144- ## Documentation for Models
145-
146- { {#modelPackage} }{ {#models} }{ {#model} } - [{ {{modelPackage} }}.{ {{classname} }}]({ {modelDocPath} }{ {{classname} }}.md){ {/model} }{ {/models} }{ {/modelPackage} }
147- { {^modelPackage} }No model defined in this package{ {/modelPackage} }{ {/modelDocs} }
148-
149- <a id =" documentation-for-authorization" ></a >
150- ## Documentation for Authorization
151-
152- { {^authMethods} }Endpoints do not require authorization.{ {/authMethods} }
153- { {#hasAuthMethods} }Authentication schemes defined for the API:{ {/hasAuthMethods} }
154- { {#authMethods} }
155- <a id =" { { name} } " ></a >
156- ### { {name} }
157-
158- { {#isApiKey} }- **Type**: API key
159- - **API key parameter name**: { {keyParamName} }
160- - **Location**: { {#isKeyInQuery} }URL query string{ {/isKeyInQuery} }{ {#isKeyInHeader} }HTTP header{ {/isKeyInHeader} }
161- { {/isApiKey} }
162- { {#isBasicBasic} }
163- - **Type**: HTTP basic authentication
164- { {/isBasicBasic} }
165- { {#isBasicBearer} }
166- - **Type**: Bearer Authentication
167- { {/isBasicBearer} }
168- { {#isHttpSignature} }
169- - **Type**: HTTP signature authentication
170- { {/isHttpSignature} }
171- { {#isOAuth} }
172- - **Type**: OAuth
173- - **Flow**: { {flow} }
174- - **Authorization URL**: { {authorizationUrl} }
175- - **Scopes**: { {^scopes} }N/A{ {/scopes} }{ {#scopes} }
176- - { {scope} }: { {description} }{ {/scopes} }
177- { {/isOAuth} }
178-
179- { {/authMethods} }
125+ Use the provided On and After partial methods in the api classes.
126+
127+ ## Api Information
128+ - appName: { {appName} }
129+ - appVersion: { {appVersion} }
130+ - appDescription: { {appDescription} }
180131
181132## Build
133+ This C# SDK is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
134+
182135- SDK version: { {packageVersion} }
183136{ {^hideGenerationTimestamp} }
184137- Build date: { {generatedDate} }
185138{ {/hideGenerationTimestamp} }
186139- Generator version: { {generatorVersion} }
187140- Build package: { {generatorClass} }
188-
189- ## Api Information
190- - appName: { {appName} }
191- - appVersion: { {appVersion} }
192- - appDescription: { {appDescription} }
193-
194- ## [OpenApi Global properties](https://openapi-generator.tech/docs/globals)
195- - generateAliasAsModel: { {generateAliasAsModel} }
196- - supportingFiles: { {supportingFiles} }
197- - models: omitted for brevity
198- - apis: omitted for brevity
199- - apiDocs: { {generateApiDocs} }
200- - modelDocs: { {generateModelDocs} }
201- - apiTests: { {generateApiTests} }
202- - modelTests: { {generateModelTests} }
203-
204- ## [OpenApi Generator Parameters](https://openapi-generator.tech/docs/generators/csharp-netcore)
205- - allowUnicodeIdentifiers: { {allowUnicodeIdentifiers} }
206- - apiName: { {apiName} }
207- - caseInsensitiveResponseHeaders: { {caseInsensitiveResponseHeaders} }
208- - conditionalSerialization: { {conditionalSerialization} }
209- - disallowAdditionalPropertiesIfNotPresent: { {disallowAdditionalPropertiesIfNotPresent} }
210- - gitHost: { {gitHost} }
211- - gitRepoId: { {gitRepoId} }
212- - gitUserId: { {gitUserId} }
213- - hideGenerationTimestamp: { {hideGenerationTimestamp} }
214- - interfacePrefix: { {interfacePrefix} }
215- - library: { {library} }
216- - licenseId: { {licenseId} }
217- - modelPropertyNaming: { {modelPropertyNaming} }
218- - netCoreProjectFile: { {netCoreProjectFile} }
219- - nonPublicApi: { {nonPublicApi} }
220- - nullableReferenceTypes: { {nullableReferenceTypes} }
221- - optionalAssemblyInfo: { {optionalAssemblyInfo} }
222- - optionalEmitDefaultValues: { {optionalEmitDefaultValues} }
223- - optionalMethodArgument: { {optionalMethodArgument} }
224- - optionalProjectFile: { {optionalProjectFile} }
225- - packageAuthors: { {packageAuthors} }
226- - packageCompany: { {packageCompany} }
227- - packageCopyright: { {packageCopyright} }
228- - packageDescription: { {packageDescription} }
229- - packageGuid: { {packageGuid} }
230- - packageName: { {packageName} }
231- - packageTags: { {packageTags} }
232- - packageTitle: { {packageTitle} }
233- - packageVersion: { {packageVersion} }
234- - releaseNote: { {releaseNote} }
235- - returnICollection: { {returnICollection} }
236- - sortParamsByRequiredFlag: { {sortParamsByRequiredFlag} }
237- - sourceFolder: { {sourceFolder} }
238- - targetFramework: { {targetFramework} }
239- - useCollection: { {useCollection} }
240- - useDateTimeOffset: { {useDateTimeOffset} }
241- - useOneOfDiscriminatorLookup: { {useOneOfDiscriminatorLookup} }
242- - validatable: { {validatable} }{ {#infoUrl} }
243- For more information, please visit [{ {{.} }}]({ {{.} }}){ {/infoUrl} }
244-
245- This C# SDK is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
0 commit comments