apikey.go raw

   1  // Copyright 2012 Google LLC. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  // Package transport contains HTTP transports used to make
   6  // authenticated API requests.
   7  //
   8  // This package is DEPRECATED. Users should instead use,
   9  //
  10  //	service, err := NewService(..., option.WithAPIKey(...))
  11  package transport
  12  
  13  import (
  14  	"errors"
  15  	"net/http"
  16  )
  17  
  18  // APIKey is an HTTP Transport which wraps an underlying transport and
  19  // appends an API Key "key" parameter to the URL of outgoing requests.
  20  //
  21  // Deprecated: please use NewService(..., option.WithAPIKey(...)) instead.
  22  type APIKey struct {
  23  	// Key is the API Key to set on requests.
  24  	Key string
  25  
  26  	// Transport is the underlying HTTP transport.
  27  	// If nil, http.DefaultTransport is used.
  28  	Transport http.RoundTripper
  29  }
  30  
  31  func (t *APIKey) RoundTrip(req *http.Request) (*http.Response, error) {
  32  	rt := t.Transport
  33  	if rt == nil {
  34  		rt = http.DefaultTransport
  35  		if rt == nil {
  36  			return nil, errors.New("googleapi/transport: no Transport specified or available")
  37  		}
  38  	}
  39  	newReq := *req
  40  	args := newReq.URL.Query()
  41  	args.Set("key", t.Key)
  42  	newReq.URL.RawQuery = args.Encode()
  43  	return rt.RoundTrip(&newReq)
  44  }
  45