1
0
mirror of https://github.com/mayflower/docker-ls.git synced 2025-11-26 12:03:12 +03:00
Files
docker-ls/lib/connector/basic_auth_connector.go
2016-02-26 13:46:27 +01:00

58 lines
1.2 KiB
Go

package connector
import (
"net/http"
"net/url"
"strings"
)
type basicAuthConnector struct {
cfg Config
httpClient *http.Client
semaphore semaphore
stat *statistics
}
func (r *basicAuthConnector) Delete(url *url.URL, hint string) (*http.Response, error) {
return r.Request("DELETE", url, hint)
}
func (r *basicAuthConnector) Get(url *url.URL, hint string) (*http.Response, error) {
return r.Request("GET", url, hint)
}
func (r *basicAuthConnector) GetStatistics() Statistics {
return r.stat
}
func (r *basicAuthConnector) Request(method string, url *url.URL, hint string) (response *http.Response, err error) {
r.semaphore.Lock()
defer r.semaphore.Unlock()
r.stat.Request()
request, err := http.NewRequest(method, url.String(), strings.NewReader(""))
if err != nil {
return
}
credentials := r.cfg.Credentials()
if credentials.Password() != "" || credentials.User() != "" {
request.SetBasicAuth(credentials.User(), credentials.Password())
}
response, err = r.httpClient.Do(request)
return
}
func NewBasicAuthConnector(cfg Config) Connector {
return &basicAuthConnector{
cfg: cfg,
httpClient: createHttpClient(cfg),
semaphore: newSemaphore(cfg.MaxConcurrentRequests()),
stat: new(statistics),
}
}