Example #1
0
        /// <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 Basic<IEnumerable<GalleryItem>> SearchGalleryAdvanced(
            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 = GalleryRequestBuilder.SearchGalleryAdvancedRequest(url, qAll, qAny, qExactly, qNot, fileType,
                imageSize);

            using (var request = RequestBuilderBase.CreateRequest(HttpMethod.Get, url))
            {
                var httpResponse = HttpClient.SendAsync(request).Result;
                var jsonString = httpResponse.Content.ReadAsStringAsync().Result;
                var output = Newtonsoft.Json.JsonConvert.DeserializeObject<Basic<IEnumerable<GalleryItem>>>(httpResponse.Content.ReadAsStringAsync().Result.ToString());
                return output;
            }
        }
Example #2
0
        /// <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 = GalleryRequestBuilder.SearchGalleryAdvancedRequest(url, qAll, qAny, qExactly, qNot, fileType,
                                                                     imageSize);

            using (var request = RequestBuilderBase.CreateRequest(HttpMethod.Get, url))
            {
                var searchResults = await SendRequestAsync <IEnumerable <GalleryItem> >(request).ConfigureAwait(false);

                return(searchResults);
            }
        }
Example #3
0
        public void SearchGalleryAdvancedRequest_WithQueryNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new GalleryRequestBuilder();

            var exception = Record.Exception(() => GalleryRequestBuilder.SearchGalleryAdvancedRequest("Xio"));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            Assert.True(
                exception.Message.Contains("At least one search parameter must be provided (All | Any | Exactly | Not)"));
        }
Example #4
0
        public void SearchGalleryAdvancedRequest_WithDefaults_Equal()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new GalleryRequestBuilder();

            var mockUrl = $"{client.EndpointUrl}gallery/search";
            var actual  = GalleryRequestBuilder.SearchGalleryAdvancedRequest(mockUrl, "star wars");

            var expected =
                "https://api.imgur.com/3/gallery/search?q_all=star+wars";

            Assert.Equal(expected, actual);
        }
Example #5
0
        public void SearchGalleryAdvancedRequest_WithUrlNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new GalleryRequestBuilder();

            var exception = Record.Exception(() => GalleryRequestBuilder.SearchGalleryAdvancedRequest(null, "dfdfd"));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal(argNullException.ParamName, "url");
        }
Example #6
0
        public void SearchGalleryAdvancedRequest_Equal()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new GalleryRequestBuilder();

            var mockUrl = $"{client.EndpointUrl}gallery/search";
            var actual  = GalleryRequestBuilder.SearchGalleryAdvancedRequest(mockUrl, "star wars", "luke han leia", "Obi-Wan",
                                                                             "Kirk", ImageFileType.Anigif, ImageSize.Lrg);

            var expected =
                "https://api.imgur.com/3/gallery/search?q_all=star+wars&q_any=luke+han+leia&q_exactly=Obi-Wan&q_not=Kirk&q_type=anigif&q_size_px=lrg";

            Assert.Equal(expected, actual);
        }