apikey.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // Package transport contains HTTP transports used to make
  5. // authenticated API requests.
  6. //
  7. // This package is DEPRECATED. Users should instead use,
  8. //
  9. // service, err := NewService(..., option.WithAPIKey(...))
  10. package transport
  11. import (
  12. "errors"
  13. "net/http"
  14. )
  15. // APIKey is an HTTP Transport which wraps an underlying transport and
  16. // appends an API Key "key" parameter to the URL of outgoing requests.
  17. //
  18. // Deprecated: please use NewService(..., option.WithAPIKey(...)) instead.
  19. type APIKey struct {
  20. // Key is the API Key to set on requests.
  21. Key string
  22. // Transport is the underlying HTTP transport.
  23. // If nil, http.DefaultTransport is used.
  24. Transport http.RoundTripper
  25. }
  26. func (t *APIKey) RoundTrip(req *http.Request) (*http.Response, error) {
  27. rt := t.Transport
  28. if rt == nil {
  29. rt = http.DefaultTransport
  30. if rt == nil {
  31. return nil, errors.New("googleapi/transport: no Transport specified or available")
  32. }
  33. }
  34. newReq := *req
  35. args := newReq.URL.Query()
  36. args.Set("key", t.Key)
  37. newReq.URL.RawQuery = args.Encode()
  38. return rt.RoundTrip(&newReq)
  39. }