/// <summary>
        ///     Get user explore feed (Explore tab info) asynchronously
        /// </summary>
        /// <param name="paginationParameters">Pagination parameters: next id and max amount of pages to load</param>
        /// <returns><see cref="InstaExploreFeed" /></returns>
        public async Task <IResult <InstaExploreFeed> > GetExploreFeedAsync(PaginationParameters paginationParameters)
        {
            UserAuthValidator.Validate(_userAuthValidate);
            var exploreFeed = new InstaExploreFeed();

            try
            {
                var exploreUri = UriCreator.GetExploreUri(paginationParameters.NextId);
                var request    = HttpHelper.GetDefaultRequest(HttpMethod.Get, exploreUri, _deviceInfo);
                var response   = await _httpRequestProcessor.SendAsync(request);

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

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(Result.UnExpectedResponse <InstaExploreFeed>(response, json));
                }
                var feedResponse = JsonConvert.DeserializeObject <InstaExploreFeedResponse>(json,
                                                                                            new InstaExploreFeedDataConverter());
                exploreFeed = ConvertersFabric.Instance.GetExploreFeedConverter(feedResponse).Convert();
                var nextId = feedResponse.Items.Medias.LastOrDefault(media => !string.IsNullOrEmpty(media.NextMaxId))
                             ?.NextMaxId;
                exploreFeed.Medias.PageSize = feedResponse.ResultsCount;
                paginationParameters.NextId = nextId;
                exploreFeed.NextId          = nextId;
                while (feedResponse.MoreAvailable &&
                       !string.IsNullOrEmpty(paginationParameters.NextId) &&
                       paginationParameters.PagesLoaded < paginationParameters.MaximumPagesToLoad)
                {
                    var nextFeed = await GetExploreFeedAsync(paginationParameters);

                    if (!nextFeed.Succeeded)
                    {
                        return(Result.Fail(nextFeed.Info, exploreFeed));
                    }
                    nextId = feedResponse.Items.Medias.LastOrDefault(media => !string.IsNullOrEmpty(media.NextMaxId))
                             ?.NextMaxId;
                    exploreFeed.NextId = paginationParameters.NextId = nextId;
                    paginationParameters.PagesLoaded++;
                    exploreFeed.Medias.AddRange(nextFeed.Value.Medias);
                }

                exploreFeed.Medias.Pages = paginationParameters.PagesLoaded;
                return(Result.Success(exploreFeed));
            }
            catch (Exception exception)
            {
                _logger?.LogException(exception);
                return(Result.Fail(exception, exploreFeed));
            }
        }
Example #2
0
        /// <summary>
        ///     Get user explore feed (Explore tab info) asynchronously
        /// </summary>
        /// <param name="paginationParameters">Pagination parameters: next id and max amount of pages to load</param>
        /// <returns><see cref="InstaExploreFeed" /></returns>
        public async Task <IResult <InstaExploreFeed> > GetExploreFeedAsync(PaginationParameters paginationParameters)
        {
            UserAuthValidator.Validate(_userAuthValidate);
            var exploreFeed = new InstaExploreFeed();

            try
            {
                InstaExploreFeed Convert(InstaExploreFeedResponse exploreFeedResponse)
                {
                    return(ConvertersFabric.Instance.GetExploreFeedConverter(exploreFeedResponse).Convert());
                }
                var feeds = await GetExploreFeed(paginationParameters);

                if (!feeds.Succeeded)
                {
                    if (feeds.Value != null)
                    {
                        return(Result.Fail(feeds.Info, Convert(feeds.Value)));
                    }
                    else
                    {
                        return(Result.Fail(feeds.Info, (InstaExploreFeed)null));
                    }
                }
                var feedResponse = feeds.Value;
                paginationParameters.NextMaxId = feedResponse.MaxId;
                paginationParameters.RankToken = feedResponse.RankToken;

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

                    if (!nextFeed.Succeeded)
                    {
                        return(Result.Fail(nextFeed.Info, Convert(feeds.Value)));
                    }

                    feedResponse.NextMaxId           = paginationParameters.NextMaxId = nextFeed.Value.MaxId;
                    feedResponse.RankToken           = paginationParameters.RankToken = nextFeed.Value.RankToken;
                    feedResponse.MoreAvailable       = nextFeed.Value.MoreAvailable;
                    feedResponse.AutoLoadMoreEnabled = nextFeed.Value.AutoLoadMoreEnabled;
                    feedResponse.NextMaxId           = nextFeed.Value.NextMaxId;
                    feedResponse.ResultsCount        = nextFeed.Value.ResultsCount;
                    feedResponse.Items.Channel       = nextFeed.Value.Items.Channel;
                    feedResponse.Items.Medias.AddRange(nextFeed.Value.Items.Medias);
                    if (nextFeed.Value.Items.StoryTray == null)
                    {
                        feedResponse.Items.StoryTray = nextFeed.Value.Items.StoryTray;
                    }
                    else
                    {
                        feedResponse.Items.StoryTray.Id         = nextFeed.Value.Items.StoryTray.Id;
                        feedResponse.Items.StoryTray.IsPortrait = nextFeed.Value.Items.StoryTray.IsPortrait;

                        feedResponse.Items.StoryTray.Tray.AddRange(nextFeed.Value.Items.StoryTray.Tray);
                        if (nextFeed.Value.Items.StoryTray.TopLive == null)
                        {
                            feedResponse.Items.StoryTray.TopLive = nextFeed.Value.Items.StoryTray.TopLive;
                        }
                        else
                        {
                            feedResponse.Items.StoryTray.TopLive.RankedPosition = nextFeed.Value.Items.StoryTray.TopLive.RankedPosition;
                            feedResponse.Items.StoryTray.TopLive.BroadcastOwners.AddRange(nextFeed.Value.Items.StoryTray.TopLive.BroadcastOwners);
                        }
                    }

                    paginationParameters.PagesLoaded++;
                }
                exploreFeed = Convert(feedResponse);
                exploreFeed.Medias.Pages    = paginationParameters.PagesLoaded;
                exploreFeed.Medias.PageSize = feedResponse.ResultsCount;
                return(Result.Success(exploreFeed));
            }
            catch (HttpRequestException httpException)
            {
                _logger?.LogException(httpException);
                return(Result.Fail(httpException, default(InstaExploreFeed), ResponseType.NetworkProblem));
            }
            catch (Exception exception)
            {
                _logger?.LogException(exception);
                return(Result.Fail(exception, exploreFeed));
            }
        }