public static async Task <HTTPResponse> Request(string url, HttpMethod method, HttpContent param, IDictionary <string, string> headers, IDictionary <string, string> cookies, CancellationToken cancellationToken) { var result = new HTTPResponse() { IsOK = false, }; url = Uri.EscapeUriString(Uri.UnescapeDataString(url)); if (method == null) { method = HttpMethod.Get; } var request = new HttpRequestMessage(method, new Uri(url)); Logger.Debug(string.Format(CultureInfo.InvariantCulture, "Requesting {1} \"{0}\"", request.RequestUri.AbsoluteUri, method.Method)); request.Headers.TryAddWithoutValidation("User-Agent", GetUserAgent()); if (param != null) { request.Content = param; } if (headers != null) { foreach (var header in headers) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); } } if (cookies != null) { foreach (var cookie in cookies) { CookieContainer.Add(request.RequestUri, new Cookie(cookie.Key, cookie.Value)); } } HttpResponseMessage response = null; try { response = await Http.SendAsync(request, cancellationToken).ConfigureAwait(false); } catch (Exception e) { Logger.Error($"Request error: {e.Message}"); await Analytics.Send(url, null, null, null, null, null, e, cancellationToken).ConfigureAwait(false); } if (response != null) { result.IsOK = response.IsSuccessStatusCode; #if __EMBY__ result.Content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); result.ContentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); #else result.Content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); result.ContentStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); #endif result.Headers = response.Headers; result.Cookies = CookieContainer.GetCookies(request.RequestUri).Cast <Cookie>(); } return(result); }
public static async Task <List <RemoteImageInfo> > GetImagesSizeAndValidate(IEnumerable <RemoteImageInfo> images, CancellationToken cancellationToken) { var result = new List <RemoteImageInfo>(); var tasks = new List <Task <RemoteImageInfo> >(); var cleanImages = Cleanup(images); var primaryList = cleanImages.Where(o => o.Type == ImageType.Primary).ToList(); var backdropList = cleanImages.Where(o => o.Type == ImageType.Backdrop).ToList(); var dublList = new List <RemoteImageInfo>(); foreach (var image in primaryList) { tasks.Add(GetImageSizeAndValidate(image, cancellationToken)); } foreach (var image in backdropList) { if (!primaryList.Where(o => o.Url == image.Url).Any()) { tasks.Add(GetImageSizeAndValidate(image, cancellationToken)); } else { dublList.Add(image); } } try { await Task.WhenAll(tasks).ConfigureAwait(false); } catch (Exception e) { Logger.Error($"GetImagesSizeAndValidate error: \"{e}\""); await Analytics.Send(null, null, null, null, null, null, e, cancellationToken).ConfigureAwait(false); } finally { foreach (var task in tasks) { var res = task.Result; if (res != null) { result.Add(res); } } } if (result.Any()) { foreach (var image in dublList) { var res = result.Where(o => o.Url == image.Url); if (res.Any()) { var img = res.First(); result.Add(new RemoteImageInfo { ProviderName = image.ProviderName, Url = image.Url, Type = ImageType.Backdrop, Height = img.Height, Width = img.Width, }); } } } return(result); }