cloud.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  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. // http://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. // Package cloud contains Google Cloud Platform APIs related types
  15. // and common functions.
  16. package cloud // import "google.golang.org/cloud"
  17. import (
  18. "net/http"
  19. "golang.org/x/net/context"
  20. "google.golang.org/cloud/internal"
  21. )
  22. // NewContext returns a new context that uses the provided http.Client.
  23. // Provided http.Client is responsible to authorize and authenticate
  24. // the requests made to the Google Cloud APIs.
  25. // It mutates the client's original Transport to append the cloud
  26. // package's user-agent to the outgoing requests.
  27. // You can obtain the project ID from the Google Developers Console,
  28. // https://console.developers.google.com.
  29. func NewContext(projID string, c *http.Client) context.Context {
  30. if c == nil {
  31. panic("invalid nil *http.Client passed to NewContext")
  32. }
  33. return WithContext(context.Background(), projID, c)
  34. }
  35. // WithContext returns a new context in a similar way NewContext does,
  36. // but initiates the new context with the specified parent.
  37. func WithContext(parent context.Context, projID string, c *http.Client) context.Context {
  38. // TODO(bradfitz): delete internal.Transport. It's too wrappy for what it does.
  39. // Do User-Agent some other way.
  40. if c == nil {
  41. panic("invalid nil *http.Client passed to WithContext")
  42. }
  43. if _, ok := c.Transport.(*internal.Transport); !ok {
  44. base := c.Transport
  45. if base == nil {
  46. base = http.DefaultTransport
  47. }
  48. c.Transport = &internal.Transport{Base: base}
  49. }
  50. return internal.WithContext(parent, projID, c)
  51. }