Skip to content

Commit

Permalink
httpclient: Reuse existing configured logger (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Sep 4, 2024
1 parent 1aa74af commit 8d6f663
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions internal/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package httpclient

import (
"fmt"
"log"
"net/http"

"github.com/hashicorp/go-retryablehttp"
Expand All @@ -13,8 +14,10 @@ import (

// NewHTTPClient provides a pre-configured http.Client
// e.g. with relevant User-Agent header
func NewHTTPClient() *http.Client {
client := retryablehttp.NewClient().StandardClient()
func NewHTTPClient(logger *log.Logger) *http.Client {
rc := retryablehttp.NewClient()
rc.Logger = logger
client := rc.StandardClient()
client.Transport = &userAgentRoundTripper{
userAgent: fmt.Sprintf("hc-install/%s", version.Version()),
inner: client.Transport,
Expand Down
2 changes: 1 addition & 1 deletion internal/releasesjson/checksum_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (cd *ChecksumDownloader) DownloadAndVerifyChecksums(ctx context.Context) (C
return nil, err
}

client := httpclient.NewHTTPClient()
client := httpclient.NewHTTPClient(cd.Logger)
sigURL := fmt.Sprintf("%s/%s/%s/%s", cd.BaseURL,
url.PathEscape(cd.ProductVersion.Name),
url.PathEscape(cd.ProductVersion.Version.String()),
Expand Down
2 changes: 1 addition & 1 deletion internal/releasesjson/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d *Downloader) DownloadAndUnpack(ctx context.Context, pv *ProductVersion,
}
}

client := httpclient.NewHTTPClient()
client := httpclient.NewHTTPClient(d.Logger)

archiveURL, err := determineArchiveURL(pb.URL, d.BaseURL)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/releasesjson/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *Releases) SetLogger(logger *log.Logger) {
}

func (r *Releases) ListProductVersions(ctx context.Context, productName string) (ProductVersionsMap, error) {
client := httpclient.NewHTTPClient()
client := httpclient.NewHTTPClient(r.logger)

productIndexURL := fmt.Sprintf("%s/%s/index.json",
r.BaseURL,
Expand Down Expand Up @@ -122,7 +122,7 @@ func (r *Releases) ListProductVersions(ctx context.Context, productName string)
}

func (r *Releases) GetProductVersion(ctx context.Context, product string, version *version.Version) (*ProductVersion, error) {
client := httpclient.NewHTTPClient()
client := httpclient.NewHTTPClient(r.logger)

indexURL := fmt.Sprintf("%s/%s/%s/index.json",
r.BaseURL,
Expand Down

0 comments on commit 8d6f663

Please sign in to comment.