Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(influx): add User-Agent header to the CLI http calls
Browse files Browse the repository at this point in the history
closes: #18336
jsteenb2 committed Jun 2, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 81efcea commit f44e345
Showing 2 changed files with 30 additions and 25 deletions.
13 changes: 12 additions & 1 deletion cmd/influx/main.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
@@ -51,7 +52,17 @@ func newHTTPClient() (*httpc.Client, error) {
return httpClient, nil
}

c, err := http.NewHTTPClient(flags.Host, flags.Token, flags.skipVerify)
userAgent := fmt.Sprintf(
"influx/%s (%s) Sha/%s Date/%s",
version, runtime.GOOS, commit, date,
)

c, err := http.NewHTTPClient(
flags.Host,
flags.Token,
flags.skipVerify,
httpc.WithUserAgentHeader(userAgent),
)
if err != nil {
return nil, err
}
42 changes: 18 additions & 24 deletions pkg/httpc/options.go
Original file line number Diff line number Diff line change
@@ -42,34 +42,26 @@ func WithAuth(fn func(r *http.Request)) ClientOptFn {

// WithAuthToken provides token auth for requests.
func WithAuthToken(token string) ClientOptFn {
return func(opts *clientOpt) error {
fn := func(r *http.Request) {
r.Header.Set("Authorization", "Token "+token)
}
return WithAuth(fn)(opts)
}
return WithAuth(func(r *http.Request) {
r.Header.Set("Authorization", "Token "+token)
})
}

// WithSessionCookie provides cookie auth for requests to mimic the browser.
// Typically, session is influxdb.Session.Key.
func WithSessionCookie(session string) ClientOptFn {
return func(opts *clientOpt) error {
fn := func(r *http.Request) {
r.AddCookie(&http.Cookie{
Name: "session",
Value: session,
})
}
return WithAuth(fn)(opts)
}
return WithAuth(func(r *http.Request) {
r.AddCookie(&http.Cookie{
Name: "session",
Value: session,
})
})
}

// WithContentType sets the content type that will be applied to the requests created
// by the Client.
func WithContentType(ct string) ClientOptFn {
return func(opt *clientOpt) error {
return WithHeader(headerContentType, ct)(opt)
}
return WithHeader(headerContentType, ct)
}

func withDoer(d doer) ClientOptFn {
@@ -91,6 +83,11 @@ func WithHeader(header, val string) ClientOptFn {
}
}

// WithUserAgentHeader sets the user agent for the http client requests.
func WithUserAgentHeader(userAgent string) ClientOptFn {
return WithHeader("User-Agent", userAgent)
}

// WithHTTPClient sets the raw http client on the httpc Client.
func WithHTTPClient(c *http.Client) ClientOptFn {
return func(opt *clientOpt) error {
@@ -136,12 +133,9 @@ func WithWriterFn(fn WriteCloserFn) ClientOptFn {

// WithWriterGZIP gzips the request body generated from this client.
func WithWriterGZIP() ClientOptFn {
return func(opt *clientOpt) error {
fn := func(w io.WriteCloser) (string, string, io.WriteCloser) {
return headerContentEncoding, "gzip", gzip.NewWriter(w)
}
return WithWriterFn(fn)(opt)
}
return WithWriterFn(func(w io.WriteCloser) (string, string, io.WriteCloser) {
return headerContentEncoding, "gzip", gzip.NewWriter(w)
})
}

func defaultHTTPClient(scheme string, insecure bool) *http.Client {

0 comments on commit f44e345

Please sign in to comment.