cloud.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 internal provides support for the cloud packages.
  15. //
  16. // Users should not import this package directly.
  17. package internal
  18. import (
  19. "fmt"
  20. "net/http"
  21. "sync"
  22. "golang.org/x/net/context"
  23. )
  24. type contextKey struct{}
  25. func WithContext(parent context.Context, projID string, c *http.Client) context.Context {
  26. if c == nil {
  27. panic("nil *http.Client passed to WithContext")
  28. }
  29. if projID == "" {
  30. panic("empty project ID passed to WithContext")
  31. }
  32. return context.WithValue(parent, contextKey{}, &cloudContext{
  33. ProjectID: projID,
  34. HTTPClient: c,
  35. })
  36. }
  37. const userAgent = "gcloud-golang/0.1"
  38. type cloudContext struct {
  39. ProjectID string
  40. HTTPClient *http.Client
  41. mu sync.Mutex // guards svc
  42. svc map[string]interface{} // e.g. "storage" => *rawStorage.Service
  43. }
  44. // Service returns the result of the fill function if it's never been
  45. // called before for the given name (which is assumed to be an API
  46. // service name, like "datastore"). If it has already been cached, the fill
  47. // func is not run.
  48. // It's safe for concurrent use by multiple goroutines.
  49. func Service(ctx context.Context, name string, fill func(*http.Client) interface{}) interface{} {
  50. return cc(ctx).service(name, fill)
  51. }
  52. func (c *cloudContext) service(name string, fill func(*http.Client) interface{}) interface{} {
  53. c.mu.Lock()
  54. defer c.mu.Unlock()
  55. if c.svc == nil {
  56. c.svc = make(map[string]interface{})
  57. } else if v, ok := c.svc[name]; ok {
  58. return v
  59. }
  60. v := fill(c.HTTPClient)
  61. c.svc[name] = v
  62. return v
  63. }
  64. // Transport is an http.RoundTripper that appends
  65. // Google Cloud client's user-agent to the original
  66. // request's user-agent header.
  67. type Transport struct {
  68. // Base is the actual http.RoundTripper
  69. // requests will use. It must not be nil.
  70. Base http.RoundTripper
  71. }
  72. // RoundTrip appends a user-agent to the existing user-agent
  73. // header and delegates the request to the base http.RoundTripper.
  74. func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
  75. req = cloneRequest(req)
  76. ua := req.Header.Get("User-Agent")
  77. if ua == "" {
  78. ua = userAgent
  79. } else {
  80. ua = fmt.Sprintf("%s %s", ua, userAgent)
  81. }
  82. req.Header.Set("User-Agent", ua)
  83. return t.Base.RoundTrip(req)
  84. }
  85. // cloneRequest returns a clone of the provided *http.Request.
  86. // The clone is a shallow copy of the struct and its Header map.
  87. func cloneRequest(r *http.Request) *http.Request {
  88. // shallow copy of the struct
  89. r2 := new(http.Request)
  90. *r2 = *r
  91. // deep copy of the Header
  92. r2.Header = make(http.Header)
  93. for k, s := range r.Header {
  94. r2.Header[k] = s
  95. }
  96. return r2
  97. }
  98. func ProjID(ctx context.Context) string {
  99. return cc(ctx).ProjectID
  100. }
  101. func HTTPClient(ctx context.Context) *http.Client {
  102. return cc(ctx).HTTPClient
  103. }
  104. // cc returns the internal *cloudContext (cc) state for a context.Context.
  105. // It panics if the user did it wrong.
  106. func cc(ctx context.Context) *cloudContext {
  107. if c, ok := ctx.Value(contextKey{}).(*cloudContext); ok {
  108. return c
  109. }
  110. panic("invalid context.Context type; it should be created with cloud.NewContext")
  111. }