Properly remake request object on retry
This commit is contained in:
		@@ -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)
 | 
				
			||||||
@@ -81,48 +81,55 @@ 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
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            using var response = await Send(() =>
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path);
 | 
					                var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path);
 | 
				
			||||||
                SetRequestJsonBody(request, body);
 | 
					                SetRequestJsonBody(request, body);
 | 
				
			||||||
 | 
					                return request;
 | 
				
			||||||
            var response = await Send(request, ratelimitParams);
 | 
					            }, 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
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            using var response = await Send(() =>
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path);
 | 
					                var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path);
 | 
				
			||||||
                SetRequestFormDataBody(request, payload, files);
 | 
					                SetRequestFormDataBody(request, payload, files);
 | 
				
			||||||
 | 
					                return request;
 | 
				
			||||||
            var response = await Send(request, ratelimitParams);
 | 
					            }, 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
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            using var response = await Send(() =>
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var request = new HttpRequestMessage(HttpMethod.Patch, ApiBaseUrl + path);
 | 
					                var request = new HttpRequestMessage(HttpMethod.Patch, ApiBaseUrl + path);
 | 
				
			||||||
                SetRequestJsonBody(request, body);
 | 
					                SetRequestJsonBody(request, body);
 | 
				
			||||||
 | 
					                return request;
 | 
				
			||||||
            var response = await Send(request, ratelimitParams);
 | 
					            }, 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
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            using var response = await Send(() =>
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var request = new HttpRequestMessage(HttpMethod.Put, ApiBaseUrl + path);
 | 
					                var request = new HttpRequestMessage(HttpMethod.Put, ApiBaseUrl + path);
 | 
				
			||||||
                SetRequestJsonBody(request, body);
 | 
					                SetRequestJsonBody(request, body);
 | 
				
			||||||
 | 
					                return request;
 | 
				
			||||||
            var response = await Send(request, ratelimitParams);
 | 
					            }, 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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user