This repository has been archived by the owner on Jan 19, 2019. It is now read-only.
forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restjson.go
64 lines (54 loc) · 1.45 KB
/
restjson.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
package restjson
//go:generate go run ../../fixtures/protocol/generate.go ../../fixtures/protocol/input/rest-json.json build_test.go
//go:generate go run ../../fixtures/protocol/generate.go ../../fixtures/protocol/output/rest-json.json unmarshal_test.go
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/internal/protocol/jsonrpc"
"github.com/awslabs/aws-sdk-go/internal/protocol/rest"
)
func Build(r *aws.Request) {
rest.Build(r)
if t := rest.PayloadType(r.Params); t == "structure" || t == "" {
jsonrpc.Build(r)
}
}
func Unmarshal(r *aws.Request) {
if t := rest.PayloadType(r.Data); t == "structure" || t == "" {
jsonrpc.Unmarshal(r)
}
}
func UnmarshalMeta(r *aws.Request) {
rest.Unmarshal(r)
}
func UnmarshalError(r *aws.Request) {
code := r.HTTPResponse.Header.Get("X-Amzn-Errortype")
bodyBytes, err := ioutil.ReadAll(r.HTTPResponse.Body)
if err != nil {
r.Error = err
return
}
if len(bodyBytes) == 0 {
r.Error = aws.APIError{
StatusCode: r.HTTPResponse.StatusCode,
Message: r.HTTPResponse.Status,
}
return
}
var jsonErr jsonErrorResponse
if err := json.Unmarshal(bodyBytes, &jsonErr); err != nil {
r.Error = err
return
}
codes := strings.SplitN(code, ":", 2)
r.Error = aws.APIError{
StatusCode: r.HTTPResponse.StatusCode,
Code: codes[0],
Message: jsonErr.Message,
}
}
type jsonErrorResponse struct {
Message string `json:"message"`
}