-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding example of overwriting User-Agent header
- Loading branch information
1 parent
4a444de
commit 4efa483
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 InfluxData, Inc. All rights reserved. | ||
// Use of this source code is governed by MIT | ||
// license that can be found in the LICENSE file. | ||
|
||
package influxdb2_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/influxdata/influxdb-client-go/v2" | ||
ihttp "github.com/influxdata/influxdb-client-go/v2/api/http" | ||
) | ||
|
||
// UserAgentSetter is the implementation of Doer interface for setting User-Agent header | ||
type UserAgentSetter struct { | ||
UserAgent string | ||
RequestDoer ihttp.Doer | ||
} | ||
|
||
// Do fulfills the Doer interface | ||
func (u *UserAgentSetter) Do(req *http.Request) (*http.Response, error) { | ||
// Set User-Agent header to request | ||
req.Header.Set("User-Agent", u.UserAgent) | ||
// Call original Doer to proceed with request | ||
return u.RequestDoer.Do(req) | ||
} | ||
|
||
func ExampleClient_customUserAgentHeader() { | ||
// Set custom Doer to HTTPOptions | ||
opts := influxdb2.DefaultOptions() | ||
opts.HTTPOptions().SetHTTPDoer(&UserAgentSetter{ | ||
UserAgent: "NetMonitor/1.1", | ||
RequestDoer: http.DefaultClient, | ||
}) | ||
|
||
//Create client with customized options | ||
client := influxdb2.NewClientWithOptions("http://localhost:8086", "my-token", opts) | ||
|
||
// Always close client at the end | ||
defer client.Close() | ||
|
||
// Issue a call with custom User-Agent header | ||
resp, err := client.Ping(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if resp { | ||
fmt.Println("Server is up") | ||
} else { | ||
fmt.Println("Server is down") | ||
} | ||
} |