コード例 #1
0
        /// <summary>
        ///     Searches for specific hashtag by search query.
        /// </summary>
        /// <param name="query">Search query</param>
        /// <param name="excludeList">
        ///     Array of numerical hashtag IDs (ie "17841562498105353") to exclude from the response,
        ///     allowing you to skip tags from a previous call to get more results
        /// </param>
        /// <param name="rankToken">The rank token from the previous page's response</param>
        /// <returns>
        ///     List of hashtags
        /// </returns>
        public async Task <IResult <HashtagSearch> > SearchHashtagAsync(string query, IEnumerable <long> excludeList, string rankToken)
        {
            InstaUserAuthValidator.Validate(userAuthValidate);
            var requestHeaderFieldsTooLarge = (HttpStatusCode)431;
            var count = 50;
            var tags  = new HashtagSearch();

            try
            {
                var userUri  = InstaUriCreator.GetSearchTagUri(query, count, excludeList, rankToken);
                var request  = httpHelper.GetDefaultRequest(HttpMethod.Get, userUri, deviceInfo);
                var response = await httpRequestProcessor.SendAsync(request).ConfigureAwait(false);

                var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                if (response.StatusCode == requestHeaderFieldsTooLarge)
                {
                    return(Result.Success(tags));
                }

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(Result.UnExpectedResponse <HashtagSearch>(response, json));
                }

                var tagsResponse = JsonConvert.DeserializeObject <HashtagSearchResponse>(
                    json,
                    new InstaHashtagSearchDataConverter());
                tags = InstaConvertersFabric.Instance.GetHashTagsSearchConverter(tagsResponse).Convert();

                if (tags.Any() && excludeList != null && excludeList.Contains(tags.First().Id))
                {
                    tags.RemoveAt(0);
                }

                if (!tags.Any())
                {
                    tags = new HashtagSearch();
                }

                return(Result.Success(tags));
            }
            catch (HttpRequestException httpException)
            {
                logger?.LogError(httpException, "Error");
                return(Result.Fail(httpException, default(HashtagSearch), ResponseType.NetworkProblem));
            }
            catch (Exception exception)
            {
                logger?.LogError(exception, "Error");
                return(Result.Fail(exception, tags));
            }
        }