forked from aws/aws-lambda-ruby-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_lambda_marshaller.rb
37 lines (33 loc) · 1.24 KB
/
aws_lambda_marshaller.rb
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
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# frozen_string_literal: true
require 'stringio'
module AwsLambda
class Marshaller
class << self
# By default, JSON-parses the raw request body. This can be overwritten
# by users who know what they are doing.
def marshall_request(raw_request)
content_type = raw_request['Content-Type']
if content_type == 'application/json'
JSON.parse(raw_request.body)
else
raw_request.body # return it unaltered
end
end
# By default, just runs #to_json on the method's response value.
# This can be overwritten by users who know what they are doing.
# The response is an array of response, content-type.
# If returned without a content-type, it is assumed to be application/json
# Finally, StringIO/IO is used to signal a response that shouldn't be
# formatted as JSON, and should get a different content-type header.
def marshall_response(method_response)
case method_response
when StringIO, IO
[method_response, 'application/unknown']
else
method_response.to_json # application/json is assumed
end
end
end
end
end