-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use build-in httpclient instead of AgileHttp;
- Loading branch information
agile.zhou
committed
Nov 26, 2023
1 parent
435687d
commit fe86291
Showing
14 changed files
with
261 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using AgileConfig.Server.Common; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Net.Http; | ||
|
||
namespace AgileConfig.Server.Apisite | ||
{ | ||
public static class StartupExtension | ||
{ | ||
public static void AddDefaultHttpClient(this IServiceCollection services, bool isTrustSSL) | ||
{ | ||
services.AddHttpClient(Global.DefaultHttpClientName) | ||
.ConfigurePrimaryHttpMessageHandler(() => { | ||
return NewMessageHandler(isTrustSSL); | ||
}) | ||
; | ||
} | ||
|
||
static HttpMessageHandler NewMessageHandler(bool alwaysTrustSsl) | ||
{ | ||
var handler = new HttpClientHandler(); | ||
if (alwaysTrustSsl) | ||
{ | ||
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => | ||
{ | ||
return true; | ||
}; | ||
} | ||
|
||
return handler; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/AgileConfig.Server.Common/RestClient/DefaultRestClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace AgileConfig.Server.Common.RestClient | ||
{ | ||
public class DefaultRestClient : IRestClient | ||
{ | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public DefaultRestClient( | ||
IHttpClientFactory httpClientFactory | ||
) | ||
{ | ||
this._httpClientFactory = httpClientFactory; | ||
} | ||
|
||
private HttpClient GetDefaultClient() | ||
{ | ||
return _httpClientFactory.CreateClient(Global.DefaultHttpClientName); | ||
} | ||
|
||
public async Task<T> GetAsync<T>(string url, Dictionary<string, string> headers = null) | ||
{ | ||
using var resp = await GetAsync(url, headers); | ||
resp.EnsureSuccessStatusCode(); | ||
|
||
var json = await resp.Content.ReadAsStringAsync(); | ||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json); | ||
} | ||
|
||
public async Task<T> PostAsync<T>(string url, object data, Dictionary<string, string> headers = null) | ||
{ | ||
using var resp = await PostAsync(url, data, headers); | ||
|
||
resp.EnsureSuccessStatusCode(); | ||
|
||
var json = await resp.Content.ReadAsStringAsync(); | ||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json); | ||
} | ||
|
||
public async Task<HttpResponseMessage> PostAsync(string url, object data, Dictionary<string, string> headers = null) | ||
{ | ||
var httpclient = GetDefaultClient(); | ||
if (headers != null) | ||
{ | ||
foreach (var header in headers) | ||
{ | ||
httpclient.DefaultRequestHeaders.Add(header.Key, header.Value); | ||
} | ||
} | ||
|
||
var jsondata = ""; | ||
if (data != null) | ||
{ | ||
jsondata = Newtonsoft.Json.JsonConvert.SerializeObject(data); | ||
} | ||
var stringContent = new StringContent(jsondata, | ||
new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")); | ||
|
||
var resp = await httpclient.PostAsync(url, stringContent); | ||
|
||
return resp; | ||
} | ||
|
||
public async Task<HttpResponseMessage> GetAsync(string url, Dictionary<string, string> headers = null) | ||
{ | ||
var httpclient = GetDefaultClient(); | ||
if (headers != null) | ||
{ | ||
foreach (var header in headers) | ||
{ | ||
httpclient.DefaultRequestHeaders.Add(header.Key, header.Value); | ||
} | ||
} | ||
var resp = await httpclient.GetAsync(url); | ||
|
||
return resp; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace AgileConfig.Server.Common.RestClient | ||
{ | ||
public interface IRestClient | ||
{ | ||
Task<T> GetAsync<T>(string url, Dictionary<string, string> headers = null); | ||
|
||
Task<HttpResponseMessage> GetAsync(string url, Dictionary<string, string> headers = null); | ||
|
||
Task<T> PostAsync<T>(string url, object data, Dictionary<string, string> headers = null); | ||
|
||
Task<HttpResponseMessage> PostAsync(string url, object data, Dictionary<string, string> headers = null); | ||
|
||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/AgileConfig.Server.Common/RestClient/ServiceCollectionEx.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace AgileConfig.Server.Common.RestClient | ||
{ | ||
public static class ServiceCollectionEx | ||
{ | ||
public static void AddRestClient(this IServiceCollection sc) | ||
{ | ||
sc.AddScoped<IRestClient, DefaultRestClient>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.