~allenap/gwacl/testing-not-verbose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2013 Canonical Ltd.  This software is licensed under the
// GNU Lesser General Public License version 3 (see the file COPYING).

package gwacl

import (
    "bytes"
    "io/ioutil"
    "launchpad.net/gwacl/fork/http"
    . "launchpad.net/gwacl/logging"
)

type X509Request struct {
    APIVersion  string
    Method      string
    URL         string
    Payload     []byte
    ContentType string
}

// newX509RequestGET initializes an X509Request for a GET.  You may still need
// to set further values.
func newX509RequestGET(url, apiVersion string) *X509Request {
    return &X509Request{
        Method:     "GET",
        URL:        url,
        APIVersion: apiVersion,
    }
}

// newX509RequestPOST initializes an X509Request for a POST.  You may still
// need to set further values.
func newX509RequestPOST(url, apiVersion string, payload []byte, contentType string) *X509Request {
    return &X509Request{
        Method:      "POST",
        URL:         url,
        APIVersion:  apiVersion,
        Payload:     payload,
        ContentType: contentType,
    }
}

// newX509RequestDELETE initializes an X509Request for a DELETE.
func newX509RequestDELETE(url, apiVersion string) *X509Request {
    return &X509Request{
        Method:     "DELETE",
        URL:        url,
        APIVersion: apiVersion,
    }
}

// newX509RequestPUT initializes an X509Request for a PUT.  You may still
// need to set further values.
func newX509RequestPUT(url, apiVersion string, payload []byte, contentType string) *X509Request {
    return &X509Request{
        Method:      "PUT",
        URL:         url,
        APIVersion:  apiVersion,
        Payload:     payload,
        ContentType: contentType,
    }
}

type x509Response struct {
    StatusCode int
    // TODO: What exactly do we get back?  How will we know its encoding?
    Body   []byte
    Header http.Header
}

func performX509Request(session *x509Session, request *X509Request) (*x509Response, error) {
    response := &x509Response{}

    Debugf("Request: %s %s", request.Method, request.URL)
    if len(request.Payload) > 0 {
        Debugf("Request body:\n%s", request.Payload)
    }

    bodyReader := ioutil.NopCloser(bytes.NewReader(request.Payload))
    httpRequest, err := http.NewRequest(request.Method, request.URL, bodyReader)
    if err != nil {
        return nil, err
    }
    httpRequest.Header.Set("Content-Type", request.ContentType)
    httpRequest.Header.Set("x-ms-version", request.APIVersion)
    retrier := session.retryPolicy.getForkedHttpRetrier(session.client)
    httpResponse, err := retrier.RetryRequest(httpRequest)
    if err != nil {
        return nil, err
    }

    response.StatusCode = httpResponse.StatusCode
    response.Body, err = readAndClose(httpResponse.Body)
    if err != nil {
        return nil, err
    }
    response.Header = httpResponse.Header

    Debugf("Response: %d %s", response.StatusCode, http.StatusText(response.StatusCode))
    if response.Header != nil {
        buf := bytes.Buffer{}
        response.Header.Write(&buf)
        Debugf("Response headers:\n%s", buf.String())
    }
    if len(response.Body) > 0 {
        Debugf("Response body:\n%s", response.Body)
    }

    return response, nil
}