private async Task <string> SendRequestAsync(string relativeUrl, AllowedHttpMethod method, object requestBody = null) { try { var request = CreateRequest(relativeUrl, method); if (requestBody != null) { await WriteRequestBody(request, requestBody); } using var response = await request.GetResponseAsync(); using var responseStream = response.GetResponseStream(); using var reader = new StreamReader(responseStream); return(reader.ReadToEnd()); } catch (Exception exception) { var stringBuilder = new StringBuilder($"Failed to send {method} to {BaseUrl}{relativeUrl}: "); if (exception is WebException webException && webException?.Response is HttpWebResponse response) { stringBuilder.Append($"Server returned a status code of {response.StatusCode}"); }
private async Task <T> SendRequestAsync <T>(string relativeUrl, AllowedHttpMethod method, object requestBody = null) { try { var responseString = await SendRequestAsync(relativeUrl, method, requestBody); return(JsonHelper.Deserialise <T>(responseString)); } catch (System.Text.Json.JsonException exception) { Logger.LogError($"Failed to deserialise response from {relativeUrl} to instance of {typeof(T).Name}: {exception.Message}"); throw; } }