1
1
# Created with Openapi Generator
2
2
3
3
<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:
43
37
```
44
38
45
39
<a id =" usage" ></a >
46
40
## Using the library in your project
47
41
48
42
```cs
49
- using System;
50
- using System.Threading.Tasks;
51
43
using Microsoft.Extensions.Hosting;
52
44
using Microsoft.Extensions.DependencyInjection;
53
45
using { {packageName} }.Api;
54
46
using { {packageName} }.Client;
55
47
using { {packageName} }.Model;
48
+ using Org.OpenAPITools.Extensions;
56
49
57
50
namespace YourProject
58
51
{
59
52
public class Program
60
53
{
61
54
public static async Task Main(string[] args)
62
55
{
63
- var host = CreateHostBuilder(args).Build();{{#apiInfo} }{ {#apis} }{ {#-first} }
56
+ var host = CreateHostBuilder(args).Build();
57
+ {{#apiInfo} }
58
+ { {#apis} }
59
+ { {#-first} }
64
60
var api = host.Services.GetRequiredService<{ {interfacePrefix} }{ {classname} }>();
65
61
{ {#operations} }
66
62
{ {#-first} }
67
63
{ {#operation} }
68
64
{ {#-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();
71
67
{ {/-first} }
72
68
{ {/operation} }
73
69
{ {/-first} }
@@ -78,16 +74,17 @@ namespace YourProject
78
74
}
79
75
80
76
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
81
- .Configure{ {apiName} }((context, options) =>
77
+ .Configure{ {apiName} }((context, services, options) =>
82
78
{
83
79
{{#authMethods} }
84
80
{ {#-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 >");
87
84
options.AddTokens(token);
88
85
89
86
// 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 >();
91
88
92
89
{ {/-first} }
93
90
{ {/authMethods} }
@@ -96,11 +93,17 @@ namespace YourProject
96
93
// your custom converters if any
97
94
} );
98
95
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
+ }
104
107
);
105
108
});
106
109
}
@@ -110,136 +113,28 @@ namespace YourProject
110
113
## Questions
111
114
112
115
- 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
114
117
- How are tokens used?
115
118
Tokens are provided by a TokenProvider class. The default is RateLimitProvider which will perform client side rate limiting.
116
119
Other providers can be used with the UseProvider method.
117
120
- 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.
119
122
StatusCode and ReasonPhrase will contain information about the error.
120
123
If the return type is T, then it will throw. If the return type is TOrDefault, it will return null.
121
124
- 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} }
180
131
181
132
## Build
133
+ This C# SDK is automatically generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
134
+
182
135
- SDK version: { {packageVersion} }
183
136
{ {^hideGenerationTimestamp} }
184
137
- Build date: { {generatedDate} }
185
138
{ {/hideGenerationTimestamp} }
186
139
- Generator version: { {generatorVersion} }
187
140
- 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