public static async Task <IGalleryItem> GetImgurGalleryAsync(string query, GallerySortOrder order = GallerySortOrder.Top, TimeWindow time = TimeWindow.All) { var random = new Random(); var imgur = new ImgurClient(TokenHandler.Tokens.ImgurToken); var endpoint = new GalleryEndpoint(imgur); var gallery = string.IsNullOrWhiteSpace(query) ? (await endpoint.GetRandomGalleryAsync().ConfigureAwait(false)).ToList() : (await endpoint.SearchGalleryAsync(query, order, time).ConfigureAwait(false)).ToList(); return((gallery.Count > 0) ? gallery[random.Next(0, gallery.Count)] : null); }
/// <summary> /// Returns the images in the gallery. /// </summary> /// <param name="section">The gallery section. Default: Hot</param> /// <param name="sort">The order that the gallery should be sorted by. Default: Viral</param> /// <param name="window">The time period that should be used in filtering requests. Default: Day</param> /// <param name="page">The data paging number. Default: null</param> /// <param name="showViral">Show or hide viral images from the 'user' section. Default: true</param> /// <exception cref="ArgumentNullException"> /// Thrown when a null reference is passed to a method that does not accept it as a /// valid argument. /// </exception> /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception> /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception> /// <returns></returns> public async Task<IEnumerable<IGalleryItem>> GetGalleryAsync(GallerySection? section = GallerySection.Hot, GallerySortOrder? sort = GallerySortOrder.Viral, TimeWindow? window = TimeWindow.Day, int? page = null, bool? showViral = true) { section = section ?? GallerySection.Hot; sort = sort ?? GallerySortOrder.Viral; window = window ?? TimeWindow.Week; showViral = showViral ?? true; var sectionValue = $"{section}".ToLower(); var sortValue = $"{sort}".ToLower(); var windowValue = $"{window}".ToLower(); var showViralValue = $"{showViral}".ToLower(); var url = $"gallery/{sectionValue}/{sortValue}/{windowValue}/{page}?showViral={showViralValue}"; using (var request = RequestBuilder.CreateRequest(HttpMethod.Get, url)) { var gallery = await SendRequestAsync<IEnumerable<GalleryItem>>(request).ConfigureAwait(false); return gallery; } }
/// <summary> /// Search the gallery with a given query string. /// </summary> /// <param name="query"> /// Query string to search by. This parameter also supports boolean operators (AND, OR, NOT) and /// indices (tag: user: title: ext: subreddit: album: meme:). An example compound query would be 'title: cats AND dogs /// ext: gif' /// </param> /// <param name="sort">The order that the gallery should be sorted by. Default: Time</param> /// <param name="window">The time period that should be used in filtering requests. Default: Day</param> /// <param name="page">The data paging number. Default: null</param> /// <exception cref="ArgumentNullException"> /// Thrown when a null reference is passed to a method that does not accept it as a /// valid argument. /// </exception> /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception> /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception> /// <returns></returns> public async Task<IEnumerable<IGalleryItem>> SearchGalleryAsync(string query, GallerySortOrder? sort = GallerySortOrder.Time, TimeWindow? window = TimeWindow.All, int? page = null) { if (string.IsNullOrWhiteSpace(query)) throw new ArgumentNullException(nameof(query)); sort = sort ?? GallerySortOrder.Time; window = window ?? TimeWindow.All; var sortValue = $"{sort}".ToLower(); var windowValue = $"{window}".ToLower(); var url = $"gallery/search/{sortValue}/{windowValue}/{page}"; url = RequestBuilder.SearchGalleryRequest(url, query); using (var request = RequestBuilder.CreateRequest(HttpMethod.Get, url)) { var searchResults = await SendRequestAsync<IEnumerable<GalleryItem>>(request).ConfigureAwait(false); return searchResults; } }
/// <summary> /// Search the gallery with a given query string. /// </summary> /// <param name="qAll">Search for all of these words (and).</param> /// <param name="qAny">Search for any of these words (or).</param> /// <param name="qExactly">Search for exactly this word or phrase.</param> /// <param name="qNot">Exclude results matching this word or phrase.</param> /// <param name="fileType">Show results for a specific file type.</param> /// <param name="imageSize">Show results for a specific image size.</param> /// <param name="sort">The order that the gallery should be sorted by. Default: Time</param> /// <param name="window">The time period that should be used in filtering requests. Default: Day</param> /// <param name="page">The data paging number. Default: null</param> /// <exception cref="ArgumentNullException"> /// Thrown when a null reference is passed to a method that does not accept it as a /// valid argument. /// </exception> /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception> /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception> /// <returns></returns> public async Task<IEnumerable<IGalleryItem>> SearchGalleryAdvancedAsync( string qAll = null, string qAny = null, string qExactly = null, string qNot = null, ImageFileType? fileType = null, ImageSize? imageSize = null, GallerySortOrder? sort = GallerySortOrder.Time, TimeWindow? window = TimeWindow.All, int? page = null) { if (string.IsNullOrWhiteSpace(qAll) && string.IsNullOrWhiteSpace(qAny) && string.IsNullOrWhiteSpace(qExactly) && string.IsNullOrWhiteSpace(qNot)) throw new ArgumentNullException(null, "At least one search parameter must be provided (All | Any | Exactly | Not)."); sort = sort ?? GallerySortOrder.Time; window = window ?? TimeWindow.All; var sortValue = $"{sort}".ToLower(); var windowValue = $"{window}".ToLower(); var url = $"gallery/search/{sortValue}/{windowValue}/{page}"; url = RequestBuilder.SearchGalleryAdvancedRequest(url, qAll, qAny, qExactly, qNot, fileType, imageSize); using (var request = RequestBuilder.CreateRequest(HttpMethod.Get, url)) { var searchResults = await SendRequestAsync<IEnumerable<GalleryItem>>(request).ConfigureAwait(false); return searchResults; } }