move http with proxy call into a helper function

This commit is contained in:
Megan Marsh 2019-01-23 16:45:07 -08:00
parent 904c4b9adb
commit c63b54a1e7
5 changed files with 23 additions and 21 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
commonhelper "github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
@ -28,11 +29,7 @@ func (s *stepSetISO) Run(_ context.Context, state multistep.StateBag) multistep.
req.Header.Set("User-Agent", "Packer")
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
httpClient := commonhelper.HttpClientWithEnvironmentProxy()
res, err := httpClient.Do(req)
if err == nil && (res.StatusCode >= 200 && res.StatusCode < 300) {

View File

@ -20,6 +20,7 @@ import (
"runtime"
"strings"
commonhelper "github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/packer"
)
@ -257,11 +258,7 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
req.Header.Set("User-Agent", d.userAgent)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
httpClient := commonhelper.HttpClientWithEnvironmentProxy()
resp, err := httpClient.Do(req)
if err != nil || resp == nil {

View File

@ -5,7 +5,6 @@ import (
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
@ -13,6 +12,7 @@ import (
"strings"
"testing"
commonhelper "github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/packer"
)
@ -258,11 +258,7 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) {
t.Fatal(err)
}
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
httpClient := commonhelper.HttpClientWithEnvironmentProxy()
_, err = httpClient.Do(req)
if err != nil {

View File

@ -0,0 +1,14 @@
package common
import (
"net/http"
)
func HttpClientWithEnvironmentProxy() *http.Client {
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
return httpClient
}

View File

@ -9,6 +9,8 @@ import (
"net/http"
"os"
"strings"
commonhelper "github.com/hashicorp/packer/helper/common"
)
type VagrantCloudClient struct {
@ -37,11 +39,7 @@ func (v VagrantCloudErrors) FormatErrors() string {
func (v VagrantCloudClient) New(baseUrl string, token string) (*VagrantCloudClient, error) {
c := &VagrantCloudClient{
client: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
},
client: commonhelper.HttpClientWithEnvironmentProxy(),
BaseURL: baseUrl,
AccessToken: token,
}