From c1f2b1cad92ea738ea9486d341a8ab38a862d47a Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 20 Oct 2020 10:05:37 +0800 Subject: [PATCH] [c#][netcore] add proxy support (#7741) * add proxy support to c# netcore client * add test, update doc --- .../main/resources/csharp-netcore/ApiClient.mustache | 10 ++++++++++ .../resources/csharp-netcore/Configuration.mustache | 9 +++++++++ .../csharp-netcore/IReadableConfiguration.mustache | 7 +++++++ .../main/resources/csharp-netcore/README.mustache | 11 +++++++++++ .../petstore/csharp-netcore/OpenAPIClient/README.md | 11 +++++++++++ .../src/Org.OpenAPITools.Test/ConfigurationTests.cs | 12 ++++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 10 ++++++++++ .../src/Org.OpenAPITools/Client/Configuration.cs | 9 +++++++++ .../Client/IReadableConfiguration.cs | 7 +++++++ .../csharp-netcore/OpenAPIClientCore/README.md | 11 +++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 10 ++++++++++ .../src/Org.OpenAPITools/Client/Configuration.cs | 9 +++++++++ .../Client/IReadableConfiguration.cs | 7 +++++++ 13 files changed, 123 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache index d327765bd82..edcebaef09f 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache @@ -422,6 +422,11 @@ namespace {{packageName}}.Client client.Timeout = configuration.Timeout; + if (configuration.Proxy != null) + { + client.Proxy = configuration.Proxy; + } + if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; @@ -529,6 +534,11 @@ namespace {{packageName}}.Client client.Timeout = configuration.Timeout; + if (configuration.Proxy != null) + { + client.Proxy = configuration.Proxy; + } + if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache index 175dc6c0858..f5fc29683dd 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache @@ -7,6 +7,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Reflection; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -109,6 +110,7 @@ namespace {{packageName}}.Client [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] public Configuration() { + Proxy = null; UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}"; BasePath = "{{{basePath}}}"; DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary(); @@ -236,6 +238,12 @@ namespace {{packageName}}.Client /// public virtual int Timeout { get; set; } + /// + /// Gets or sets the proxy + /// + /// Proxy. + public virtual WebProxy Proxy { get; set; } + /// /// Gets or sets the HTTP user agent. /// @@ -553,6 +561,7 @@ namespace {{packageName}}.Client DefaultHeaders = defaultHeaders, BasePath = second.BasePath ?? first.BasePath, Timeout = second.Timeout, + Proxy = second.Proxy ?? first.Proxy, UserAgent = second.UserAgent ?? first.UserAgent, Username = second.Username ?? first.Username, Password = second.Password ?? first.Password, diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache index 90897a53275..7c6487d92f8 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Net; using System.Security.Cryptography.X509Certificates; namespace {{packageName}}.Client @@ -66,6 +67,12 @@ namespace {{packageName}}.Client /// HTTP connection timeout. int Timeout { get; } + /// + /// Gets the proxy. + /// + /// Proxy. + WebProxy Proxy { get; } + /// /// Gets the user agent. /// diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/README.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/README.mustache index c86f32641de..0f149e21bc1 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/README.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/README.mustache @@ -84,6 +84,17 @@ nuget pack -Build -OutputDirectory out {{packageName}}.csproj Then, publish to a [local feed](https://docs.microsoft.com/en-us/nuget/hosting-packages/local-feeds) or [other host](https://docs.microsoft.com/en-us/nuget/hosting-packages/overview) and consume the new package via Nuget as usual. {{/netStandard}} + +## Usage + +To use the API client with a HTTP proxy, setup a `System.Net.WebProxy` +```csharp +Configuration c = new Configuration(); +System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/"); +webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; +c.Proxy = webProxy; +``` + ## Getting Started diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/README.md b/samples/client/petstore/csharp-netcore/OpenAPIClient/README.md index ce6ff880135..98da1830fc3 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/README.md +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/README.md @@ -44,6 +44,17 @@ using Org.OpenAPITools.Api; using Org.OpenAPITools.Client; using Org.OpenAPITools.Model; ``` + +## Usage + +To use the API client with a HTTP proxy, setup a `System.Net.WebProxy` +```csharp +Configuration c = new Configuration(); +System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/"); +webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; +c.Proxy = webProxy; +``` + ## Getting Started diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ConfigurationTests.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ConfigurationTests.cs index bedfab04c34..c3d8c9deae3 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ConfigurationTests.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ConfigurationTests.cs @@ -22,6 +22,18 @@ namespace Org.OpenAPITools.Test { } + /// + /// Test WebProxy + /// + [Fact] + public void WebProxyTest() + { + Configuration c = new Configuration(); + System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/"); + webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; + c.Proxy = webProxy; + } + /// /// Test GetServerUrl /// diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs index d46baa056da..e5fbef5f1ed 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -426,6 +426,11 @@ namespace Org.OpenAPITools.Client client.Timeout = configuration.Timeout; + if (configuration.Proxy != null) + { + client.Proxy = configuration.Proxy; + } + if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; @@ -532,6 +537,11 @@ namespace Org.OpenAPITools.Client client.Timeout = configuration.Timeout; + if (configuration.Proxy != null) + { + client.Proxy = configuration.Proxy; + } + if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs index 96683c563d4..279cfe96b74 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs @@ -13,6 +13,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Reflection; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -104,6 +105,7 @@ namespace Org.OpenAPITools.Client [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] public Configuration() { + Proxy = null; UserAgent = "OpenAPI-Generator/1.0.0/csharp"; BasePath = "http://petstore.swagger.io:80/v2"; DefaultHeaders = new ConcurrentDictionary(); @@ -255,6 +257,12 @@ namespace Org.OpenAPITools.Client /// public virtual int Timeout { get; set; } + /// + /// Gets or sets the proxy + /// + /// Proxy. + public virtual WebProxy Proxy { get; set; } + /// /// Gets or sets the HTTP user agent. /// @@ -562,6 +570,7 @@ namespace Org.OpenAPITools.Client DefaultHeaders = defaultHeaders, BasePath = second.BasePath ?? first.BasePath, Timeout = second.Timeout, + Proxy = second.Proxy ?? first.Proxy, UserAgent = second.UserAgent ?? first.UserAgent, Username = second.Username ?? first.Username, Password = second.Password ?? first.Password, diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs index e8ca0b98db5..141e65a5b72 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; +using System.Net; using System.Security.Cryptography.X509Certificates; namespace Org.OpenAPITools.Client @@ -74,6 +75,12 @@ namespace Org.OpenAPITools.Client /// HTTP connection timeout. int Timeout { get; } + /// + /// Gets the proxy. + /// + /// Proxy. + WebProxy Proxy { get; } + /// /// Gets the user agent. /// diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/README.md b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/README.md index 14047428547..82ce25e0091 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/README.md +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/README.md @@ -56,6 +56,17 @@ nuget pack -Build -OutputDirectory out Org.OpenAPITools.csproj Then, publish to a [local feed](https://docs.microsoft.com/en-us/nuget/hosting-packages/local-feeds) or [other host](https://docs.microsoft.com/en-us/nuget/hosting-packages/overview) and consume the new package via Nuget as usual. + +## Usage + +To use the API client with a HTTP proxy, setup a `System.Net.WebProxy` +```csharp +Configuration c = new Configuration(); +System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/"); +webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; +c.Proxy = webProxy; +``` + ## Getting Started diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs index 7b807ef86b8..3d14a4f02ae 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -427,6 +427,11 @@ namespace Org.OpenAPITools.Client client.Timeout = configuration.Timeout; + if (configuration.Proxy != null) + { + client.Proxy = configuration.Proxy; + } + if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; @@ -533,6 +538,11 @@ namespace Org.OpenAPITools.Client client.Timeout = configuration.Timeout; + if (configuration.Proxy != null) + { + client.Proxy = configuration.Proxy; + } + if (configuration.UserAgent != null) { client.UserAgent = configuration.UserAgent; diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs index 6ea883d24e3..154091fc731 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs @@ -13,6 +13,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Reflection; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -109,6 +110,7 @@ namespace Org.OpenAPITools.Client [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] public Configuration() { + Proxy = null; UserAgent = "OpenAPI-Generator/1.0.0/csharp"; BasePath = "http://petstore.swagger.io:80/v2"; DefaultHeaders = new ConcurrentDictionary(); @@ -260,6 +262,12 @@ namespace Org.OpenAPITools.Client /// public virtual int Timeout { get; set; } + /// + /// Gets or sets the proxy + /// + /// Proxy. + public virtual WebProxy Proxy { get; set; } + /// /// Gets or sets the HTTP user agent. /// @@ -568,6 +576,7 @@ namespace Org.OpenAPITools.Client DefaultHeaders = defaultHeaders, BasePath = second.BasePath ?? first.BasePath, Timeout = second.Timeout, + Proxy = second.Proxy ?? first.Proxy, UserAgent = second.UserAgent ?? first.UserAgent, Username = second.Username ?? first.Username, Password = second.Password ?? first.Password, diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs index e8ca0b98db5..141e65a5b72 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; +using System.Net; using System.Security.Cryptography.X509Certificates; namespace Org.OpenAPITools.Client @@ -74,6 +75,12 @@ namespace Org.OpenAPITools.Client /// HTTP connection timeout. int Timeout { get; } + /// + /// Gets the proxy. + /// + /// Proxy. + WebProxy Proxy { get; } + /// /// Gets the user agent. ///