2023-04-30 08:08:32 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
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
|
|
|
|
|
|
|
|
|
namespace DistTestCore
|
|
|
|
|
{
|
|
|
|
|
public class Http
|
|
|
|
|
{
|
2023-04-30 08:08:32 +00:00
|
|
|
|
private readonly BaseLog log;
|
2023-05-04 06:55:20 +00:00
|
|
|
|
private readonly ITimeSet timeSet;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private readonly string ip;
|
|
|
|
|
private readonly int port;
|
|
|
|
|
private readonly string baseUrl;
|
2023-05-11 10:44:53 +00:00
|
|
|
|
private readonly TimeSpan? timeoutOverride;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-05-11 10:44:53 +00:00
|
|
|
|
public Http(BaseLog log, ITimeSet timeSet, string ip, int port, string baseUrl, TimeSpan? timeoutOverride = 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-04-12 11:53:55 +00:00
|
|
|
|
this.ip = ip;
|
|
|
|
|
this.port = port;
|
|
|
|
|
this.baseUrl = baseUrl;
|
2023-05-11 10:44:53 +00:00
|
|
|
|
this.timeoutOverride = timeoutOverride;
|
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; ;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T HttpGetJson<T>(string route)
|
|
|
|
|
{
|
2023-04-19 08:42:08 +00:00
|
|
|
|
var json = HttpGetString(route);
|
|
|
|
|
return TryJsonDeserialize<T>(json);
|
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-04-24 14:07:32 +00:00
|
|
|
|
var json = HttpPostJson(route, body);
|
|
|
|
|
return TryJsonDeserialize<TResponse>(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string HttpPostJson<TRequest>(string route, TRequest body)
|
|
|
|
|
{
|
|
|
|
|
return Retry(() =>
|
2023-04-18 13:33:12 +00:00
|
|
|
|
{
|
|
|
|
|
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));
|
2023-04-18 13:33:12 +00:00
|
|
|
|
var result = Time.Wait(client.PostAsync(url, content));
|
2023-04-30 08:08:32 +00:00
|
|
|
|
var str= Time.Wait(result.Content.ReadAsStringAsync());
|
|
|
|
|
Log(url, str);
|
|
|
|
|
return str;
|
2023-04-18 13:33:12 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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-04-30 08:08:32 +00:00
|
|
|
|
var str =Time.Wait(response.Content.ReadAsStringAsync());
|
|
|
|
|
Log(url, str);
|
|
|
|
|
return str;
|
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));
|
2023-04-12 11:53:55 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetUrl()
|
|
|
|
|
{
|
|
|
|
|
return $"http://{ip}:{port}{baseUrl}";
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 08:08:32 +00:00
|
|
|
|
private void Log(string url, string message)
|
|
|
|
|
{
|
|
|
|
|
log.Debug($"({url}) = '{message}'", 3);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 06:55:20 +00:00
|
|
|
|
private T Retry<T>(Func<T> operation)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-05-10 07:55:36 +00:00
|
|
|
|
return Time.Retry(operation, timeSet.HttpCallRetryTimeout(), timeSet.HttpCallRetryDelay());
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 08:42:08 +00:00
|
|
|
|
private static 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-05-04 06:55:20 +00:00
|
|
|
|
private HttpClient GetClient()
|
2023-05-11 10:44:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (timeoutOverride.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return GetClient(timeoutOverride.Value);
|
|
|
|
|
}
|
|
|
|
|
return GetClient(timeSet.HttpCallTimeout());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpClient GetClient(TimeSpan timeout)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
2023-05-11 10:44:53 +00:00
|
|
|
|
client.Timeout = timeout;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|