Compare commits
No commits in common. "9aa8a41389a52a141fcd969003ef2a06e1461114" and "9834d350ec6fa9c180b6088b1ca8fee10f0cb19b" have entirely different histories.
9aa8a41389
...
9834d350ec
3 changed files with 9 additions and 113 deletions
|
@ -75,20 +75,11 @@ class HttpStream : IDisposable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask<int> Read(Memory<byte> buffer)
|
public ValueTask<int> Read(Memory<byte> buffer)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int index = 0;
|
return Buffer.ReadAsync(buffer);
|
||||||
while (index < buffer.Length)
|
|
||||||
{
|
|
||||||
int count = await Buffer.ReadAsync(buffer[index..]);
|
|
||||||
if (count == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
index += count;
|
|
||||||
}
|
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,7 +35,7 @@ public class Status
|
||||||
public bool Pinned { get; set; }
|
public bool Pinned { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string Content => Pleroma?.Content?.Plain ?? HtmlContent;
|
public string Content => Pleroma?.Content.Plain ?? HtmlContent;
|
||||||
|
|
||||||
public bool CheckMention(string id)
|
public bool CheckMention(string id)
|
||||||
{
|
{
|
||||||
|
|
|
@ -40,85 +40,6 @@ public class Pleroma
|
||||||
Authorization = authorization;
|
Authorization = authorization;
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task RequestJSONRetry(HttpRequest req)
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await RequestJSON(req);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
catch (PleromaException e)
|
|
||||||
{
|
|
||||||
if (e.Text == "Throttled")
|
|
||||||
{
|
|
||||||
await Task.Delay(5000);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<T?> RequestJSONRetry<T>(HttpRequest req) where T : class
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return await RequestJSON<T>(req);
|
|
||||||
}
|
|
||||||
catch (PleromaException e)
|
|
||||||
{
|
|
||||||
if (e.Text == "Throttled")
|
|
||||||
{
|
|
||||||
await Task.Delay(5000);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task RequestJSON(HttpRequest req)
|
|
||||||
{
|
|
||||||
req.Fields.UserAgent = UserAgent;
|
|
||||||
req.Fields.Authorization = Authorization;
|
|
||||||
|
|
||||||
HttpResponse res = await HttpClient.Request(Host, true, req);
|
|
||||||
|
|
||||||
if (res.StatusCode == 404)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!res.Fields.ContentType.HasValue || !res.Fields.ContentType.Value.Match(JsonMIMEType))
|
|
||||||
throw new HttpException("Server did not respond with JSON" + (res.Content.HasValue ? ", got: " + res.Content.Value.AsText : null));
|
|
||||||
|
|
||||||
if (!res.Content.HasValue)
|
|
||||||
throw new HttpException("Server responded with no content");
|
|
||||||
|
|
||||||
string text = res.Content.Value.AsText;
|
|
||||||
|
|
||||||
if (res.StatusCode is >= 400 and < 600)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
PleromaException? err = JsonSerializer.Deserialize<PleromaException>(text, SerializerOptions);
|
|
||||||
if (err != null && err.Text != null)
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
catch (JsonException)
|
|
||||||
{
|
|
||||||
//Not an error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res.StatusCode is not >= 200 or not < 300)
|
|
||||||
throw new HttpException("Unknown error occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<T?> RequestJSON<T>(HttpRequest req) where T : class
|
async Task<T?> RequestJSON<T>(HttpRequest req) where T : class
|
||||||
{
|
{
|
||||||
req.Fields.UserAgent = UserAgent;
|
req.Fields.UserAgent = UserAgent;
|
||||||
|
@ -167,7 +88,7 @@ public class Pleroma
|
||||||
HttpRequest req = new HttpRequest(HttpMethod.POST, "/api/v1/statuses");
|
HttpRequest req = new HttpRequest(HttpMethod.POST, "/api/v1/statuses");
|
||||||
req.Content = new HttpContent(JsonMIMEType, JsonSerializer.SerializeToUtf8Bytes(status, SerializerOptions));
|
req.Content = new HttpContent(JsonMIMEType, JsonSerializer.SerializeToUtf8Bytes(status, SerializerOptions));
|
||||||
req.Fields.Accept = [JsonMIMEType];
|
req.Fields.Accept = [JsonMIMEType];
|
||||||
return RequestJSONRetry<Status>(req)!;
|
return RequestJSON<Status>(req)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -178,7 +99,7 @@ public class Pleroma
|
||||||
//TODO: Parameters and selecting different timelines (home, public, bubble)
|
//TODO: Parameters and selecting different timelines (home, public, bubble)
|
||||||
HttpRequest req = new HttpRequest(HttpMethod.GET, "/api/v1/timelines/public");
|
HttpRequest req = new HttpRequest(HttpMethod.GET, "/api/v1/timelines/public");
|
||||||
req.Fields.Accept = [ JsonMIMEType ];
|
req.Fields.Accept = [ JsonMIMEType ];
|
||||||
return RequestJSONRetry<Status[]>(req)!;
|
return RequestJSON<Status[]>(req)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -194,7 +115,7 @@ public class Pleroma
|
||||||
{
|
{
|
||||||
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/accounts/{account_id}/statuses");
|
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/accounts/{account_id}/statuses");
|
||||||
req.Fields.Accept = [JsonMIMEType];
|
req.Fields.Accept = [JsonMIMEType];
|
||||||
return RequestJSONRetry<Status[]>(req)!;
|
return RequestJSON<Status[]>(req)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -204,7 +125,7 @@ public class Pleroma
|
||||||
{
|
{
|
||||||
HttpRequest req = new HttpRequest(HttpMethod.GET, "/api/v1/accounts/verify_credentials");
|
HttpRequest req = new HttpRequest(HttpMethod.GET, "/api/v1/accounts/verify_credentials");
|
||||||
req.Fields.Accept = [JsonMIMEType];
|
req.Fields.Accept = [JsonMIMEType];
|
||||||
return RequestJSONRetry<Account>(req)!;
|
return RequestJSON<Account>(req)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -214,7 +135,7 @@ public class Pleroma
|
||||||
{
|
{
|
||||||
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/accounts/{id}");
|
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/accounts/{id}");
|
||||||
req.Fields.Accept = [JsonMIMEType];
|
req.Fields.Accept = [JsonMIMEType];
|
||||||
return RequestJSONRetry<Account>(req);
|
return RequestJSON<Account>(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -230,22 +151,6 @@ public class Pleroma
|
||||||
{
|
{
|
||||||
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/statuses/{status_id}/context");
|
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/statuses/{status_id}/context");
|
||||||
req.Fields.Accept = [JsonMIMEType];
|
req.Fields.Accept = [JsonMIMEType];
|
||||||
return RequestJSONRetry<Context>(req);
|
return RequestJSON<Context>(req);
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes a status.
|
|
||||||
/// </summary>
|
|
||||||
public Task Delete(Status status)
|
|
||||||
=> Delete(status.ID);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deletes a status by ID.
|
|
||||||
/// </summary>
|
|
||||||
public Task Delete(string status_id)
|
|
||||||
{
|
|
||||||
HttpRequest req = new HttpRequest(HttpMethod.DELETE, $"/api/v1/statuses/{status_id}");
|
|
||||||
req.Fields.Accept = [JsonMIMEType];
|
|
||||||
return RequestJSONRetry(req);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue