-
Notifications
You must be signed in to change notification settings - Fork 40
/
apigw_v1.go
68 lines (57 loc) · 1.98 KB
/
apigw_v1.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
package algnhsa
import (
"context"
"encoding/json"
"errors"
"net/http"
"path"
"github.com/aws/aws-lambda-go/events"
)
/*
AWS Documentation:
- https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
*/
var (
errAPIGatewayV1UnexpectedRequest = errors.New("expected APIGatewayProxyRequest event")
)
func newAPIGatewayV1Request(ctx context.Context, payload []byte, opts *Options) (lambdaRequest, error) {
var event events.APIGatewayProxyRequest
if err := json.Unmarshal(payload, &event); err != nil {
return lambdaRequest{}, err
}
if event.RequestContext.AccountID == "" {
return lambdaRequest{}, errAPIGatewayV1UnexpectedRequest
}
req := lambdaRequest{
HTTPMethod: event.HTTPMethod,
Path: event.Path,
QueryStringParameters: event.QueryStringParameters,
MultiValueQueryStringParameters: event.MultiValueQueryStringParameters,
Headers: event.Headers,
MultiValueHeaders: event.MultiValueHeaders,
Body: event.Body,
IsBase64Encoded: event.IsBase64Encoded,
SourceIP: event.RequestContext.Identity.SourceIP,
Context: context.WithValue(ctx, RequestTypeAPIGatewayV1, event),
requestType: RequestTypeAPIGatewayV1,
}
if opts.UseProxyPath {
req.Path = path.Join("/", event.PathParameters["proxy"])
}
return req, nil
}
func newAPIGatewayV1Response(r *http.Response) (lambdaResponse, error) {
resp := lambdaResponse{
MultiValueHeaders: r.Header,
}
return resp, nil
}
// APIGatewayV1RequestFromContext extracts the APIGatewayProxyRequest event from ctx.
func APIGatewayV1RequestFromContext(ctx context.Context) (events.APIGatewayProxyRequest, bool) {
val := ctx.Value(RequestTypeAPIGatewayV1)
if val == nil {
return events.APIGatewayProxyRequest{}, false
}
event, ok := val.(events.APIGatewayProxyRequest)
return event, ok
}