summaryrefslogtreecommitdiffstats
path: root/client/go/util/http.go
blob: ffdd3d1599bd3ec05dd51ed497542f6fa9a4aa65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package util

import (
	"crypto/tls"
	"fmt"
	"net/http"
	"time"

	"github.com/vespa-engine/vespa/client/go/build"
)

type HTTPClient interface {
	Do(request *http.Request, timeout time.Duration) (response *http.Response, error error)
	UseCertificate(certificate []tls.Certificate)
}

type defaultHTTPClient struct {
	client *http.Client
}

func (c *defaultHTTPClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) {
	if c.client.Timeout != timeout { // Set wanted timeout
		c.client.Timeout = timeout
	}
	if request.Header == nil {
		request.Header = make(http.Header)
	}
	request.Header.Set("User-Agent", fmt.Sprintf("Vespa CLI/%s", build.Version))
	return c.client.Do(request)
}

func (c *defaultHTTPClient) UseCertificate(certificates []tls.Certificate) {
	c.client.Transport = &http.Transport{TLSClientConfig: &tls.Config{
		Certificates: certificates,
	}}
}

func CreateClient(timeout time.Duration) HTTPClient {
	return &defaultHTTPClient{client: &http.Client{Timeout: timeout}}
}