using System.Net;
using System.Net.Http.Headers;
using System.Text;
namespace Uwaa.Pleroma;
delegate void AddPair(string key, string value);
///
/// A pleroma client.
///
public class Pleroma
{
static readonly JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
};
internal static string CreateQuery(Action generator)
{
StringBuilder sb = new StringBuilder();
void addPair(string key, string value)
{
if (sb.Length == 0)
sb.Append('?');
else
sb.Append('&');
sb.Append(WebUtility.UrlEncode(key));
sb.Append('=');
sb.Append(WebUtility.UrlEncode(value));
}
generator(addPair);
return sb.ToString();
}
public HttpClient HttpClient;
public Pleroma(string host, string? authorization, string? userAgent = "Uwaa.Pleroma/0.0")
{
UriBuilder builder = new UriBuilder();
builder.Scheme = "https";
builder.Host = host;
HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.Add("User-agent", userAgent);
HttpClient.BaseAddress = builder.Uri;
HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (authorization != null)
HttpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authorization);
}
public Pleroma(HttpClient client)
{
HttpClient = client;
}
internal Task Retry(Func reqFactory)
{
return Retry