コード例 #1
0
        private async Task <IResult <InstaActivityFeed> > GetRecentActivityInternalAsync(
            Uri uri,
            PaginationParameters paginationParameters)
        {
            try
            {
                var request  = httpHelper.GetDefaultRequest(HttpMethod.Get, uri, deviceInfo);
                var response = await httpRequestProcessor.SendAsync(request, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);

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

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

                var feedPage = JsonConvert.DeserializeObject <InstaRecentActivityResponse>(
                    json,
                    new InstaRecentActivityConverter());
                activityFeed.IsOwnActivity = feedPage.IsOwnActivity;
                var nextId = feedPage.NextMaxId;
                activityFeed.Items.AddRange(
                    feedPage.Stories.Select(InstaConvertersFabric.Instance.GetSingleRecentActivityConverter)
                    .Select(converter => converter.Convert()));
                paginationParameters.PagesLoaded++;
                activityFeed.NextMaxId = paginationParameters.NextMaxId = feedPage.NextMaxId;
                while (!string.IsNullOrEmpty(nextId) &&
                       paginationParameters.PagesLoaded < paginationParameters.MaximumPagesToLoad)
                {
                    var nextFollowingFeed = await GetFollowingActivityWithMaxIdAsync(nextId).ConfigureAwait(false);

                    if (!nextFollowingFeed.Succeeded)
                    {
                        return(Result.Fail(nextFollowingFeed.Info, activityFeed));
                    }

                    nextId = nextFollowingFeed.Value.NextMaxId;
                    activityFeed.Items.AddRange(
                        feedPage.Stories.Select(InstaConvertersFabric.Instance.GetSingleRecentActivityConverter)
                        .Select(converter => converter.Convert()));
                    paginationParameters.PagesLoaded++;
                    activityFeed.NextMaxId = paginationParameters.NextMaxId = nextId;
                }

                return(Result.Success(activityFeed));
            }
            catch (HttpRequestException httpException)
            {
                logger?.LogError(httpException, "Error");
                return(Result.Fail(httpException, default(InstaActivityFeed), ResponseType.NetworkProblem));
            }
            catch (Exception exception)
            {
                logger?.LogError(exception, "Error");
                return(Result.Fail <InstaActivityFeed>(exception));
            }
        }
コード例 #2
0
        private async Task <IResult <InstaActivityFeed> > GetRecentActivityInternalAsync(Uri uri, int maxPages = 0)
        {
            var request  = HttpHelper.GetDefaultRequest(HttpMethod.Get, uri, _deviceInfo);
            var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);

            var activityFeed = new InstaActivityFeed();
            var json         = await response.Content.ReadAsStringAsync();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var feedPage = JsonConvert.DeserializeObject <InstaRecentActivityResponse>(json,
                                                                                           new InstaRecentActivityConverter());
                activityFeed.IsOwnActivity = feedPage.IsOwnActivity;
                var nextId = feedPage.NextMaxId;
                activityFeed.Items.AddRange(
                    feedPage.Stories.Select(ConvertersFabric.GetSingleRecentActivityConverter)
                    .Select(converter => converter.Convert()));
                var pages = 1;
                while (!string.IsNullOrEmpty(nextId) && pages < maxPages)
                {
                    var nextFollowingFeed = await GetFollowingActivityWithMaxIdAsync(nextId);

                    if (!nextFollowingFeed.Succeeded)
                    {
                        Result.Success($"Not all pages was downloaded: {nextFollowingFeed.Info.Message}", activityFeed);
                    }
                    nextId = nextFollowingFeed.Value.NextMaxId;
                    activityFeed.Items.AddRange(
                        feedPage.Stories.Select(ConvertersFabric.GetSingleRecentActivityConverter)
                        .Select(converter => converter.Convert()));
                    pages++;
                }
                return(Result.Success(activityFeed));
            }
            return(Result.Fail(GetBadStatusFromJsonString(json).Message, (InstaActivityFeed)null));
        }