You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

39 lines
1.0 KiB

  1. // Copyright 2012 Google Inc. 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. package transport
  7. import (
  8. "errors"
  9. "net/http"
  10. )
  11. // APIKey is an HTTP Transport which wraps an underlying transport and
  12. // appends an API Key "key" parameter to the URL of outgoing requests.
  13. type APIKey struct {
  14. // Key is the API Key to set on requests.
  15. Key string
  16. // Transport is the underlying HTTP transport.
  17. // If nil, http.DefaultTransport is used.
  18. Transport http.RoundTripper
  19. }
  20. func (t *APIKey) RoundTrip(req *http.Request) (*http.Response, error) {
  21. rt := t.Transport
  22. if rt == nil {
  23. rt = http.DefaultTransport
  24. if rt == nil {
  25. return nil, errors.New("googleapi/transport: no Transport specified or available")
  26. }
  27. }
  28. newReq := *req
  29. args := newReq.URL.Query()
  30. args.Set("key", t.Key)
  31. newReq.URL.RawQuery = args.Encode()
  32. return rt.RoundTrip(&newReq)
  33. }