private HttpWebResponse ExecuteWithRetry(RestRequest request) { var maxRetries = 4; var numberOfAttempts = 0; while (numberOfAttempts < maxRetries) { var response = _oAuthSignedRestClient.Execute(request); // See https://developer.deere.com/#!documentation&doc=myjohndeere%2F429.htm // Note System.Net.HttpStatusCode does not include 429, but C# enum handling allows non-specified values if (response.StatusCode == (HttpStatusCode)429) { var secondsToWait = Int32.Parse(response.Headers.Get("Retry-After")); Thread.Sleep(1000 * secondsToWait); } else if (response.StatusCode == HttpStatusCode.ServiceUnavailable || response.StatusCode == HttpStatusCode.InternalServerError) { // Sorry. Something probably went wrong on our end. // Exponential backoff raises the odds that waiting a few milliseconds results in a successful retry Thread.Sleep((int)Math.Pow(10, numberOfAttempts + 1)); } else { return(response); } numberOfAttempts++; } return(null); }
private static ApiCatalog GetApiCatalog(OAuthSignedRestClient oAuthSignedRestClient) { // The API Catalog is a collection of links to other available MyJohnDeere API's. // It's an easy place to discover other API's that look interesting, or just explore what's available. var restRequest = new RestRequest { Url = "https://sandboxapi.deere.com/platform/", Method = HttpMethod.Get }; var response = oAuthSignedRestClient.Execute(restRequest); var apiCatalog = response.GetResponseAsObject <ApiCatalog>(); apiCatalog.links.ForEach(link => Console.WriteLine($"{link.rel}: {link.uri}")); return(apiCatalog); }