From fbaba66e124441d0e283fd4e2966d7af790ce23a Mon Sep 17 00:00:00 2001 From: Igor Drozdov Date: Tue, 23 Apr 2019 14:07:38 +0300 Subject: Move http client building to config --- go/internal/gitlabnet/httpclient.go | 69 ------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 go/internal/gitlabnet/httpclient.go (limited to 'go/internal/gitlabnet/httpclient.go') diff --git a/go/internal/gitlabnet/httpclient.go b/go/internal/gitlabnet/httpclient.go deleted file mode 100644 index de40051..0000000 --- a/go/internal/gitlabnet/httpclient.go +++ /dev/null @@ -1,69 +0,0 @@ -package gitlabnet - -import ( - "context" - "net" - "net/http" - "strings" - - "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" -) - -const ( - socketBaseUrl = "http://unix" - UnixSocketProtocol = "http+unix://" - HttpProtocol = "http://" -) - -type GitlabHttpClient struct { - httpClient *http.Client - config *config.Config - host string -} - -func buildSocketClient(config *config.Config) *GitlabHttpClient { - path := strings.TrimPrefix(config.GitlabUrl, UnixSocketProtocol) - transport := &http.Transport{ - DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { - dialer := net.Dialer{} - return dialer.DialContext(ctx, "unix", path) - }, - } - - return buildClient(config, transport, socketBaseUrl) -} - -func buildHttpClient(config *config.Config) *GitlabHttpClient { - return buildClient(config, &http.Transport{}, config.GitlabUrl) -} - -func buildClient(config *config.Config, transport *http.Transport, host string) *GitlabHttpClient { - httpClient := &http.Client{ - Transport: transport, - Timeout: config.HttpSettings.ReadTimeout(), - } - - return &GitlabHttpClient{httpClient: httpClient, config: config, host: host} -} - -func (c *GitlabHttpClient) Get(path string) (*http.Response, error) { - return c.doRequest("GET", path, nil) -} - -func (c *GitlabHttpClient) Post(path string, data interface{}) (*http.Response, error) { - return c.doRequest("POST", path, data) -} - -func (c *GitlabHttpClient) doRequest(method, path string, data interface{}) (*http.Response, error) { - request, err := newRequest(method, c.host, path, data) - if err != nil { - return nil, err - } - - user, password := c.config.HttpSettings.User, c.config.HttpSettings.Password - if user != "" && password != "" { - request.SetBasicAuth(user, password) - } - - return doRequest(c.httpClient, c.config, request) -} -- cgit v1.2.1