winauth httpclient

This commit is contained in:
Aigio Liu
2021-04-14 22:22:32 +08:00
parent 1962308d95
commit 4bfa3f26a4
3 changed files with 111 additions and 1 deletions
@@ -35,6 +35,100 @@ namespace System.Application.Services.Implementation
/// </summary>
static readonly bool EnableForward = true;
public async Task<T?> SendAsync<T>(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<T>(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<T?> GetAsync<T>(
string requestUri,
string accept,
@@ -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<string> RequestAsync(string url, string method, NameValueCollection data, NameValueCollection headers)
//{
// new WinAuthSteamClient()
//}
}
}
@@ -38,7 +38,7 @@ namespace WinAuth
/// <summary>
/// SteamClient for logging and getting/accepting/rejecting trade confirmations
/// </summary>
public class WinAuthSteamClient : IDisposable
public partial class WinAuthSteamClient : IDisposable
{
/// <summary>
/// URLs for all mobile services