Exemplo n.º 1
0
        public async Task <Option <IEnumerable <Repository>, RepoCounterApiException> > GetUserRepositoriesAsync(string username)
        {
            string query = _embeddedResourceService.GetResource("Me.Bartecki.RepoCounter.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>, RepoCounterApiException>(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 RepoCounterApiException(
                    errorCode,
                    httpRequestException.Message,
                    httpRequestException);

                return(Option.None <IEnumerable <Repository>, RepoCounterApiException>(apiException));
            }
        }
Exemplo n.º 2
0
        public ObjectResult GetUserFriendlyError(RepoCounterApiException exception)
        {
            //TODO: Log the exception
            //Alternatively when isInDevelopment then we could actually throw this exception.

            var errorCode      = exception.ErrorCode;
            var httpStatusCode = (int)GetHttpStatusCode(errorCode);
            var message        = new ErrorMessage()
            {
                ErrorCode = errorCode,
                Message   = _isInDevelopment ?
                            exception.ToString() :
                            $"{errorCode.ToString()}: {exception.Message}"
            };

            return(new ObjectResult(message)
            {
                StatusCode = httpStatusCode
            });
        }