-
Notifications
You must be signed in to change notification settings - Fork 0
/
responseentity.go
47 lines (41 loc) · 1.06 KB
/
responseentity.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
package http
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
)
var (
headers = map[string]string{
"Content-Type": "application/json",
"X-Custom-Header": "application/json",
}
JsonMarshalResponse = events.APIGatewayProxyResponse{
StatusCode: INTERNAL_SERVER_ERROR.Code,
Headers: headers,
MultiValueHeaders: nil,
Body: "Error Marshaling JSON Object!",
IsBase64Encoded: false,
}
)
type ResponseEntity struct {
ResponseObject interface{}
Status Status
Error error
}
func (entity ResponseEntity) toJson() (string, error) {
bytes, err := json.Marshal(&entity.ResponseObject)
return string(bytes), err
}
func (entity ResponseEntity) ToApiGatewayProxyResponse() events.APIGatewayProxyResponse {
jsonStr, err := entity.toJson()
if err != nil {
return JsonMarshalResponse
} else {
return events.APIGatewayProxyResponse{
StatusCode: entity.Status.Code,
Headers: headers,
MultiValueHeaders: nil,
Body: jsonStr,
IsBase64Encoded: false,
}
}
}