2023-04-12 11:53:55 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
private readonly string ip;
|
|
|
|
|
private readonly int port;
|
|
|
|
|
private readonly string baseUrl;
|
|
|
|
|
|
|
|
|
|
public Http(string ip, int port, string baseUrl)
|
|
|
|
|
{
|
|
|
|
|
this.ip = ip;
|
|
|
|
|
this.port = port;
|
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
|
|
|
|
|
|
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-13 07:33:10 +00:00
|
|
|
|
var result = Time.Wait(client.GetAsync(url));
|
|
|
|
|
return Time.Wait(result.Content.ReadAsStringAsync());
|
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-19 08:42:08 +00:00
|
|
|
|
var json = Retry(() =>
|
2023-04-18 13:33:12 +00:00
|
|
|
|
{
|
|
|
|
|
using var client = GetClient();
|
|
|
|
|
var url = GetUrl() + route;
|
|
|
|
|
using var content = JsonContent.Create(body);
|
|
|
|
|
var result = Time.Wait(client.PostAsync(url, content));
|
2023-04-19 08:42:08 +00:00
|
|
|
|
return Time.Wait(result.Content.ReadAsStringAsync());
|
2023-04-18 13:33:12 +00:00
|
|
|
|
});
|
2023-04-19 08:42:08 +00:00
|
|
|
|
|
|
|
|
|
return TryJsonDeserialize<TResponse>(json);
|
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;
|
|
|
|
|
|
|
|
|
|
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-12 11:53:55 +00:00
|
|
|
|
|
2023-04-13 07:33:10 +00:00
|
|
|
|
return Time.Wait(response.Content.ReadAsStringAsync());
|
2023-04-12 11:53:55 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Stream HttpGetStream(string route)
|
|
|
|
|
{
|
|
|
|
|
return Retry(() =>
|
|
|
|
|
{
|
|
|
|
|
var client = GetClient();
|
|
|
|
|
var url = GetUrl() + route;
|
|
|
|
|
|
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}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static T Retry<T>(Func<T> operation)
|
|
|
|
|
{
|
|
|
|
|
var retryCounter = 0;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return operation();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
Timing.HttpCallRetryDelay();
|
|
|
|
|
retryCounter++;
|
|
|
|
|
if (retryCounter > Timing.HttpCallRetryCount())
|
|
|
|
|
{
|
2023-04-19 08:42:08 +00:00
|
|
|
|
Assert.Fail(exception.ToString());
|
2023-04-12 11:53:55 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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}";
|
|
|
|
|
Assert.Fail(msg);
|
|
|
|
|
throw new InvalidOperationException(msg, exception);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private static HttpClient GetClient()
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
client.Timeout = Timing.HttpCallTimeout();
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|