-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
49 lines (41 loc) · 1.29 KB
/
Copy pathresponse.go
File metadata and controls
49 lines (41 loc) · 1.29 KB
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
// SPDX-License-Identifier: Apache-2.0
package shared
import (
"encoding/json"
"encoding/xml"
"github.com/skyoo2003/devcloud/internal/plugin"
)
func JSONResponse(status int, v any) (*plugin.Response, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
return &plugin.Response{StatusCode: status, Body: b, ContentType: "application/x-amz-json-1.1"}, nil
}
func XMLResponse(status int, v any) (*plugin.Response, error) {
b, err := xml.Marshal(v)
if err != nil {
return nil, err
}
return &plugin.Response{StatusCode: status, Body: b, ContentType: "text/xml"}, nil
}
func JSONError(code, message string, status int) *plugin.Response {
b, _ := json.Marshal(map[string]string{"__type": code, "message": message})
return &plugin.Response{StatusCode: status, Body: b, ContentType: "application/x-amz-json-1.1"}
}
type queryXMLErrorResponse struct {
XMLName xml.Name `xml:"Response"`
Errors struct {
Error struct {
Code string `xml:"Code"`
Message string `xml:"Message"`
} `xml:"Error"`
} `xml:"Errors"`
}
func QueryXMLError(code, message string, status int) *plugin.Response {
e := queryXMLErrorResponse{}
e.Errors.Error.Code = code
e.Errors.Error.Message = message
b, _ := xml.Marshal(e)
return &plugin.Response{StatusCode: status, Body: b, ContentType: "text/xml"}
}