コード例 #1
0
        public async Task <Option <IEnumerable <Repository>, AllegroApiException> > GetUserRepositoriesAsync(string username)
        {
            string query = _embeddedResourceService.GetResource("Me.Bartecki.Allegro.Infrastructure.Integrations.RepositoryStores.GitHub.GitHubQuery.graphql");

            try
            {
                bool   isNextPageAvailable = false;
                string nextPageId          = null;

                List <GitHubRepository> repositories = new List <GitHubRepository>();
                do
                {
                    var request = new GraphQLHttpRequest(query,
                                                         new
                    {
                        username     = username,
                        nextCursorId = nextPageId
                    });
                    var response = await _client.SendQueryAsync <RootResponse>(request);

                    if (response.Errors?.Any() == true)
                    {
                        return(GetException(response));
                    }

                    repositories.AddRange(response.Data.User.Repositories.Nodes);

                    //Handle pagination
                    var pageInfo = response.Data.User.Repositories.PageInfo;
                    isNextPageAvailable = pageInfo.HasNextPage;
                    if (isNextPageAvailable)
                    {
                        nextPageId = pageInfo.EndCursor;
                    }
                } while (isNextPageAvailable);

                return(Option.Some <IEnumerable <Repository>, AllegroApiException>(repositories.Select(Convert)));
            }
            catch (GraphQLHttpException httpRequestException) when(
                httpRequestException.HttpResponseMessage.StatusCode == HttpStatusCode.Unauthorized)
            {
                //Rethrow this exception, critical misconfiguration.
                throw;
            }
            catch (GraphQLHttpException httpRequestException)
            {
                var errorCode    = ErrorCodes.RepositorySource_UnableToReach;
                var apiException = new AllegroApiException(
                    errorCode,
                    httpRequestException.Message,
                    httpRequestException);

                return(Option.None <IEnumerable <Repository>, AllegroApiException>(apiException));
            }
        }