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