-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_response.go
65 lines (56 loc) · 1.87 KB
/
web_response.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
package lambda
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/events"
"io"
"net/http"
)
func CheckResponse(statusCode int, body io.ReadCloser) (int, error) {
if !(200 <= statusCode && statusCode < 300) {
data, _ := io.ReadAll(body)
return statusCode, fmt.Errorf("http response status not ok, status: %d, body: %s", statusCode, string(data))
}
return statusCode, nil
}
func SerializeResponse(statusCode int, body io.ReadCloser, outptr any) (int, error) {
data, err := io.ReadAll(body)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("http response read error: %w", err)
}
err = json.Unmarshal(data, outptr)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("http response unmarshal error: %w, data: %s", err, string(data))
}
return statusCode, nil
}
func SendJSON(object any, statusCode int) (*events.APIGatewayV2HTTPResponse, error) {
bytes, err := json.Marshal(object)
if err != nil {
return nil, fmt.Errorf("response json, marshal error, %w", err)
}
return &events.APIGatewayV2HTTPResponse{
StatusCode: statusCode,
Body: base64.StdEncoding.EncodeToString(bytes),
IsBase64Encoded: true,
Headers: map[string]string{
"Content-Type": "application/json",
},
}, nil
}
func SendOK(data any) (*events.APIGatewayV2HTTPResponse, error) {
return SendJSON(data, 200)
}
func SendERR(error string, statusCode int) (*events.APIGatewayV2HTTPResponse, error) {
return SendJSON(map[string]string{"error": error}, statusCode)
}
func SendInvalidToken() (*events.APIGatewayV2HTTPResponse, error) {
return SendERR("ERR-INVALID-TOKEN", 401)
}
func SendInvalidArgs(name string) (*events.APIGatewayV2HTTPResponse, error) {
return SendERR("ERR-INVALID-ARG: "+name, 400)
}
func SendRemoteServer(status int) (*events.APIGatewayV2HTTPResponse, error) {
return SendERR("ERR-REMOTE-SERVER", status)
}