diff options
author | ferhat elmas <elmas.ferhat@gmail.com> | 2016-11-15 01:18:53 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-15 01:18:53 +0100 |
commit | 16d86605732ba3198c0acd4317b53cf4991a7d4d (patch) | |
tree | 3306f0438235f7dfe19fd37c5393a4794abe0535 /plumbing/client/http/common.go | |
parent | eb89d2dd9a36440d58aea224c055b364e49785f7 (diff) | |
download | go-git-16d86605732ba3198c0acd4317b53cf4991a7d4d.tar.gz |
Add configurable http client factory (fixes #120) (#121)
* new http client factory ready to install/override default http(s)
* mv GitUploadPackServiceFactory to clients.common pkg
* rename http.HTTPError to http.Err
* rename http.HTTPAuthMethod to http.AuthMethod
* add doc and examples/ usage
* general improvements:
- update install link in readme to v4 (example are already pointing v4)
- fix indentation in package doc (styling for godoc.org)
- use http.Status constants instead of integers
- close leaked response body
- rm named returns which stutter in doc
- fix one format string
- rm unnecessary if checks
- documentation fixes
Diffstat (limited to 'plumbing/client/http/common.go')
-rw-r--r-- | plumbing/client/http/common.go | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/plumbing/client/http/common.go b/plumbing/client/http/common.go index 4c07876..2447995 100644 --- a/plumbing/client/http/common.go +++ b/plumbing/client/http/common.go @@ -9,8 +9,8 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/client/common" ) -// HTTPAuthMethod concrete implementation of common.AuthMethod for HTTP services -type HTTPAuthMethod interface { +// AuthMethod is concrete implementation of common.AuthMethod for HTTP services +type AuthMethod interface { common.AuthMethod setAuth(r *http.Request) } @@ -29,7 +29,7 @@ func (a *BasicAuth) setAuth(r *http.Request) { r.SetBasicAuth(a.username, a.password) } -// Name name of the auth +// Name is name of the auth func (a *BasicAuth) Name() string { return "http-basic-auth" } @@ -43,34 +43,33 @@ func (a *BasicAuth) String() string { return fmt.Sprintf("%s - %s:%s", a.Name(), a.username, masked) } -// HTTPError a dedicated error to return errors bases on status codes -type HTTPError struct { +// Err is a dedicated error to return errors based on status code +type Err struct { Response *http.Response } -// NewHTTPError returns a new HTTPError based on a http response -func NewHTTPError(r *http.Response) error { - if r.StatusCode >= 200 && r.StatusCode < 300 { +// NewErr returns a new Err based on a http response +func NewErr(r *http.Response) error { + if r.StatusCode >= http.StatusOK && r.StatusCode < http.StatusMultipleChoices { return nil } switch r.StatusCode { - case 401: + case http.StatusUnauthorized: return common.ErrAuthorizationRequired - case 404: + case http.StatusNotFound: return common.ErrRepositoryNotFound } - err := &HTTPError{r} - return plumbing.NewUnexpectedError(err) + return plumbing.NewUnexpectedError(&Err{r}) } // StatusCode returns the status code of the response -func (e *HTTPError) StatusCode() int { +func (e *Err) StatusCode() int { return e.Response.StatusCode } -func (e *HTTPError) Error() string { +func (e *Err) Error() string { return fmt.Sprintf("unexpected requesting %q status code: %d", e.Response.Request.URL, e.Response.StatusCode, ) |