From 4bfa3f26a4ea46bfc39bd49de58f964fe273e0e9 Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Wed, 14 Apr 2021 22:22:26 +0800 Subject: [PATCH] winauth httpclient --- .../Implementation/HttpServiceImpl.cs | 94 +++++++++++++++++++ .../WinAuthSteamClient.HttpClientImpl.cs | 16 ++++ .../WinAuth/WinAuthSteamClient.cs | 2 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.HttpClientImpl.cs diff --git a/src/ST.Client/Services/Implementation/HttpServiceImpl.cs b/src/ST.Client/Services/Implementation/HttpServiceImpl.cs index 066bd4fc2..0a0646cc7 100644 --- a/src/ST.Client/Services/Implementation/HttpServiceImpl.cs +++ b/src/ST.Client/Services/Implementation/HttpServiceImpl.cs @@ -35,6 +35,100 @@ namespace System.Application.Services.Implementation /// static readonly bool EnableForward = true; + public async Task SendAsync(HttpRequestMessage request, string? accept, CancellationToken cancellationToken) where T : notnull + { + HttpResponseMessage? response = null; + bool notDispose = false; + try + { + var requestUri = request.RequestUri.ToString(); + if (EnableForward && + allowUrls.Any(x => requestUri.StartsWith(x, StringComparison.OrdinalIgnoreCase))) + { + try + { + response = await cloud_client.Forward(request, + HttpCompletionOption.ResponseHeadersRead, + cancellationToken); + } + catch (Exception e) + { + logger.LogWarning(e, "CloudService Forward Fail."); + response = null; + } + } + + if (response == null) + { + var client = CreateClient(); + response = await client.SendAsync(request, + HttpCompletionOption.ResponseHeadersRead, + cancellationToken).ConfigureAwait(false); + } + + if (response.IsSuccessStatusCode) + { + if (response.Content != null) + { + var rspContentClrType = typeof(T); + if (rspContentClrType == typeof(string)) + { + return (T)(object)await response.Content.ReadAsStringAsync(); + } + else if (rspContentClrType == typeof(byte[])) + { + return (T)(object)await response.Content.ReadAsByteArrayAsync(); + } + else if (rspContentClrType == typeof(Stream)) + { + notDispose = true; + return (T)(object)await response.Content.ReadAsStreamAsync(); + } + var mime = response.Content.Headers.ContentType?.MediaType ?? accept; + switch (mime) + { + case MediaTypeNames.JSON: + case MediaTypeNames.XML: + case MediaTypeNames.XML_APP: + { + using var stream = await response.Content + .ReadAsStreamAsync().ConfigureAwait(false); + using var reader = new StreamReader(stream, Encoding.UTF8); + switch (mime) + { + case MediaTypeNames.JSON: + { + using var json = new JsonTextReader(reader); + return jsonSerializer.Deserialize(json); + } + case MediaTypeNames.XML: + case MediaTypeNames.XML_APP: + { + var xmlSerializer = new XmlSerializer(typeof(T)); + return (T)xmlSerializer.Deserialize(reader); + } + } + } + break; + } + } + } + } + catch (Exception e) + { + Log.Warn(TAG, e, "GetAsync Fail."); + } + finally + { + if (!notDispose) + { + request.Dispose(); + response?.Dispose(); + } + } + return default; + } + public async Task GetAsync( string requestUri, string accept, diff --git a/src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.HttpClientImpl.cs b/src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.HttpClientImpl.cs new file mode 100644 index 000000000..50dc24c35 --- /dev/null +++ b/src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.HttpClientImpl.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Text; +using System.Threading.Tasks; + +namespace WinAuth +{ + partial class WinAuthSteamClient + { + //protected async Task RequestAsync(string url, string method, NameValueCollection data, NameValueCollection headers) + //{ + // new WinAuthSteamClient() + //} + } +} diff --git a/src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.cs b/src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.cs index 81e7fb305..73d8bc125 100644 --- a/src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.cs +++ b/src/ST.Services.CloudService/WinAuth/WinAuthSteamClient.cs @@ -38,7 +38,7 @@ namespace WinAuth /// /// SteamClient for logging and getting/accepting/rejecting trade confirmations /// - public class WinAuthSteamClient : IDisposable + public partial class WinAuthSteamClient : IDisposable { /// /// URLs for all mobile services