| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package useragent includes constants and utilitiesfor setting the User-Agent |
| 16 |
// for Go CDK connections to GCP. |
| 17 |
package useragent // import "gocloud.dev/internal/useragent" |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"maps" |
| 22 |
"net/http" |
| 23 |
|
| 24 |
"google.golang.org/api/option" |
| 25 |
"google.golang.org/grpc" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
prefix = "go-cloud" |
| 30 |
version = "0.45.0" |
| 31 |
) |
| 32 |
|
| 33 |
// ClientOption returns an option.ClientOption that sets a Go CDK User-Agent. |
| 34 |
func ClientOption(api string) option.ClientOption { |
| 35 |
return option.WithUserAgent(userAgentString(api)) |
| 36 |
} |
| 37 |
|
| 38 |
// GRPCDialOption returns a grpc.DialOption that sets a Go CDK User-Agent. |
| 39 |
func GRPCDialOption(api string) grpc.DialOption { |
| 40 |
return grpc.WithUserAgent(userAgentString(api)) |
| 41 |
} |
| 42 |
|
| 43 |
// AzureUserAgentPrefix returns a prefix that is used to set Azure SDK User-Agent to help with diagnostics. |
| 44 |
func AzureUserAgentPrefix(api string) string { |
| 45 |
return userAgentString(api) |
| 46 |
} |
| 47 |
|
| 48 |
func userAgentString(api string) string { |
| 49 |
return fmt.Sprintf("%s/%s/%s", prefix, api, version) |
| 50 |
} |
| 51 |
|
| 52 |
// userAgentTransport wraps an http.RoundTripper, adding a User-Agent header |
| 53 |
// to each request. |
| 54 |
type userAgentTransport struct { |
| 55 |
base http.RoundTripper |
| 56 |
api string |
| 57 |
} |
| 58 |
|
| 59 |
func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 60 |
// Clone the request to avoid mutating it. |
| 61 |
newReq := *req |
| 62 |
newReq.Header = make(http.Header) |
| 63 |
maps.Copy(newReq.Header, req.Header) |
| 64 |
// Append to the User-Agent string to preserve other information. |
| 65 |
newReq.Header.Set("User-Agent", req.UserAgent()+" "+userAgentString(t.api)) |
| 66 |
return t.base.RoundTrip(&newReq) |
| 67 |
} |
| 68 |
|
| 69 |
// HTTPClient wraps client and appends a Go CDK string to the User-Agent |
| 70 |
// header for all requests. |
| 71 |
func HTTPClient(client *http.Client, api string) *http.Client { |
| 72 |
c := *client |
| 73 |
c.Transport = &userAgentTransport{base: c.Transport, api: api} |
| 74 |
return &c |
| 75 |
} |