Properly remake request object on retry

This commit is contained in:
Ske 2021-03-21 14:15:13 +01:00
parent 0c35975605
commit a63e75a490

View File

@ -69,8 +69,8 @@ namespace Myriad.Rest
public async Task<T?> Get<T>(string path, (string endpointName, ulong major) ratelimitParams) where T: class public async Task<T?> Get<T>(string path, (string endpointName, ulong major) ratelimitParams) where T: class
{ {
var request = new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + path); using var response = await Send(() => new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + path),
var response = await Send(request, ratelimitParams, true); ratelimitParams, true);
// GET-only special case: 404s are nulls and not exceptions // GET-only special case: 404s are nulls and not exceptions
if (response.StatusCode == HttpStatusCode.NotFound) if (response.StatusCode == HttpStatusCode.NotFound)
@ -82,47 +82,54 @@ namespace Myriad.Rest
public async Task<T?> Post<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body) public async Task<T?> Post<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body)
where T: class where T: class
{ {
var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path); using var response = await Send(() =>
SetRequestJsonBody(request, body); {
var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path);
var response = await Send(request, ratelimitParams); SetRequestJsonBody(request, body);
return request;
}, ratelimitParams);
return await ReadResponse<T>(response); return await ReadResponse<T>(response);
} }
public async Task<T?> PostMultipart<T>(string path, (string endpointName, ulong major) ratelimitParams, object? payload, MultipartFile[]? files) public async Task<T?> PostMultipart<T>(string path, (string endpointName, ulong major) ratelimitParams, object? payload, MultipartFile[]? files)
where T: class where T: class
{ {
var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path); using var response = await Send(() =>
SetRequestFormDataBody(request, payload, files); {
var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path);
var response = await Send(request, ratelimitParams); SetRequestFormDataBody(request, payload, files);
return request;
}, ratelimitParams);
return await ReadResponse<T>(response); return await ReadResponse<T>(response);
} }
public async Task<T?> Patch<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body) public async Task<T?> Patch<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body)
where T: class where T: class
{ {
var request = new HttpRequestMessage(HttpMethod.Patch, ApiBaseUrl + path); using var response = await Send(() =>
SetRequestJsonBody(request, body); {
var request = new HttpRequestMessage(HttpMethod.Patch, ApiBaseUrl + path);
var response = await Send(request, ratelimitParams); SetRequestJsonBody(request, body);
return request;
}, ratelimitParams);
return await ReadResponse<T>(response); return await ReadResponse<T>(response);
} }
public async Task<T?> Put<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body) public async Task<T?> Put<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body)
where T: class where T: class
{ {
var request = new HttpRequestMessage(HttpMethod.Put, ApiBaseUrl + path); using var response = await Send(() =>
SetRequestJsonBody(request, body); {
var request = new HttpRequestMessage(HttpMethod.Put, ApiBaseUrl + path);
var response = await Send(request, ratelimitParams); SetRequestJsonBody(request, body);
return request;
}, ratelimitParams);
return await ReadResponse<T>(response); return await ReadResponse<T>(response);
} }
public async Task Delete(string path, (string endpointName, ulong major) ratelimitParams) public async Task Delete(string path, (string endpointName, ulong major) ratelimitParams)
{ {
var request = new HttpRequestMessage(HttpMethod.Delete, ApiBaseUrl + path); using var _ = await Send(() => new HttpRequestMessage(HttpMethod.Delete, ApiBaseUrl + path), ratelimitParams);
await Send(request, ratelimitParams);
} }
private void SetRequestJsonBody(HttpRequestMessage request, object? body) private void SetRequestJsonBody(HttpRequestMessage request, object? body)
@ -159,12 +166,13 @@ namespace Myriad.Rest
return await response.Content.ReadFromJsonAsync<T>(_jsonSerializerOptions); return await response.Content.ReadFromJsonAsync<T>(_jsonSerializerOptions);
} }
private async Task<HttpResponseMessage> Send(HttpRequestMessage request, private async Task<HttpResponseMessage> Send(Func<HttpRequestMessage> createRequest,
(string endpointName, ulong major) ratelimitParams, (string endpointName, ulong major) ratelimitParams,
bool ignoreNotFound = false) bool ignoreNotFound = false)
{ {
return await _retryPolicy.ExecuteAsync(async _ => return await _retryPolicy.ExecuteAsync(async _ =>
{ {
var request = createRequest();
_logger.Debug("Sending request: {RequestMethod} {RequestPath}", _logger.Debug("Sending request: {RequestMethod} {RequestPath}",
request.Method, request.RequestUri); request.Method, request.RequestUri);