2023-06-21 06:28:40 +00:00
|
|
|
|
using Logging;
|
2023-04-30 08:08:32 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2023-08-18 05:40:40 +00:00
|
|
|
|
using Serialization = Newtonsoft.Json.Serialization;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
using System.Net.Http.Headers;
|
2023-04-18 13:33:12 +00:00
|
|
|
|
using System.Net.Http.Json;
|
2023-04-13 07:33:10 +00:00
|
|
|
|
using Utils;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-09-12 11:32:06 +00:00
|
|
|
|
namespace Core
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-09-14 13:40:15 +00:00
|
|
|
|
public interface IHttp
|
|
|
|
|
{
|
|
|
|
|
string HttpGetString(string route);
|
|
|
|
|
T HttpGetJson<T>(string route);
|
|
|
|
|
TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body);
|
|
|
|
|
string HttpPostJson<TRequest>(string route, TRequest body);
|
2023-10-02 12:42:36 +00:00
|
|
|
|
TResponse HttpPostString<TResponse>(string route, string body);
|
2023-09-14 13:40:15 +00:00
|
|
|
|
string HttpPostStream(string route, Stream stream);
|
|
|
|
|
Stream HttpGetStream(string route);
|
2023-10-24 07:41:37 +00:00
|
|
|
|
T Deserialize<T>(string json);
|
2023-09-14 13:40:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class Http : IHttp
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
private static readonly object httpLock = new object();
|
2023-09-11 14:57:57 +00:00
|
|
|
|
private readonly ILog log;
|
2023-05-04 06:55:20 +00:00
|
|
|
|
private readonly ITimeSet timeSet;
|
2023-06-21 06:28:40 +00:00
|
|
|
|
private readonly Address address;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private readonly string baseUrl;
|
2023-08-13 09:19:35 +00:00
|
|
|
|
private readonly Action<HttpClient> onClientCreated;
|
2023-06-30 06:39:18 +00:00
|
|
|
|
private readonly string? logAlias;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-09-14 13:40:15 +00:00
|
|
|
|
internal Http(ILog log, ITimeSet timeSet, Address address, string baseUrl, string? logAlias = null)
|
2023-08-13 09:19:35 +00:00
|
|
|
|
: this(log, timeSet, address, baseUrl, DoNothing, logAlias)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 13:40:15 +00:00
|
|
|
|
internal Http(ILog log, ITimeSet timeSet, Address address, string baseUrl, Action<HttpClient> onClientCreated, string? logAlias = null)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-30 08:08:32 +00:00
|
|
|
|
this.log = log;
|
2023-05-04 06:55:20 +00:00
|
|
|
|
this.timeSet = timeSet;
|
2023-06-01 07:35:18 +00:00
|
|
|
|
this.address = address;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
this.baseUrl = baseUrl;
|
2023-08-13 09:19:35 +00:00
|
|
|
|
this.onClientCreated = onClientCreated;
|
2023-06-30 06:39:18 +00:00
|
|
|
|
this.logAlias = logAlias;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
if (!this.baseUrl.StartsWith("/")) this.baseUrl = "/" + this.baseUrl;
|
|
|
|
|
if (!this.baseUrl.EndsWith("/")) this.baseUrl += "/";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string HttpGetString(string route)
|
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return LockRetry(() =>
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return GetString(route);
|
2023-05-31 11:15:41 +00:00
|
|
|
|
}, $"HTTP-GET:{route}");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T HttpGetJson<T>(string route)
|
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return LockRetry(() =>
|
|
|
|
|
{
|
|
|
|
|
var json = GetString(route);
|
|
|
|
|
return Deserialize<T>(json);
|
|
|
|
|
}, $"HTTP-GET:{route}");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 13:33:12 +00:00
|
|
|
|
public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body)
|
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return LockRetry(() =>
|
2023-08-31 09:19:53 +00:00
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
var response = PostJson(route, body);
|
|
|
|
|
var json = Time.Wait(response.Content.ReadAsStringAsync());
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpRequestException(json);
|
|
|
|
|
}
|
|
|
|
|
Log(GetUrl() + route, json);
|
|
|
|
|
return Deserialize<TResponse>(json);
|
|
|
|
|
}, $"HTTP-POST-JSON: {route}");
|
2023-04-24 14:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 09:19:53 +00:00
|
|
|
|
public string HttpPostJson<TRequest>(string route, TRequest body)
|
2023-04-24 14:07:32 +00:00
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return LockRetry(() =>
|
2023-08-11 10:38:26 +00:00
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
var response = PostJson(route, body);
|
|
|
|
|
return Time.Wait(response.Content.ReadAsStringAsync());
|
|
|
|
|
}, $"HTTP-POST-JSON: {route}");
|
2023-08-11 10:38:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 12:42:36 +00:00
|
|
|
|
public TResponse HttpPostString<TResponse>(string route, string body)
|
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return LockRetry(() =>
|
|
|
|
|
{
|
|
|
|
|
var response = PostJsonString(route, body);
|
|
|
|
|
if (response == null) throw new Exception("Received no response.");
|
2023-10-31 10:01:10 +00:00
|
|
|
|
var result = Deserialize<TResponse>(response);
|
2023-10-24 07:41:37 +00:00
|
|
|
|
if (result == null) throw new Exception("Failed to deserialize response");
|
|
|
|
|
return result;
|
|
|
|
|
}, $"HTTO-POST-JSON: {route}");
|
2023-10-02 12:42:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:53:55 +00:00
|
|
|
|
public string HttpPostStream(string route, Stream stream)
|
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return LockRetry(() =>
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
using var client = GetClient();
|
|
|
|
|
var url = GetUrl() + route;
|
2023-04-30 08:08:32 +00:00
|
|
|
|
Log(url, "~ STREAM ~");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
var content = new StreamContent(stream);
|
|
|
|
|
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
2023-04-13 07:33:10 +00:00
|
|
|
|
var response = Time.Wait(client.PostAsync(url, content));
|
2023-08-08 12:42:59 +00:00
|
|
|
|
var str = Time.Wait(response.Content.ReadAsStringAsync());
|
2023-04-30 08:08:32 +00:00
|
|
|
|
Log(url, str);
|
|
|
|
|
return str;
|
2023-05-31 11:15:41 +00:00
|
|
|
|
}, $"HTTP-POST-STREAM: {route}");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Stream HttpGetStream(string route)
|
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
return LockRetry(() =>
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
var client = GetClient();
|
|
|
|
|
var url = GetUrl() + route;
|
2023-04-30 08:08:32 +00:00
|
|
|
|
Log(url, "~ STREAM ~");
|
2023-04-13 07:33:10 +00:00
|
|
|
|
return Time.Wait(client.GetStreamAsync(url));
|
2023-05-31 11:15:41 +00:00
|
|
|
|
}, $"HTTP-GET-STREAM: {route}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 07:41:37 +00:00
|
|
|
|
public T Deserialize<T>(string json)
|
2023-05-31 11:15:41 +00:00
|
|
|
|
{
|
2023-08-18 05:40:40 +00:00
|
|
|
|
var errors = new List<string>();
|
2023-08-31 09:19:53 +00:00
|
|
|
|
var deserialized = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings()
|
|
|
|
|
{
|
2023-08-18 05:40:40 +00:00
|
|
|
|
Error = delegate(object? sender, Serialization.ErrorEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.CurrentObject == args.ErrorContext.OriginalObject)
|
|
|
|
|
{
|
|
|
|
|
errors.Add($"""
|
|
|
|
|
Member: '{args.ErrorContext.Member?.ToString() ?? "<null>"}'
|
|
|
|
|
Path: {args.ErrorContext.Path}
|
|
|
|
|
Error: {args.ErrorContext.Error.Message}
|
|
|
|
|
""");
|
|
|
|
|
args.ErrorContext.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-10-24 07:41:37 +00:00
|
|
|
|
if (errors.Count > 0)
|
2023-08-31 09:19:53 +00:00
|
|
|
|
{
|
2023-08-18 05:40:40 +00:00
|
|
|
|
throw new JsonSerializationException($"Failed to deserialize JSON '{json}' with exception(s): \n{string.Join("\n", errors)}");
|
2023-05-31 11:15:41 +00:00
|
|
|
|
}
|
2023-08-31 09:19:53 +00:00
|
|
|
|
else if (deserialized == null)
|
|
|
|
|
{
|
2023-08-18 05:40:40 +00:00
|
|
|
|
throw new JsonSerializationException($"Failed to deserialize JSON '{json}': resulting deserialized object is null");
|
2023-05-31 11:15:41 +00:00
|
|
|
|
}
|
2023-08-18 05:40:40 +00:00
|
|
|
|
return deserialized;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 07:41:37 +00:00
|
|
|
|
private string GetString(string route)
|
|
|
|
|
{
|
|
|
|
|
using var client = GetClient();
|
|
|
|
|
var url = GetUrl() + route;
|
|
|
|
|
Log(url, "");
|
|
|
|
|
var result = Time.Wait(client.GetAsync(url));
|
|
|
|
|
var str = Time.Wait(result.Content.ReadAsStringAsync());
|
|
|
|
|
Log(url, str);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 09:19:53 +00:00
|
|
|
|
private HttpResponseMessage PostJson<TRequest>(string route, TRequest body)
|
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
using var client = GetClient();
|
|
|
|
|
var url = GetUrl() + route;
|
|
|
|
|
using var content = JsonContent.Create(body);
|
|
|
|
|
Log(url, JsonConvert.SerializeObject(body));
|
|
|
|
|
return Time.Wait(client.PostAsync(url, content));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string PostJsonString(string route, string body)
|
|
|
|
|
{
|
|
|
|
|
using var client = GetClient();
|
|
|
|
|
var url = GetUrl() + route;
|
|
|
|
|
Log(url, body);
|
|
|
|
|
var content = new StringContent(body);
|
|
|
|
|
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
|
|
|
|
|
var result = Time.Wait(client.PostAsync(url, content));
|
|
|
|
|
var str = Time.Wait(result.Content.ReadAsStringAsync());
|
|
|
|
|
Log(url, str);
|
|
|
|
|
return str;
|
2023-08-31 09:19:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private string GetUrl()
|
|
|
|
|
{
|
2023-06-01 07:35:18 +00:00
|
|
|
|
return $"{address.Host}:{address.Port}{baseUrl}";
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 08:08:32 +00:00
|
|
|
|
private void Log(string url, string message)
|
|
|
|
|
{
|
2023-06-30 06:39:18 +00:00
|
|
|
|
if (logAlias != null)
|
|
|
|
|
{
|
|
|
|
|
log.Debug($"({logAlias})({url}) = '{message}'", 3);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Debug($"({url}) = '{message}'", 3);
|
|
|
|
|
}
|
2023-04-30 08:08:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 07:41:37 +00:00
|
|
|
|
private T LockRetry<T>(Func<T> operation, string description)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-10-24 07:41:37 +00:00
|
|
|
|
lock (httpLock)
|
|
|
|
|
{
|
|
|
|
|
return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description);
|
|
|
|
|
}
|
2023-04-19 08:42:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 06:55:20 +00:00
|
|
|
|
private HttpClient GetClient()
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
2023-07-17 13:21:10 +00:00
|
|
|
|
client.Timeout = timeSet.HttpCallTimeout();
|
2023-08-13 09:19:35 +00:00
|
|
|
|
onClientCreated(client);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
return client;
|
|
|
|
|
}
|
2023-08-13 09:19:35 +00:00
|
|
|
|
|
|
|
|
|
private static void DoNothing(HttpClient client)
|
|
|
|
|
{
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|