コード例 #1
0
ファイル: GalleryEndpoint.cs プロジェクト: shiglet/Imgur.API
        /// <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 = GalleryRequestBuilder.SearchGalleryRequest(url, query);

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

                return(searchResults);
            }
        }
コード例 #2
0
        public void SearchGalleryRequest_Equal()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new GalleryRequestBuilder();

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

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

            Assert.Equal(expected, actual);
        }
コード例 #3
0
        public void SearchGalleryRequest_WithUrlNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new GalleryRequestBuilder();

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

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

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal(argNullException.ParamName, "url");
        }
コード例 #4
0
        /// <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 Basic<IEnumerable<GalleryItem>> SearchGallery(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 = GalleryRequestBuilder.SearchGalleryRequest(url, query);

            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;
            }
        }