From 4efa483bfe2580f146b1b1049cecf2c46174766b Mon Sep 17 00:00:00 2001 From: vlastahajek Date: Wed, 12 Oct 2022 16:46:36 +0200 Subject: [PATCH] feat: adding example of overwriting User-Agent header --- example_ua_set_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 example_ua_set_test.go diff --git a/example_ua_set_test.go b/example_ua_set_test.go new file mode 100644 index 00000000..b33e5838 --- /dev/null +++ b/example_ua_set_test.go @@ -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") + } +}