[c#][netcore] add proxy support (#7741)

* add proxy support to c# netcore client

* add test, update doc
This commit is contained in:
William Cheng 2020-10-20 10:05:37 +08:00 committed by GitHub
parent 5bac98cd99
commit c1f2b1cad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 123 additions and 0 deletions

View File

@ -422,6 +422,11 @@ namespace {{packageName}}.Client
client.Timeout = configuration.Timeout; client.Timeout = configuration.Timeout;
if (configuration.Proxy != null)
{
client.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null) if (configuration.UserAgent != null)
{ {
client.UserAgent = configuration.UserAgent; client.UserAgent = configuration.UserAgent;
@ -529,6 +534,11 @@ namespace {{packageName}}.Client
client.Timeout = configuration.Timeout; client.Timeout = configuration.Timeout;
if (configuration.Proxy != null)
{
client.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null) if (configuration.UserAgent != null)
{ {
client.UserAgent = configuration.UserAgent; client.UserAgent = configuration.UserAgent;

View File

@ -7,6 +7,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
@ -109,6 +110,7 @@ namespace {{packageName}}.Client
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration() public Configuration()
{ {
Proxy = null;
UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}"; UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}";
BasePath = "{{{basePath}}}"; BasePath = "{{{basePath}}}";
DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>(); DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
@ -236,6 +238,12 @@ namespace {{packageName}}.Client
/// </summary> /// </summary>
public virtual int Timeout { get; set; } public virtual int Timeout { get; set; }
/// <summary>
/// Gets or sets the proxy
/// </summary>
/// <value>Proxy.</value>
public virtual WebProxy Proxy { get; set; }
/// <summary> /// <summary>
/// Gets or sets the HTTP user agent. /// Gets or sets the HTTP user agent.
/// </summary> /// </summary>
@ -553,6 +561,7 @@ namespace {{packageName}}.Client
DefaultHeaders = defaultHeaders, DefaultHeaders = defaultHeaders,
BasePath = second.BasePath ?? first.BasePath, BasePath = second.BasePath ?? first.BasePath,
Timeout = second.Timeout, Timeout = second.Timeout,
Proxy = second.Proxy ?? first.Proxy,
UserAgent = second.UserAgent ?? first.UserAgent, UserAgent = second.UserAgent ?? first.UserAgent,
Username = second.Username ?? first.Username, Username = second.Username ?? first.Username,
Password = second.Password ?? first.Password, Password = second.Password ?? first.Password,

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
namespace {{packageName}}.Client namespace {{packageName}}.Client
@ -66,6 +67,12 @@ namespace {{packageName}}.Client
/// <value>HTTP connection timeout.</value> /// <value>HTTP connection timeout.</value>
int Timeout { get; } int Timeout { get; }
/// <summary>
/// Gets the proxy.
/// </summary>
/// <value>Proxy.</value>
WebProxy Proxy { get; }
/// <summary> /// <summary>
/// Gets the user agent. /// Gets the user agent.
/// </summary> /// </summary>

View File

@ -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. 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}} {{/netStandard}}
<a name="usage"></a>
## 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;
```
<a name="getting-started"></a> <a name="getting-started"></a>
## Getting Started ## Getting Started

View File

@ -44,6 +44,17 @@ using Org.OpenAPITools.Api;
using Org.OpenAPITools.Client; using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model; using Org.OpenAPITools.Model;
``` ```
<a name="usage"></a>
## 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;
```
<a name="getting-started"></a> <a name="getting-started"></a>
## Getting Started ## Getting Started

View File

@ -22,6 +22,18 @@ namespace Org.OpenAPITools.Test
{ {
} }
/// <summary>
/// Test WebProxy
/// </summary>
[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;
}
/// <summary> /// <summary>
/// Test GetServerUrl /// Test GetServerUrl
/// </summary> /// </summary>

View File

@ -426,6 +426,11 @@ namespace Org.OpenAPITools.Client
client.Timeout = configuration.Timeout; client.Timeout = configuration.Timeout;
if (configuration.Proxy != null)
{
client.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null) if (configuration.UserAgent != null)
{ {
client.UserAgent = configuration.UserAgent; client.UserAgent = configuration.UserAgent;
@ -532,6 +537,11 @@ namespace Org.OpenAPITools.Client
client.Timeout = configuration.Timeout; client.Timeout = configuration.Timeout;
if (configuration.Proxy != null)
{
client.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null) if (configuration.UserAgent != null)
{ {
client.UserAgent = configuration.UserAgent; client.UserAgent = configuration.UserAgent;

View File

@ -13,6 +13,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
@ -104,6 +105,7 @@ namespace Org.OpenAPITools.Client
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration() public Configuration()
{ {
Proxy = null;
UserAgent = "OpenAPI-Generator/1.0.0/csharp"; UserAgent = "OpenAPI-Generator/1.0.0/csharp";
BasePath = "http://petstore.swagger.io:80/v2"; BasePath = "http://petstore.swagger.io:80/v2";
DefaultHeaders = new ConcurrentDictionary<string, string>(); DefaultHeaders = new ConcurrentDictionary<string, string>();
@ -255,6 +257,12 @@ namespace Org.OpenAPITools.Client
/// </summary> /// </summary>
public virtual int Timeout { get; set; } public virtual int Timeout { get; set; }
/// <summary>
/// Gets or sets the proxy
/// </summary>
/// <value>Proxy.</value>
public virtual WebProxy Proxy { get; set; }
/// <summary> /// <summary>
/// Gets or sets the HTTP user agent. /// Gets or sets the HTTP user agent.
/// </summary> /// </summary>
@ -562,6 +570,7 @@ namespace Org.OpenAPITools.Client
DefaultHeaders = defaultHeaders, DefaultHeaders = defaultHeaders,
BasePath = second.BasePath ?? first.BasePath, BasePath = second.BasePath ?? first.BasePath,
Timeout = second.Timeout, Timeout = second.Timeout,
Proxy = second.Proxy ?? first.Proxy,
UserAgent = second.UserAgent ?? first.UserAgent, UserAgent = second.UserAgent ?? first.UserAgent,
Username = second.Username ?? first.Username, Username = second.Username ?? first.Username,
Password = second.Password ?? first.Password, Password = second.Password ?? first.Password,

View File

@ -10,6 +10,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
namespace Org.OpenAPITools.Client namespace Org.OpenAPITools.Client
@ -74,6 +75,12 @@ namespace Org.OpenAPITools.Client
/// <value>HTTP connection timeout.</value> /// <value>HTTP connection timeout.</value>
int Timeout { get; } int Timeout { get; }
/// <summary>
/// Gets the proxy.
/// </summary>
/// <value>Proxy.</value>
WebProxy Proxy { get; }
/// <summary> /// <summary>
/// Gets the user agent. /// Gets the user agent.
/// </summary> /// </summary>

View File

@ -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. 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.
<a name="usage"></a>
## 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;
```
<a name="getting-started"></a> <a name="getting-started"></a>
## Getting Started ## Getting Started

View File

@ -427,6 +427,11 @@ namespace Org.OpenAPITools.Client
client.Timeout = configuration.Timeout; client.Timeout = configuration.Timeout;
if (configuration.Proxy != null)
{
client.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null) if (configuration.UserAgent != null)
{ {
client.UserAgent = configuration.UserAgent; client.UserAgent = configuration.UserAgent;
@ -533,6 +538,11 @@ namespace Org.OpenAPITools.Client
client.Timeout = configuration.Timeout; client.Timeout = configuration.Timeout;
if (configuration.Proxy != null)
{
client.Proxy = configuration.Proxy;
}
if (configuration.UserAgent != null) if (configuration.UserAgent != null)
{ {
client.UserAgent = configuration.UserAgent; client.UserAgent = configuration.UserAgent;

View File

@ -13,6 +13,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
@ -109,6 +110,7 @@ namespace Org.OpenAPITools.Client
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public Configuration() public Configuration()
{ {
Proxy = null;
UserAgent = "OpenAPI-Generator/1.0.0/csharp"; UserAgent = "OpenAPI-Generator/1.0.0/csharp";
BasePath = "http://petstore.swagger.io:80/v2"; BasePath = "http://petstore.swagger.io:80/v2";
DefaultHeaders = new ConcurrentDictionary<string, string>(); DefaultHeaders = new ConcurrentDictionary<string, string>();
@ -260,6 +262,12 @@ namespace Org.OpenAPITools.Client
/// </summary> /// </summary>
public virtual int Timeout { get; set; } public virtual int Timeout { get; set; }
/// <summary>
/// Gets or sets the proxy
/// </summary>
/// <value>Proxy.</value>
public virtual WebProxy Proxy { get; set; }
/// <summary> /// <summary>
/// Gets or sets the HTTP user agent. /// Gets or sets the HTTP user agent.
/// </summary> /// </summary>
@ -568,6 +576,7 @@ namespace Org.OpenAPITools.Client
DefaultHeaders = defaultHeaders, DefaultHeaders = defaultHeaders,
BasePath = second.BasePath ?? first.BasePath, BasePath = second.BasePath ?? first.BasePath,
Timeout = second.Timeout, Timeout = second.Timeout,
Proxy = second.Proxy ?? first.Proxy,
UserAgent = second.UserAgent ?? first.UserAgent, UserAgent = second.UserAgent ?? first.UserAgent,
Username = second.Username ?? first.Username, Username = second.Username ?? first.Username,
Password = second.Password ?? first.Password, Password = second.Password ?? first.Password,

View File

@ -10,6 +10,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
namespace Org.OpenAPITools.Client namespace Org.OpenAPITools.Client
@ -74,6 +75,12 @@ namespace Org.OpenAPITools.Client
/// <value>HTTP connection timeout.</value> /// <value>HTTP connection timeout.</value>
int Timeout { get; } int Timeout { get; }
/// <summary>
/// Gets the proxy.
/// </summary>
/// <value>Proxy.</value>
WebProxy Proxy { get; }
/// <summary> /// <summary>
/// Gets the user agent. /// Gets the user agent.
/// </summary> /// </summary>