pleroma: retry if throttled
This commit is contained in:
parent
f4862e6c8a
commit
43ec5a372d
1 changed files with 86 additions and 6 deletions
|
@ -40,6 +40,85 @@ public class Pleroma
|
|||
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
|
||||
{
|
||||
req.Fields.UserAgent = UserAgent;
|
||||
|
@ -88,7 +167,7 @@ public class Pleroma
|
|||
HttpRequest req = new HttpRequest(HttpMethod.POST, "/api/v1/statuses");
|
||||
req.Content = new HttpContent(JsonMIMEType, JsonSerializer.SerializeToUtf8Bytes(status, SerializerOptions));
|
||||
req.Fields.Accept = [JsonMIMEType];
|
||||
return RequestJSON<Status>(req)!;
|
||||
return RequestJSONRetry<Status>(req)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -99,7 +178,7 @@ public class Pleroma
|
|||
//TODO: Parameters and selecting different timelines (home, public, bubble)
|
||||
HttpRequest req = new HttpRequest(HttpMethod.GET, "/api/v1/timelines/public");
|
||||
req.Fields.Accept = [ JsonMIMEType ];
|
||||
return RequestJSON<Status[]>(req)!;
|
||||
return RequestJSONRetry<Status[]>(req)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -115,7 +194,7 @@ public class Pleroma
|
|||
{
|
||||
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/accounts/{account_id}/statuses");
|
||||
req.Fields.Accept = [JsonMIMEType];
|
||||
return RequestJSON<Status[]>(req)!;
|
||||
return RequestJSONRetry<Status[]>(req)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -125,7 +204,7 @@ public class Pleroma
|
|||
{
|
||||
HttpRequest req = new HttpRequest(HttpMethod.GET, "/api/v1/accounts/verify_credentials");
|
||||
req.Fields.Accept = [JsonMIMEType];
|
||||
return RequestJSON<Account>(req)!;
|
||||
return RequestJSONRetry<Account>(req)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -135,7 +214,7 @@ public class Pleroma
|
|||
{
|
||||
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/accounts/{id}");
|
||||
req.Fields.Accept = [JsonMIMEType];
|
||||
return RequestJSON<Account>(req);
|
||||
return RequestJSONRetry<Account>(req);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -151,6 +230,7 @@ public class Pleroma
|
|||
{
|
||||
HttpRequest req = new HttpRequest(HttpMethod.GET, $"/api/v1/statuses/{status_id}/context");
|
||||
req.Fields.Accept = [JsonMIMEType];
|
||||
return RequestJSON<Context>(req);
|
||||
return RequestJSONRetry<Context>(req);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue