diff --git a/templates/openapi-generator/csharp/ApiClient.mustache b/templates/openapi-generator/csharp/ApiClient.mustache index ca24f88f..3e774675 100644 --- a/templates/openapi-generator/csharp/ApiClient.mustache +++ b/templates/openapi-generator/csharp/ApiClient.mustache @@ -26,6 +26,9 @@ using FileIO = System.IO.File; {{#supportsRetry}} using Polly; {{/supportsRetry}} +{{#hasOAuthMethods}} +using {{packageName}}.Client.Auth; +{{/hasOAuthMethods}} using {{packageName}}.{{modelPackage}}; namespace {{packageName}}.Client @@ -361,7 +364,7 @@ namespace {{packageName}}.Client request.RequestFormat = DataFormat.Json; } - request.AddParameter("application/json", JsonConvert.SerializeObject(options.Data), ParameterType.RequestBody); + request.AddJsonBody(options.Data); } } @@ -462,11 +465,29 @@ namespace {{packageName}}.Client ClientCertificates = configuration.ClientCertificates, Timeout = configuration.Timeout, Proxy = configuration.Proxy, + UserAgent = configuration.UserAgent, UseDefaultCredentials = configuration.UseDefaultCredentials, RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback }; setOptions(clientOptions); + + {{#hasOAuthMethods}} + if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) && + !string.IsNullOrEmpty(configuration.OAuthClientId) && + !string.IsNullOrEmpty(configuration.OAuthClientSecret) && + configuration.OAuthFlow != null) + { + clientOptions.Authenticator = new OAuthAuthenticator( + configuration.OAuthTokenUrl, + configuration.OAuthClientId, + configuration.OAuthClientSecret, + configuration.OAuthScope, + configuration.OAuthFlow, + SerializerSettings, + configuration); + } + {{/hasOAuthMethods}} using (RestClient client = new RestClient(clientOptions, configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)))) { diff --git a/templates/openapi-generator/csharp/Configuration.mustache b/templates/openapi-generator/csharp/Configuration.mustache index c3e90a93..39dbd9c0 100644 --- a/templates/openapi-generator/csharp/Configuration.mustache +++ b/templates/openapi-generator/csharp/Configuration.mustache @@ -14,6 +14,8 @@ using System.Text; using System.Net.Http; using System.Net.Security; {{#useRestSharp}} +{{#hasOAuthMethods}}using {{packageName}}.Client.Auth; +{{/hasOAuthMethods}} {{/useRestSharp}} namespace {{packageName}}.Client @@ -124,6 +126,7 @@ namespace {{packageName}}.Client public Configuration() { Proxy = null; + UserAgent = WebUtility.UrlEncode("{{httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}"); BasePath = "{{{basePath}}}"; DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary(); ApiKey = new {{^net35}}Concurrent{{/net35}}Dictionary(); @@ -246,7 +249,7 @@ namespace {{packageName}}.Client /// /// Gets or sets the base path for API access. /// - public virtual string BasePath + public virtual string BasePath { get { return _basePath; } set { _basePath = value; } @@ -344,6 +347,38 @@ namespace {{packageName}}.Client public virtual string AccessToken { get; set; } {{#useRestSharp}} + {{#hasOAuthMethods}} + /// + /// Gets or sets the token URL for OAuth2 authentication. + /// + /// The OAuth Token URL. + public virtual string OAuthTokenUrl { get; set; } + + /// + /// Gets or sets the client ID for OAuth2 authentication. + /// + /// The OAuth Client ID. + public virtual string OAuthClientId { get; set; } + + /// + /// Gets or sets the client secret for OAuth2 authentication. + /// + /// The OAuth Client Secret. + public virtual string OAuthClientSecret { get; set; } + + /// + /// Gets or sets the client scope for OAuth2 authentication. + /// + /// The OAuth Client Scope. + public virtual string{{nrt?}} OAuthScope { get; set; } + + /// + /// Gets or sets the flow for OAuth2 authentication. + /// + /// The OAuth Flow. + public virtual OAuthFlow? OAuthFlow { get; set; } + + {{/hasOAuthMethods}} {{/useRestSharp}} /// /// Gets or sets the temporary folder path to store the files downloaded from the server. @@ -599,7 +634,7 @@ namespace {{packageName}}.Client set { _HttpSigningConfiguration = value; } } {{/hasHttpSignatureMethods}} - + /// /// Gets and Sets the RemoteCertificateValidationCallback /// @@ -678,6 +713,13 @@ namespace {{packageName}}.Client Password = second.Password ?? first.Password, AccessToken = second.AccessToken ?? first.AccessToken, {{#useRestSharp}} + {{#hasOAuthMethods}} + OAuthTokenUrl = second.OAuthTokenUrl ?? first.OAuthTokenUrl, + OAuthClientId = second.OAuthClientId ?? first.OAuthClientId, + OAuthClientSecret = second.OAuthClientSecret ?? first.OAuthClientSecret, + OAuthScope = second.OAuthScope ?? first.OAuthScope, + OAuthFlow = second.OAuthFlow ?? first.OAuthFlow, + {{/hasOAuthMethods}} {{/useRestSharp}} {{#hasHttpSignatureMethods}} HttpSigningConfiguration = second.HttpSigningConfiguration ?? first.HttpSigningConfiguration, diff --git a/templates/openapi-generator/csharp/IReadableConfiguration.mustache b/templates/openapi-generator/csharp/IReadableConfiguration.mustache index 9a129a21..6712aa63 100644 --- a/templates/openapi-generator/csharp/IReadableConfiguration.mustache +++ b/templates/openapi-generator/csharp/IReadableConfiguration.mustache @@ -6,6 +6,8 @@ using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; {{#useRestSharp}} +{{#hasOAuthMethods}}using {{packageName}}.Client.Auth; +{{/hasOAuthMethods}} {{/useRestSharp}} namespace {{packageName}}.Client @@ -22,6 +24,38 @@ namespace {{packageName}}.Client string AccessToken { get; } {{#useRestSharp}} + {{#hasOAuthMethods}} + /// + /// Gets the OAuth token URL. + /// + /// OAuth Token URL. + string OAuthTokenUrl { get; } + + /// + /// Gets the OAuth client ID. + /// + /// OAuth Client ID. + string OAuthClientId { get; } + + /// + /// Gets the OAuth client secret. + /// + /// OAuth Client Secret. + string OAuthClientSecret { get; } + + /// + /// Gets the OAuth token scope. + /// + /// OAuth Token scope. + string{{nrt?}} OAuthScope { get; } + + /// + /// Gets the OAuth flow. + /// + /// OAuth Flow. + OAuthFlow? OAuthFlow { get; } + + {{/hasOAuthMethods}} {{/useRestSharp}} /// /// Gets the API key. diff --git a/templates/openapi-generator/csharp/README.mustache b/templates/openapi-generator/csharp/README.mustache index ed47eafc..0ef94566 100644 --- a/templates/openapi-generator/csharp/README.mustache +++ b/templates/openapi-generator/csharp/README.mustache @@ -1,6 +1,4 @@ -# FattureInCloud C# SDK - -[![NuGet](https://img.shields.io/nuget/v/It.FattureInCloud.Sdk?color=g)](https://www.nuget.org/packages/It.FattureInCloud.Sdk) ![unit tests](https://github.com/fattureincloud/fattureincloud-csharp-sdk/actions/workflows/validate.yaml/badge.svg) +# {{packageName}} - the C# library for the {{appName}} {{#appDescriptionWithNewLines}} {{{.}}} @@ -13,11 +11,10 @@ This C# SDK is automatically generated by the [OpenAPI Generator](https://openap {{^hideGenerationTimestamp}} - Build date: {{generatedDate}} {{/hideGenerationTimestamp}} +- Generator version: {{generatorVersion}} - Build package: {{generatorClass}} - - {{#infoUrl}} -For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) + For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) {{/infoUrl}} @@ -32,7 +29,7 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) ## Dependencies {{#useRestSharp}} -- [RestSharp](https://www.nuget.org/packages/RestSharp) - 106.13.0 or later +- [RestSharp](https://www.nuget.org/packages/RestSharp) - 112.0.0 or later {{/useRestSharp}} - [Json.NET](https://www.nuget.org/packages/Newtonsoft.Json/) - 13.0.2 or later - [JsonSubTypes](https://www.nuget.org/packages/JsonSubTypes/) - 1.8.0 or later @@ -65,13 +62,16 @@ NOTE: RestSharp for .Net Core creates a new socket for each api call, which can {{/useRestSharp}} ## Installation +{{#netStandard}} +Generate the DLL using your preferred tool (e.g. `dotnet build`) +{{/netStandard}} +{{^netStandard}} +Run the following command to generate the DLL +- [Mac/Linux] `/bin/sh build.sh` +- [Windows] `build.bat` +{{/netStandard}} -To install the bindings via [Nuget](https://www.nuget.org), run the following command: -```shell -dotnet add package It.FattureInCloud.Sdk -``` - -Then add the following namespaces to your project: +Then include the DLL (under the `bin` folder) in the C# project, and use the namespaces: ```csharp using {{packageName}}.{{apiPackage}}; using {{packageName}}.Client; @@ -240,14 +240,8 @@ No model defined in this package ## Documentation for Authorization -{{^authMethods}} -All endpoints do not require authorization. -{{/authMethods}} -{{#authMethods}} -{{#last}} -Authentication schemes defined for the API: -{{/last}} -{{/authMethods}} +{{^authMethods}}Endpoints do not require authorization.{{/authMethods}} +{{#hasAuthMethods}}Authentication schemes defined for the API:{{/hasAuthMethods}} {{#authMethods}} ### {{name}} @@ -260,12 +254,13 @@ Authentication schemes defined for the API: {{/isBasicBasic}} {{#isBasicBearer}}- **Type**: Bearer Authentication {{/isBasicBearer}} +{{#isHttpSignature}}- **Type**: HTTP signature authentication +{{/isHttpSignature}} {{#isOAuth}}- **Type**: OAuth - **Flow**: {{flow}} - **Authorization URL**: {{authorizationUrl}} - **Scopes**: {{^scopes}}N/A{{/scopes}} -{{#scopes}} - - **{{{scope}}}**: {{{description}}} +{{#scopes}} - {{scope}}: {{description}} {{/scopes}} {{/isOAuth}} diff --git a/templates/openapi-generator/csharp/RequestOptions.mustache b/templates/openapi-generator/csharp/RequestOptions.mustache index c3fd4b17..cfc14692 100644 --- a/templates/openapi-generator/csharp/RequestOptions.mustache +++ b/templates/openapi-generator/csharp/RequestOptions.mustache @@ -62,6 +62,13 @@ namespace {{packageName}}.Client /// public Object Data { get; set; } + {{#hasOAuthMethods}} + /// + /// If request should be authenticated with OAuth. + /// + public bool OAuth { get; set; } + + {{/hasOAuthMethods}} /// /// Constructs a new instance of /// diff --git a/templates/openapi-generator/csharp/api.mustache b/templates/openapi-generator/csharp/api.mustache index 5a0e584b..7093752e 100644 --- a/templates/openapi-generator/csharp/api.mustache +++ b/templates/openapi-generator/csharp/api.mustache @@ -7,6 +7,8 @@ using System.Linq; using System.Net; using System.Net.Mime; using {{packageName}}.Client; +{{#hasOAuthMethods}}using {{packageName}}.Client.Auth; +{{/hasOAuthMethods}} {{#hasImport}}using {{packageName}}.{{modelPackage}}; {{/hasImport}} @@ -449,7 +451,19 @@ namespace {{packageName}}.{{apiPackage}} // oauth required if (!localVarRequestOptions.HeaderParameters.ContainsKey("Authorization")) { - localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken); + if (!string.IsNullOrEmpty(this.Configuration.AccessToken)) + { + localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken); + } + {{#hasOAuthMethods}} + else if (!string.IsNullOrEmpty(this.Configuration.OAuthTokenUrl) && + !string.IsNullOrEmpty(this.Configuration.OAuthClientId) && + !string.IsNullOrEmpty(this.Configuration.OAuthClientSecret) && + this.Configuration.OAuthFlow != null) + { + localVarRequestOptions.OAuth = true; + } + {{/hasOAuthMethods}} } {{/isOAuth}} {{#isHttpSignature}} @@ -729,7 +743,19 @@ namespace {{packageName}}.{{apiPackage}} // oauth required if (!localVarRequestOptions.HeaderParameters.ContainsKey("Authorization")) { - localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken); + if (!string.IsNullOrEmpty(this.Configuration.AccessToken)) + { + localVarRequestOptions.HeaderParameters.Add("Authorization", "Bearer " + this.Configuration.AccessToken); + } + {{#hasOAuthMethods}} + else if (!string.IsNullOrEmpty(this.Configuration.OAuthTokenUrl) && + !string.IsNullOrEmpty(this.Configuration.OAuthClientId) && + !string.IsNullOrEmpty(this.Configuration.OAuthClientSecret) && + this.Configuration.OAuthFlow != null) + { + localVarRequestOptions.OAuth = true; + } + {{/hasOAuthMethods}} } {{/isOAuth}} {{#isHttpSignature}} diff --git a/templates/openapi-generator/csharp/netcore_project.mustache b/templates/openapi-generator/csharp/netcore_project.mustache index 0fb50a65..a34c8a26 100644 --- a/templates/openapi-generator/csharp/netcore_project.mustache +++ b/templates/openapi-generator/csharp/netcore_project.mustache @@ -10,8 +10,7 @@ {{packageAuthors}} {{packageCompany}} {{packageTitle}} - A library that allows developers to connect with the Fatture in Cloud V2 REST API. - README.md + {{packageDescription}} {{packageCopyright}} {{packageName}} {{packageVersion}} @@ -27,7 +26,7 @@ {{#useCompareNetObjects}} - + {{/useCompareNetObjects}} {{^useGenericHost}} @@ -37,13 +36,13 @@ {{/useRestSharp}} {{#useGenericHost}} - - + + {{#supportsRetry}} - + {{/supportsRetry}} {{#net80OrLater}} - + {{/net80OrLater}} {{^net60OrLater}} @@ -62,7 +61,24 @@ {{/net60OrLater}} {{/validatable}} - +{{^useGenericHost}} + + {{^net60OrLater}} + + {{/net60OrLater}} + {{#net48}} + + {{/net48}} + + + {{^net60OrLater}} + + {{/net60OrLater}} + {{#net48}} + + {{/net48}} + +{{/useGenericHost}} {{>netcore_project.additions}} diff --git a/templates/openapi-generator/csharp/netcore_testproject.mustache b/templates/openapi-generator/csharp/netcore_testproject.mustache index e2d67ab8..d4124892 100644 --- a/templates/openapi-generator/csharp/netcore_testproject.mustache +++ b/templates/openapi-generator/csharp/netcore_testproject.mustache @@ -9,10 +9,9 @@ - + - - +