1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Added HTTPAuthDecorator

This commit is contained in:
shin- 2013-10-22 20:48:29 +02:00
parent 3ed0ff85f5
commit bbf9135adc

View file

@ -107,6 +107,23 @@ func (h *HTTPMetaHeadersDecorator) ChangeRequest(req *http.Request) (newReq *htt
return req, nil return req, nil
} }
type HTTPAuthDecorator struct {
login string
password string
}
func NewHTTPAuthDecorator(login, password string) HTTPRequestDecorator {
ret := new(HTTPAuthDecorator)
ret.login = login
ret.password = password
return ret
}
func (self *HTTPAuthDecorator) ChangeRequest(req *http.Request) (*http.Request, error) {
req.SetBasicAuth(self.login, self.password)
return req, nil
}
// HTTPRequestFactory creates an HTTP request // HTTPRequestFactory creates an HTTP request
// and applies a list of decorators on the request. // and applies a list of decorators on the request.
type HTTPRequestFactory struct { type HTTPRequestFactory struct {
@ -119,6 +136,10 @@ func NewHTTPRequestFactory(d ...HTTPRequestDecorator) *HTTPRequestFactory {
} }
} }
func (self *HTTPRequestFactory) AddDecorator(d... HTTPRequestDecorator) {
self.decorators = append(self.decorators, d...)
}
// NewRequest() creates a new *http.Request, // NewRequest() creates a new *http.Request,
// applies all decorators in the HTTPRequestFactory on the request, // applies all decorators in the HTTPRequestFactory on the request,
// then applies decorators provided by d on the request. // then applies decorators provided by d on the request.
@ -144,5 +165,6 @@ func (h *HTTPRequestFactory) NewRequest(method, urlStr string, body io.Reader, d
return nil, err return nil, err
} }
} }
Debugf("%v -- HEADERS: %v", req.URL, req.Header)
return req, err return req, err
} }