cs-codex-dist-tests/DistTestCore/Http.cs

174 lines
5.6 KiB
C#
Raw Normal View History

using Logging;
2023-04-30 08:08:32 +00:00
using Newtonsoft.Json;
2023-04-12 11:53:55 +00:00
using System.Net.Http.Headers;
using System.Net.Http.Json;
2023-04-13 07:33:10 +00:00
using Utils;
2023-04-12 11:53:55 +00:00
namespace DistTestCore
{
public class Http
{
2023-04-30 08:08:32 +00:00
private readonly BaseLog log;
private readonly ITimeSet timeSet;
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;
private readonly string? logAlias;
2023-04-12 11:53:55 +00:00
public Http(BaseLog 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)
{
}
public Http(BaseLog 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;
this.timeSet = timeSet;
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;
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)
{
return Retry(() =>
{
using var client = GetClient();
var url = GetUrl() + route;
2023-04-30 08:08:32 +00:00
Log(url, "");
2023-04-13 07:33:10 +00:00
var result = Time.Wait(client.GetAsync(url));
2023-04-30 08:08:32 +00:00
var str = Time.Wait(result.Content.ReadAsStringAsync());
Log(url, str);
return str; ;
}, $"HTTP-GET:{route}");
2023-04-12 11:53:55 +00:00
}
public T HttpGetJson<T>(string route)
{
var json = HttpGetString(route);
return TryJsonDeserialize<T>(json);
2023-04-12 11:53:55 +00:00
}
public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body)
{
var json = HttpPostJson(route, body);
return TryJsonDeserialize<TResponse>(json);
}
public string HttpPostJson<TRequest>(string route, TRequest body)
{
return Retry(() =>
{
using var client = GetClient();
var url = GetUrl() + route;
using var content = JsonContent.Create(body);
2023-04-30 08:08:32 +00:00
Log(url, JsonConvert.SerializeObject(body));
var result = Time.Wait(client.PostAsync(url, content));
var str = Time.Wait(result.Content.ReadAsStringAsync());
2023-04-30 08:08:32 +00:00
Log(url, str);
return str;
}, $"HTTP-POST-JSON: {route}");
}
2023-08-11 10:38:26 +00:00
public string HttpPostString(string route, string body)
{
return Retry(() =>
{
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;
}, $"HTTP-POST-STRING: {route}");
}
2023-04-12 11:53:55 +00:00
public string HttpPostStream(string route, Stream stream)
{
return Retry(() =>
{
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;
}, $"HTTP-POST-STREAM: {route}");
2023-04-12 11:53:55 +00:00
}
public Stream HttpGetStream(string route)
{
return Retry(() =>
{
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));
}, $"HTTP-GET-STREAM: {route}");
}
public T TryJsonDeserialize<T>(string json)
{
try
{
return JsonConvert.DeserializeObject<T>(json)!;
}
catch (Exception exception)
{
var msg = $"Failed to deserialize JSON: '{json}' with exception: {exception}";
throw new InvalidOperationException(msg, exception);
}
2023-04-12 11:53:55 +00:00
}
private string GetUrl()
{
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)
{
if (logAlias != null)
{
log.Debug($"({logAlias})({url}) = '{message}'", 3);
}
else
{
log.Debug($"({url}) = '{message}'", 3);
}
2023-04-30 08:08:32 +00:00
}
private T Retry<T>(Func<T> operation, string description)
2023-04-12 11:53:55 +00:00
{
2023-08-02 13:11:27 +00:00
try
{
return operation();
}
catch (Exception ex)
{
throw new Exception(description, ex);
}
}
private HttpClient GetClient()
2023-04-12 11:53:55 +00:00
{
var client = new HttpClient();
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
}
}