diff --git a/GoogleMapsApi/Engine/MapsAPIGenericEngine.cs b/GoogleMapsApi/Engine/MapsAPIGenericEngine.cs index 261de0d..95b1f98 100644 --- a/GoogleMapsApi/Engine/MapsAPIGenericEngine.cs +++ b/GoogleMapsApi/Engine/MapsAPIGenericEngine.cs @@ -9,7 +9,6 @@ using System.Threading.Tasks; using System.Text; - namespace GoogleMapsApi.Engine { public delegate Uri UriCreatedDelegate(Uri uri); @@ -21,7 +20,8 @@ public abstract class MapsAPIGenericEngine { internal static event UriCreatedDelegate OnUriCreated; internal static event RawResponseReceivedDelegate OnRawResponseReceived; - internal static TimeSpan DefaultTimeout = TimeSpan.FromSeconds(100); + + private static readonly HttpClient client = new HttpClient(); protected internal static async Task QueryGoogleAPIAsync(TRequest request, TimeSpan timeout, CancellationToken token = default) { @@ -31,9 +31,7 @@ protected internal static async Task QueryGoogleAPIAsync(TRequest req var requstUri = request.GetUri(); var uri = OnUriCreated?.Invoke(requstUri) ?? requstUri; - var client = new HttpClient(); - - var response = await client.DownloadDataTaskAsyncAsString(uri, timeout, token).ConfigureAwait(false); + var response = await client.DownloadDataTaskAsyncAsString(uri, timeout, token).ConfigureAwait(false); OnRawResponseReceived?.Invoke(Encoding.UTF8.GetBytes(response)); diff --git a/GoogleMapsApi/HttpClientExtensions.cs b/GoogleMapsApi/HttpClientExtensions.cs index 83c823f..97972b6 100644 --- a/GoogleMapsApi/HttpClientExtensions.cs +++ b/GoogleMapsApi/HttpClientExtensions.cs @@ -45,12 +45,14 @@ public static class HttpClientExtensions public static async Task DownloadDataTaskAsyncAsString(this HttpClient client, Uri address, TimeSpan timeout, CancellationToken token = new CancellationToken()) { - var dataDownloaded = await DownloadData(client, address, timeout, token).ConfigureAwait(false); - - if (dataDownloaded != null) - return await dataDownloaded.Content.ReadAsStringAsync().ConfigureAwait(false); - - return await GetCancelledTask().ConfigureAwait(false); + using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token)) + { + cts.CancelAfter(timeout); + + HttpResponseMessage response = await client.GetAsync(address, cts.Token).ConfigureAwait(false); + await HandleResponse(response, timeout); + return await response.Content.ReadAsStringAsync().ConfigureAwait(false); + } } private static async Task GetCancelledTask() @@ -70,24 +72,26 @@ private static async Task GetCancelledTask() if (token.IsCancellationRequested) return null; - client.Timeout = timeout; var httpResponse = await client.GetAsync(address, token).ConfigureAwait(false); + await HandleResponse(httpResponse, timeout); + return httpResponse; + } - if (!httpResponse.IsSuccessStatusCode) + private static async Task HandleResponse(HttpResponseMessage response, TimeSpan timeout) + { + if (!response.IsSuccessStatusCode) { - if (httpResponse.StatusCode == HttpStatusCode.Forbidden || - httpResponse.StatusCode == HttpStatusCode.ProxyAuthenticationRequired || - httpResponse.StatusCode == HttpStatusCode.Unauthorized) - throw new AuthenticationException(await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); + if (response.StatusCode == HttpStatusCode.Forbidden || + response.StatusCode == HttpStatusCode.ProxyAuthenticationRequired || + response.StatusCode == HttpStatusCode.Unauthorized) + throw new AuthenticationException(await response.Content.ReadAsStringAsync().ConfigureAwait(false)); - if (httpResponse.StatusCode == HttpStatusCode.GatewayTimeout || - httpResponse.StatusCode == HttpStatusCode.RequestTimeout) + if (response.StatusCode == HttpStatusCode.GatewayTimeout || + response.StatusCode == HttpStatusCode.RequestTimeout) throw new TimeoutException($"The request has exceeded the timeout limit of {timeout} and has been aborted."); - throw new HttpRequestException($"Failed with HttpResponse: {httpResponse.StatusCode} and message: {httpResponse.ReasonPhrase}"); + throw new HttpRequestException($"Failed with HttpResponse: {response.StatusCode} and message: {response.ReasonPhrase}"); } - - return httpResponse; } } } \ No newline at end of file