github.com/yorinasub17/go-cloud@v0.27.40/internal/useragent/useragent.go (about)

     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  	"net/http"
    22  
    23  	"google.golang.org/api/option"
    24  	"google.golang.org/grpc"
    25  )
    26  
    27  const (
    28  	prefix  = "go-cloud"
    29  	version = "0.1.0"
    30  )
    31  
    32  // ClientOption returns an option.ClientOption that sets a Go CDK User-Agent.
    33  func ClientOption(api string) option.ClientOption {
    34  	return option.WithUserAgent(userAgentString(api))
    35  }
    36  
    37  // GRPCDialOption returns a grpc.DialOption that sets a Go CDK User-Agent.
    38  func GRPCDialOption(api string) grpc.DialOption {
    39  	return grpc.WithUserAgent(userAgentString(api))
    40  }
    41  
    42  // AzureUserAgentPrefix returns a prefix that is used to set Azure SDK User-Agent to help with diagnostics.
    43  func AzureUserAgentPrefix(api string) string {
    44  	return userAgentString(api)
    45  }
    46  
    47  func userAgentString(api string) string {
    48  	return fmt.Sprintf("%s/%s/%s", prefix, api, version)
    49  }
    50  
    51  // userAgentTransport wraps an http.RoundTripper, adding a User-Agent header
    52  // to each request.
    53  type userAgentTransport struct {
    54  	base http.RoundTripper
    55  	api  string
    56  }
    57  
    58  func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    59  	// Clone the request to avoid mutating it.
    60  	newReq := *req
    61  	newReq.Header = make(http.Header)
    62  	for k, vv := range req.Header {
    63  		newReq.Header[k] = vv
    64  	}
    65  	// Append to the User-Agent string to preserve other information.
    66  	newReq.Header.Set("User-Agent", req.UserAgent()+" "+userAgentString(t.api))
    67  	return t.base.RoundTrip(&newReq)
    68  }
    69  
    70  // HTTPClient wraps client and appends a Go CDK string to the User-Agent
    71  // header for all requests.
    72  func HTTPClient(client *http.Client, api string) *http.Client {
    73  	c := *client
    74  	c.Transport = &userAgentTransport{base: c.Transport, api: api}
    75  	return &c
    76  }