/
unmarshal.go
102 lines (86 loc) · 3.08 KB
/
unmarshal.go
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
package computing
import (
"bytes"
"encoding/xml"
"io"
"io/ioutil"
request "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/nifcloud/nifcloud-sdk-go/private/protocol/xml/xmlutil"
)
// UnmarshalHandler is a named request handler for unmarshaling computing protocol requests
var UnmarshalHandler = request.NamedHandler{Name: "awssdk.computing.Unmarshal", Fn: Unmarshal}
// UnmarshalMetaHandler is a named request handler for unmarshaling computing protocol request metadata
var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.computing.UnmarshalMeta", Fn: UnmarshalMeta}
// UnmarshalErrorHandler is a named request handler for unmarshaling computing protocol request errors
var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.computing.UnmarshalError", Fn: UnmarshalError}
// Unmarshal unmarshals a response body for the EC2 protocol.
func Unmarshal(r *request.Request) {
defer func() {
err := r.HTTPResponse.Body.Close()
if err != nil {
r.Error = awserr.New("CloseError", "failed to close response body", err)
}
}()
decoder := xml.NewDecoder(r.HTTPResponse.Body)
err := xmlutil.UnmarshalXML(r.Data, decoder, r.Operation.Name+"Result")
if err != nil {
r.Error = awserr.New("SerializationError", "failed decoding computing response", err)
return
}
}
type responseMetadata struct {
RequestID string `xml:"requestId"`
ResponseMetadata struct {
RequestID string `xml:"RequestId"`
} `xml:"ResponseMetadata"`
}
// UnmarshalMeta unmarshals response headers for the EC2 protocol.
func UnmarshalMeta(r *request.Request) {
bodyBytes, err := ioutil.ReadAll(r.HTTPResponse.Body)
if err != nil && err != io.EOF {
r.Error = awserr.New("ReadError", "failed decoding computing response meta data", err)
return
}
buf := bytes.NewBuffer(bodyBytes)
resp := &responseMetadata{}
err = xml.NewDecoder(buf).Decode(resp)
if err != nil && err != io.EOF {
r.Error = awserr.New("SerializationError", "failed decoding computing response meta data", err)
return
}
if resp.RequestID != "" {
r.RequestID = resp.RequestID
}
// for Loadbalancer API
if resp.ResponseMetadata.RequestID != "" {
r.RequestID = resp.ResponseMetadata.RequestID
}
r.HTTPResponse.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
}
type xmlErrorResponse struct {
XMLName xml.Name `xml:"Response"`
Code string `xml:"Errors>Error>Code"`
Message string `xml:"Errors>Error>Message"`
RequestID string `xml:"RequestID"`
}
// UnmarshalError unmarshals a response error for the EC2 protocol.
func UnmarshalError(r *request.Request) {
defer func() {
err := r.HTTPResponse.Body.Close()
if err != nil {
r.Error = awserr.New("CloseError", "failed to close response body", err)
}
}()
resp := &xmlErrorResponse{}
err := xml.NewDecoder(r.HTTPResponse.Body).Decode(resp)
if err != nil && err != io.EOF {
r.Error = awserr.New("SerializationError", "failed decoding computing error response", err)
} else {
r.Error = awserr.NewRequestFailure(
awserr.New(resp.Code, resp.Message, nil),
r.HTTPResponse.StatusCode,
resp.RequestID,
)
}
}