public static async Task ApiTestAsync( Func <ScraperApiClient, CancellationToken, Task> action) { using var source = new CancellationTokenSource(TimeSpan.FromMinutes(1)); var cancellationToken = source.Token; var token = Environment.GetEnvironmentVariable("SCRAPER_API_TOKEN") ?? throw new InvalidOperationException("token is null."); using var client = new HttpClient(); var api = new ScraperApiClient(token, client); try { await action(api, cancellationToken).ConfigureAwait(false); } catch (ApiException exception) { // ignore server errors. if (exception.StatusCode == 500) { return; } throw; } }
private static async Task Main() { using var client = new HttpClient(); var apiKey = Environment.GetEnvironmentVariable("SCRAPER_API_TOKEN") ?? string.Empty; var api = new ScraperApiClient(apiKey, client); var result = await api.GetAsync("http://httpbin.org/ip"); Console.WriteLine($"Result: {result}"); }