/*Relyence API Retry Example - C# This file contains C# sample code demonstrating how to handle HTTP 429 (Too Many Requests) responses from the Relyence API. This example is provided as a .txt file for email compatibility. You may copy the applicable code into a .cs file in your project and adapt it to your integration. The example: - Retries requests that receive HTTP 429 - Uses the Retry-After response header when available - Falls back to exponential backoff with jitter - Stops retrying after a defined number of attempts This is reference code and may require modification for your application's framework, API client, error handling, and concurrency model.*/ using System; using RestSharp; using System.Linq; using System.Threading; using System.Diagnostics; public class ApiExample { const int TooManyRequestsStatusCode = 429; public static RestResponse ExecuteWithRetry(Func apiCall, int maxRetries = 6) { for (int attempt = 1; attempt <= maxRetries; attempt++) { RestResponse response = apiCall(); if ((int)response.StatusCode != TooManyRequestsStatusCode) { return response; } if (attempt == maxRetries) { return response; } //get retry-after from header if it exists otherwise use exponential backoff var retryAfter = response.Headers?.FirstOrDefault(h => h.Name.Equals("Retry-After", StringComparison.OrdinalIgnoreCase))?.Value?.ToString(); int delayMs; if (int.TryParse(retryAfter, out int seconds)) { delayMs = seconds * 1000; } else { var exponential = Math.Pow(2, attempt) * 1000; var jitter = new Random().Next(0, 500); delayMs = (int)exponential + jitter; } Thread.Sleep(delayMs); } throw new Exception("Retry loop failed unexpectedly."); } public static bool DeleteFunction(string address, BearerToken token, int functionId) { try { var client = new RestClient(address); var request = new RestRequest($"/v1/function"); request.AddHeader("Authorization", $"Bearer {token.AccessToken}"); request.AddQueryParameter("id", functionId); RestResponse response = ExecuteWithRetry(() => client.ExecuteDelete(request)); if (response.IsSuccessful) { return true; } else return false; } catch (Exception ex) { Debug.WriteLine(ex.Message); return false; } } }