Example #1
0
        /// <summary>
        ///     Get tag feed by tag value asynchronously
        /// </summary>
        /// <param name="tag">Tag value</param>
        /// <param name="paginationParameters">Pagination parameters: next id and max amount of pages to load</param>
        /// <returns>
        ///     <see cref="InstaTagFeed" />
        /// </returns>
        public async Task <IResult <InstaTagFeed> > GetTagFeedAsync(string tag, PaginationParameters paginationParameters)
        {
            UserAuthValidator.Validate(_userAuthValidate);
            var tagFeed = new InstaTagFeed();

            try
            {
                var userFeedUri = UriCreator.GetTagFeedUri(tag, paginationParameters.NextMaxId);
                var request     = _httpHelper.GetDefaultRequest(HttpMethod.Get, userFeedUri, _deviceInfo);
                var response    = await _httpRequestProcessor.SendAsync(request);

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

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(Result.UnExpectedResponse <InstaTagFeed>(response, json));
                }
                var feedResponse = JsonConvert.DeserializeObject <InstaTagFeedResponse>(json,
                                                                                        new InstaTagFeedDataConverter());
                tagFeed = ConvertersFabric.Instance.GetTagFeedConverter(feedResponse).Convert();

                paginationParameters.NextMaxId = feedResponse.NextMaxId;
                paginationParameters.PagesLoaded++;

                while (feedResponse.MoreAvailable &&
                       !string.IsNullOrEmpty(paginationParameters.NextMaxId) &&
                       paginationParameters.PagesLoaded < paginationParameters.MaximumPagesToLoad)
                {
                    var nextFeed = await GetTagFeedAsync(tag, paginationParameters);

                    if (!nextFeed.Succeeded)
                    {
                        return(Result.Fail(nextFeed.Info, tagFeed));
                    }
                    tagFeed.NextMaxId = paginationParameters.NextMaxId = nextFeed.Value.NextMaxId;
                    tagFeed.Medias.AddRange(nextFeed.Value.Medias);
                    tagFeed.Stories.AddRange(nextFeed.Value.Stories);
                }

                return(Result.Success(tagFeed));
            }
            catch (HttpRequestException httpException)
            {
                _logger?.LogException(httpException);
                return(Result.Fail(httpException, default(InstaTagFeed), ResponseType.NetworkProblem));
            }
            catch (Exception exception)
            {
                _logger?.LogException(exception);
                return(Result.Fail(exception, tagFeed));
            }
        }
Example #2
0
        /// <summary>
        ///     Get tag feed by tag value asynchronously
        /// </summary>
        /// <param name="tag">Tag value</param>
        /// <param name="paginationParameters">Pagination parameters: next id and max amount of pages to load</param>
        /// <returns>
        ///     <see cref="InstaTagFeed" />
        /// </returns>
        public async Task <IResult <InstaTagFeed> > GetTagFeedAsync(string tag, PaginationParameters paginationParameters)
        {
            UserAuthValidator.Validate(_userAuthValidate);
            var tagFeed = new InstaTagFeed();

            try
            {
                if (paginationParameters == null)
                {
                    paginationParameters = PaginationParameters.MaxPagesToLoad(1);
                }

                InstaTagFeed Convert(InstaTagFeedResponse instaTagFeedResponse)
                {
                    return(ConvertersFabric.Instance.GetTagFeedConverter(instaTagFeedResponse).Convert());
                }

                var tags = await GetTagFeed(tag, paginationParameters);

                if (!tags.Succeeded)
                {
                    if (tags.Value != null)
                    {
                        return(Result.Fail(tags.Info, Convert(tags.Value)));
                    }
                    else
                    {
                        return(Result.Fail(tags.Info, default(InstaTagFeed)));
                    }
                }
                var feedResponse = tags.Value;

                tagFeed = Convert(feedResponse);

                paginationParameters.NextMaxId = feedResponse.NextMaxId;
                paginationParameters.PagesLoaded++;

                while (feedResponse.MoreAvailable &&
                       !string.IsNullOrEmpty(paginationParameters.NextMaxId) &&
                       paginationParameters.PagesLoaded < paginationParameters.MaximumPagesToLoad)
                {
                    var nextFeed = await GetTagFeed(tag, paginationParameters);

                    if (!nextFeed.Succeeded)
                    {
                        return(Result.Fail(nextFeed.Info, tagFeed));
                    }

                    var convertedFeeds = Convert(nextFeed.Value);
                    tagFeed.NextMaxId = paginationParameters.NextMaxId = nextFeed.Value.NextMaxId;
                    tagFeed.Medias.AddRange(convertedFeeds.Medias);
                    tagFeed.Stories.AddRange(convertedFeeds.Stories);
                    feedResponse.MoreAvailable = nextFeed.Value.MoreAvailable;
                    paginationParameters.PagesLoaded++;
                }
                return(Result.Success(tagFeed));
            }
            catch (HttpRequestException httpException)
            {
                _logger?.LogException(httpException);
                return(Result.Fail(httpException, default(InstaTagFeed), ResponseType.NetworkProblem));
            }
            catch (Exception exception)
            {
                _logger?.LogException(exception);
                return(Result.Fail(exception, tagFeed));
            }
        }