Files

25 lines
1.0 KiB
C#
Raw Permalink Normal View History

using System.Net;
using System.Net.Http.Headers;
using System.Text.Json.Nodes;
namespace decePubClient.E2E.Support
{
// PrivaPub's Mastodon API as a persona (its town token), for what a test sets up or checks behind the page
public static class PersonaApi
{
// the answer's JSON, or null for a 404
public static async Task<JsonNode> Call(Pasture.Persona persona, string path, HttpMethod method = null, JsonObject body = null)
{
using var http = Pasture.Http();
using var request = new HttpRequestMessage(method ?? HttpMethod.Get, Pasture.Api + path);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", persona.Token);
if (body is not null)
request.Content = new StringContent(body.ToJsonString(), System.Text.Encoding.UTF8, "application/json");
using var response = await http.SendAsync(request);
if (response.StatusCode == HttpStatusCode.NotFound)
return null;
response.EnsureSuccessStatusCode();
return JsonNode.Parse(await response.Content.ReadAsStringAsync());
}
}
}