dial.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 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 transport/http supports network connections to HTTP servers.
  15. // This package is not intended for use by end developers. Use the
  16. // google.golang.org/api/option package to configure API clients.
  17. package http
  18. import (
  19. "errors"
  20. "net/http"
  21. "golang.org/x/net/context"
  22. "golang.org/x/oauth2"
  23. "google.golang.org/api/googleapi/transport"
  24. "google.golang.org/api/internal"
  25. "google.golang.org/api/option"
  26. )
  27. // NewClient returns an HTTP client for use communicating with a Google cloud
  28. // service, configured with the given ClientOptions. It also returns the endpoint
  29. // for the service as specified in the options.
  30. func NewClient(ctx context.Context, opts ...option.ClientOption) (*http.Client, string, error) {
  31. var o internal.DialSettings
  32. for _, opt := range opts {
  33. opt.Apply(&o)
  34. }
  35. if err := o.Validate(); err != nil {
  36. return nil, "", err
  37. }
  38. if o.GRPCConn != nil {
  39. return nil, "", errors.New("unsupported gRPC connection specified")
  40. }
  41. // TODO(cbro): consider injecting the User-Agent even if an explicit HTTP client is provided?
  42. if o.HTTPClient != nil {
  43. return o.HTTPClient, o.Endpoint, nil
  44. }
  45. uat := userAgentTransport{
  46. base: baseTransport(ctx),
  47. userAgent: o.UserAgent,
  48. }
  49. var hc *http.Client
  50. switch {
  51. case o.NoAuth:
  52. hc = &http.Client{Transport: uat}
  53. case o.APIKey != "":
  54. hc = &http.Client{
  55. Transport: &transport.APIKey{
  56. Key: o.APIKey,
  57. Transport: uat,
  58. },
  59. }
  60. default:
  61. creds, err := internal.Creds(ctx, &o)
  62. if err != nil {
  63. return nil, "", err
  64. }
  65. hc = &http.Client{
  66. Transport: &oauth2.Transport{
  67. Source: creds.TokenSource,
  68. Base: uat,
  69. },
  70. }
  71. }
  72. return hc, o.Endpoint, nil
  73. }
  74. type userAgentTransport struct {
  75. userAgent string
  76. base http.RoundTripper
  77. }
  78. func (t userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  79. rt := t.base
  80. if rt == nil {
  81. return nil, errors.New("transport: no Transport specified")
  82. }
  83. if t.userAgent == "" {
  84. return rt.RoundTrip(req)
  85. }
  86. newReq := *req
  87. newReq.Header = make(http.Header)
  88. for k, vv := range req.Header {
  89. newReq.Header[k] = vv
  90. }
  91. // TODO(cbro): append to existing User-Agent header?
  92. newReq.Header["User-Agent"] = []string{t.userAgent}
  93. return rt.RoundTrip(&newReq)
  94. }
  95. // Set at init time by dial_appengine.go. If nil, we're not on App Engine.
  96. var appengineUrlfetchHook func(context.Context) http.RoundTripper
  97. // baseTransport returns the base HTTP transport.
  98. // On App Engine, this is urlfetch.Transport, otherwise it's http.DefaultTransport.
  99. func baseTransport(ctx context.Context) http.RoundTripper {
  100. if appengineUrlfetchHook != nil {
  101. return appengineUrlfetchHook(ctx)
  102. }
  103. return http.DefaultTransport
  104. }