Skip to content

Commit

Permalink
Adding a user-agent to the all outgoing calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Sep 25, 2014
1 parent 6601a4b commit d365d2b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
16 changes: 10 additions & 6 deletions compute/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"strings"
"sync"
"time"

"google.golang.org/cloud/internal"
)

type cachedValue struct {
Expand All @@ -44,12 +46,14 @@ var (
)

var metaClient = &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 750 * time.Millisecond,
KeepAlive: 30 * time.Second,
}).Dial,
ResponseHeaderTimeout: 750 * time.Millisecond,
Transport: &internal.UATransport{
Base: &http.Transport{
Dial: (&net.Dialer{
Timeout: 750 * time.Millisecond,
KeepAlive: 30 * time.Second,
}).Dial,
ResponseHeaderTimeout: 750 * time.Millisecond,
},
},
}

Expand Down
48 changes: 48 additions & 0 deletions internal/cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package internal provides support for package cloud.
//
// Users should not import this package directly.
package internal

import (
"fmt"
"net/http"
)

const userAgent = "gcloud-golang/0.1"

// UATransport is an http.RoundTripper that appends
// Google Cloud client's user-agent to the original
// request's user-agent header.
type UATransport struct {
// Base represents the actual http.RoundTripper
// the requests will be delegated to.
Base http.RoundTripper
}

// RoundTrip appends a user-agent to the existing user-agent
// header and delegates the request to the base http.RoundTripper.
func (t *UATransport) RoundTrip(req *http.Request) (*http.Response, error) {
// TODO(jbd): Is it OK to mutate request? UATransport is internal only.
ua := req.Header.Get("User-Agent")
if ua == "" {
ua = userAgent
} else {
ua = fmt.Sprintf("%s;%s", ua, userAgent)
}
req.Header.Set("User-Agent", ua)
return t.Base.RoundTrip(req)
}
4 changes: 4 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"

raw "code.google.com/p/google-api-go-client/storage/v1"
"google.golang.org/cloud/internal"
)

const (
Expand Down Expand Up @@ -81,7 +82,10 @@ func New(tr http.RoundTripper) *Client {
// uses the provided http.Client. Provided http.Client is responsible
// to authorize and authenticate the requests made to the
// Google Cloud Storage API.
// It mutates the client's original Transport to append the cloud
// package's user-agent to the outgoing requests.
func NewWithClient(c *http.Client) *Client {
c.Transport = &internal.UATransport{Base: c.Transport}
s, _ := raw.New(c)
return &Client{conn: &conn{s: s, c: c}}
}
Expand Down

0 comments on commit d365d2b

Please sign in to comment.