コード例 #1
0
        public virtual IGithubResponse GetResponse()
        {
            var uri = string.Format("{0}/{1}",
                GithubURLs.GithubApiBaseUrl,
                Path);

            uri = AddPagingToUri(uri);

            uri = AuthProvider.PrepareUri(uri);

            if (IsCached(uri))
            {
                LogProvider.LogMessage("Returning cached result for {0}", uri);
                return GetFromCache(uri) as GithubResponse;
            }

            //Hack for mono
            System.Net.ServicePointManager.ServerCertificateValidationCallback = Validator;

            var webRequest = System.Net.WebRequest.Create(new Uri(uri)) as System.Net.HttpWebRequest;

            webRequest.Method = EnumHelper.ToString(Method);

            var authResult = AuthProvider.PreRequestAuth(this, webRequest);

            if (!authResult.Success)
            {
                var authError = new GithubAuthenticationException(uri);
                if (LogProvider.HandleAndReturnIfToThrowError(authError))
                {
                    throw authError;
                }
                return new GithubFailedResponse(uri);
            }

            webRequest = PrepareWebRequest(authResult.WebRequest);


            try
            {
                var response = webRequest.GetResponse() as System.Net.HttpWebResponse;
                var responseToReturn = GetGithubResponseFromWebResponse(response);
                Cache(responseToReturn, uri);
                return responseToReturn;

            }
            catch (Exception error)
            {
                if (error is System.Net.WebException)
                {
                    //Might be a valid response with 404 etc
                    var webError = error as System.Net.WebException;
                    var webErrorResponse = webError.Response as System.Net.HttpWebResponse;
                    if (webErrorResponse != null)
                    {
                        if (IsGithubResponse(webErrorResponse))
                        {
                            var responseEvenWithError = GetGithubResponseFromWebResponse(webErrorResponse);
                            Cache(responseEvenWithError, uri);
                            return responseEvenWithError;
                        }
                        var githubException = new GithubException(webErrorResponse, uri);
                        if (LogProvider.HandleAndReturnIfToThrowError(githubException))
                            throw githubException;
                    }
                }
                if (LogProvider.HandleAndReturnIfToThrowError(error))
                    throw;
                return new GithubFailedResponse(uri);
            }
        }
コード例 #2
0
ファイル: GithubRequest.cs プロジェクト: stemarie/GithubSharp
        public virtual IGithubResponse GetResponse()
        {
            var uri = string.Format("{0}/{1}",
                                    GithubURLs.GithubApiBaseUrl,
                                    Path);

            uri = AddPagingToUri(uri);

            uri = AuthProvider.PrepareUri(uri);

            if (IsCached(uri))
            {
                LogProvider.LogMessage("Returning cached result for {0}", uri);
                return(GetFromCache(uri) as GithubResponse);
            }

            //Hack for mono
            System.Net.ServicePointManager.ServerCertificateValidationCallback = Validator;

            var webRequest = System.Net.WebRequest.Create(new Uri(uri)) as System.Net.HttpWebRequest;

            webRequest.Method = EnumHelper.ToString(Method);

            var authResult = AuthProvider.PreRequestAuth(this, webRequest);

            if (!authResult.Success)
            {
                var authError = new GithubAuthenticationException(uri);
                if (LogProvider.HandleAndReturnIfToThrowError(authError))
                {
                    throw authError;
                }
                return(new GithubFailedResponse(uri));
            }

            webRequest = PrepareWebRequest(authResult.WebRequest);


            try
            {
                var response         = webRequest.GetResponse() as System.Net.HttpWebResponse;
                var responseToReturn = GetGithubResponseFromWebResponse(response);
                Cache(responseToReturn, uri);
                return(responseToReturn);
            }
            catch (Exception error)
            {
                if (error is System.Net.WebException)
                {
                    //Might be a valid response with 404 etc
                    var webError         = error as System.Net.WebException;
                    var webErrorResponse = webError.Response as System.Net.HttpWebResponse;
                    if (webErrorResponse != null)
                    {
                        if (IsGithubResponse(webErrorResponse))
                        {
                            var responseEvenWithError = GetGithubResponseFromWebResponse(webErrorResponse);
                            Cache(responseEvenWithError, uri);
                            return(responseEvenWithError);
                        }
                        var githubException = new GithubException(webErrorResponse, uri);
                        if (LogProvider.HandleAndReturnIfToThrowError(githubException))
                        {
                            throw githubException;
                        }
                    }
                }
                if (LogProvider.HandleAndReturnIfToThrowError(error))
                {
                    throw;
                }
                return(new GithubFailedResponse(uri));
            }
        }