~dave-cheney/goose/003-fix-fd-leak

« back to all changes in this revision

Viewing changes to http/client.go

  • Committer: Tarmac
  • Author(s): John Arbash Meinel
  • Date: 2013-02-07 05:29:13 UTC
  • mfrom: (65.2.5 user-agent)
  • Revision ID: tarmac-20130207052913-e5vfzm0pg3786t9i
[r=jameinel] http/client.go,version.go: Add User-agent: goose header

This changes the User-Agent supplied by goose from being a generic "Go http package" to stating it is goose and what version.

Along with that change, it adds a goose.Version to the package, and goose.VersionNumber.

I didn't try to do the complex work that juju-core/version/Version does. I didn't need a lot of version number parsing, so it made more sense to enter the version as integers, and have it build the string trivially.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
        "fmt"
9
9
        "io"
10
10
        "io/ioutil"
 
11
        "launchpad.net/goose"
11
12
        "launchpad.net/goose/errors"
12
13
        "log"
13
14
        "net/http"
19
20
)
20
21
 
21
22
const (
22
 
        contentTypeJson        = "application/json"
 
23
        contentTypeJSON        = "application/json"
23
24
        contentTypeOctetStream = "application/octet-stream"
24
25
)
25
26
 
68
69
        return &Client{httpClient, logger, token, MaxSendAttempts}
69
70
}
70
71
 
 
72
func gooseAgent() string {
 
73
        return fmt.Sprintf("goose (%s)", goose.Version)
 
74
}
 
75
 
 
76
func createHeaders(extraHeaders http.Header, contentType string) http.Header {
 
77
        headers := make(http.Header)
 
78
        if extraHeaders != nil {
 
79
                for header, values := range extraHeaders {
 
80
                        for _, value := range values {
 
81
                                headers.Add(header, value)
 
82
                        }
 
83
                }
 
84
        }
 
85
        headers.Add("Content-Type", contentType)
 
86
        headers.Add("Accept", contentType)
 
87
        headers.Add("User-Agent", gooseAgent())
 
88
        return headers
 
89
}
 
90
 
71
91
// JsonRequest JSON encodes and sends the supplied object (if any) to the specified URL.
72
92
// Optional method arguments are pass using the RequestData object.
73
93
// Relevant RequestData fields:
88
108
                        return
89
109
                }
90
110
        }
91
 
        headers := make(http.Header)
92
 
        if reqData.ReqHeaders != nil {
93
 
                for header, values := range reqData.ReqHeaders {
94
 
                        for _, value := range values {
95
 
                                headers.Add(header, value)
96
 
                        }
97
 
                }
98
 
        }
99
 
        headers.Add("Content-Type", contentTypeJson)
100
 
        headers.Add("Accept", contentTypeJson)
 
111
        headers := createHeaders(reqData.ReqHeaders, contentTypeJSON)
101
112
        respBody, err := c.sendRequest(method, url, bytes.NewReader(body), len(body), headers, reqData.ExpectedStatus)
102
113
        if err != nil {
103
114
                return
133
144
        if reqData.Params != nil {
134
145
                url += "?" + reqData.Params.Encode()
135
146
        }
136
 
        headers := make(http.Header)
137
 
        if reqData.ReqHeaders != nil {
138
 
                for header, values := range reqData.ReqHeaders {
139
 
                        for _, value := range values {
140
 
                                headers.Add(header, value)
141
 
                        }
142
 
                }
143
 
        }
144
 
        headers.Add("Content-Type", contentTypeOctetStream)
145
 
        headers.Add("Accept", contentTypeOctetStream)
 
147
        headers := createHeaders(reqData.ReqHeaders, contentTypeOctetStream)
146
148
        respBody, err := c.sendRequest(method, url, reqData.ReqReader, reqData.ReqLength, headers, reqData.ExpectedStatus)
147
149
        if err != nil {
148
150
                return
250
252
        errBytes, _ := ioutil.ReadAll(resp.Body)
251
253
        errInfo := string(errBytes)
252
254
        // Check if we have a JSON representation of the failure, if so decode it.
253
 
        if resp.Header.Get("Content-Type") == contentTypeJson {
 
255
        if resp.Header.Get("Content-Type") == contentTypeJSON {
254
256
                var wrappedErr ErrorWrapper
255
257
                if err := json.Unmarshal(errBytes, &wrappedErr); err == nil {
256
258
                        errInfo = wrappedErr.Error.Error()