Skip to content

Commit

Permalink
Initial Polly wrapper for GoogleOptimizeRouteService (#1715)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon authored and BillWagner committed Jan 16, 2017
1 parent fe213ca commit a722abf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Polly;

namespace AllReady.Services.Mapping.Routing
{
Expand Down Expand Up @@ -36,31 +37,48 @@ public async Task<OptimizeRouteResult> OptimizeRoute(OptimizeRouteCriteria crite
{
try
{
// todo sgordon: enhance with Polly for retries
var response = await _httpClient.GetAsync(GenerateGoogleApiUrl(criteria));

if (response.IsSuccessStatusCode)
return await GetRetryPolicy().ExecuteAsync(async () =>
{
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<GoogleDirectionsResponse>(content);
var googleResponse = await _httpClient.GetAsync(GenerateGoogleApiUrl(criteria));
if (result.Status == GoogleDirectionsResponse.OkStatus)
if (googleResponse.IsSuccessStatusCode)
{
requestIds.AddRange(result.Routes.SelectMany(r => r.WaypointOrder).Select(waypointIndex => criteria.Waypoints[waypointIndex].RequestId));
var content = await googleResponse.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<GoogleDirectionsResponse>(content);
if (result.Status == GoogleDirectionsResponse.OkStatus)
{
requestIds.AddRange(result.Routes.SelectMany(r => r.WaypointOrder).Select(waypointIndex => criteria.Waypoints[waypointIndex].RequestId));
return new OptimizeRouteResult { RequestIds = requestIds, Distance = result.TotalDistance, Duration = result.TotalDuration };
return new OptimizeRouteResult { RequestIds = requestIds, Distance = result.TotalDistance, Duration = result.TotalDuration };
}
}
}
else
{
// for now we fire an exception to force a Polly retry. We could review this later once we have logging to determine specific codes
// that represent errors
throw new Exception("Non success code");
}
return null;
});
}
catch (Exception ex)
catch
{
_logger.LogError($"Failure to contact Google Directions API - {ex}");
}
// todo - handle other status codes and logging
}
}

return null;
}

private Policy GetRetryPolicy()
{
return Policy
.Handle<Exception>()
.RetryAsync(2); // in total we will make 3 attempts
}

private string GenerateGoogleApiUrl(OptimizeRouteCriteria criteria)
{
var requestUrl = new StringBuilder("https://maps.googleapis.com/maps/api/directions/json?origin=");
Expand Down
3 changes: 2 additions & 1 deletion AllReadyApp/Web-App/AllReady/project.json
Expand Up @@ -62,7 +62,8 @@
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
"WindowsAzure.Storage": "7.1.3-preview",
"Twilio": "5.0.0-rca2",
"SendGrid.NetCore": "1.0.0-rtm-00002"
"SendGrid.NetCore": "1.0.0-rtm-00002",
"Polly": "5.0.3"
},

"tools": {
Expand Down

0 comments on commit a722abf

Please sign in to comment.