private void CheckResponse(HttpResponseMessage response) { // Some pages redirect to https://www.google.com/sorry instead of return 429 if (response.RequestMessage.RequestUri.Host.EndsWith(".google.com", StringComparison.OrdinalIgnoreCase) && response.RequestMessage.RequestUri.LocalPath.StartsWith("/sorry/", StringComparison.OrdinalIgnoreCase)) { throw RequestLimitExceededException.FailedHttpRequest(response); } var statusCode = (int)response.StatusCode; if (statusCode >= 500) { throw TransientFailureException.FailedHttpRequest(response); } if (statusCode == 429) { throw RequestLimitExceededException.FailedHttpRequest(response); } if (statusCode >= 400) { throw FatalFailureException.FailedHttpRequest(response); } }
private static int GetRetryCost(this Exception ex) { return(ex switch { TransientFailureException _ => 1, RequestLimitExceededException _ => 2, FatalFailureException _ => 3, _ => 100 });
public static async Task <ChannelPage> GetByUserNameAsync(YoutubeHttpClient httpClient, string userName) => await Retry.WrapAsync(async() => { var url = $"https://www.youtube.com/user/{userName}?hl=en"; var raw = await httpClient.GetStringAsync(url); var result = Parse(raw); if (!result.IsOk()) { throw TransientFailureException.Generic("Channel page is broken."); } return(result); });