Skip to content

Experiment converting JSON::Aany to string #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spec/lambda_builder/http_request_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ describe Lambda::Builder::HTTPRequest do
req.query_params["test"].should eq "bar"
end

it "parses api gateway v2 (HTTP gateway)" do
ENV["_HANDLER"] = "foo"
input = JSON.parse(request_body_v2)
req = Lambda::Builder::HTTPRequest.new(input)
req.method.should eq "OPTIONS"
req.path.should eq "/hi"
end

it "works with empty body and query string" do
body = %q({ "path" : "/test", "httpMethod" : "GET", "headers" : { "key": "value" }, "requestContext" : {} })
response = HTTP::Client::Response.new(200, body, HTTP::Headers.new)
Expand Down
8 changes: 4 additions & 4 deletions spec/lambda_builder/runtime_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe Lambda::Builder::Runtime do
runtime = Lambda::Builder::Runtime.new
# handler = do |_input| JSON.parse Lambda::Builder::HTTPResponse.new(200).to_json end
runtime.register_handler("my_handler") do |_input|
JSON.parse(%q({ "foo" : "bar"}))
%q({ "foo" : "bar"})
end
runtime.handlers["my_handler"].should_not be_nil
end
Expand All @@ -44,7 +44,7 @@ describe Lambda::Builder::Runtime do

runtime = Lambda::Builder::Runtime.new
runtime.register_handler("my_handler") do
JSON.parse(%q({ "foo" : "bar" }))
%q({ "foo" : "bar" })
end
runtime.process_handler
end
Expand All @@ -67,7 +67,7 @@ describe Lambda::Builder::Runtime do
runtime.register_handler("my_handler") do
response = Lambda::Builder::HTTPResponse.new(200, "text body")
response.headers["Content-Type"] = "application/text"
JSON.parse response.to_json
response.to_json
end
runtime.process_handler
end
Expand Down Expand Up @@ -102,7 +102,7 @@ describe Lambda::Builder::Runtime do

runtime = Lambda::Builder::Runtime.new
runtime.register_handler("my_handler") do
JSON.parse "{}"
"{}"
end
runtime.process_handler

Expand Down
55 changes: 55 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,58 @@ def request_body
}
END
end

def request_body_v2
<<-END
{
"version": "2.0",
"routeKey": "OPTIONS /{proxy+}",
"rawPath": "/hi",
"rawQueryString": "",
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"access-control-request-headers": "authorization,content-type",
"access-control-request-method": "POST",
"content-length": "0",
"content-type": "application/x-www-form-urlencoded",
"forwarded": "by=0.0.0.0;for=0.0.0.0;host=local.dev;proto=https",
"host": "local.dev",
"origin": "https://local.dev",
"referer": "https://local.dev/",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/0.0.0.0 Safari/537.36",
"via": "HTTP/1.1 AmazonAPIGateway",
"x-amzn-trace-id": "Self=1-60ad1af6-6041ce5335bd44ad26b42c70;Root=1-60ad2af6-7bbc041f64b16ac44ad97952",
"x-forwarded-for": "0.0.0.0",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"pathParameters": {
"proxy": "score"
},
"requestContext": {
"routeKey": "OPTIONS /{proxy+}",
"accountId": "5555555555",
"stage": "$default",
"requestId": "f5Emhi3LIAMEM7A=",
"apiId": "xxxxx",
"domainName": "local.dev",
"domainPrefix": "local",
"time": "25/May/2021:15:42:46 +0000",
"timeEpoch": 1621957366418,
"http": {
"method": "OPTIONS",
"path": "/hi",
"protocol": "HTTP/1.1",
"sourceIp": "0.0.0.0",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/0.0.0.0 Safari/537.36"
}
},
"isBase64Encoded": false
}
END
end
13 changes: 7 additions & 6 deletions src/lambda_builder/runtime.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Lambda::Builder
class Runtime
getter host : String
getter port : Int16
getter handlers : Hash(String, (JSON::Any -> JSON::Any)) = Hash(String, (JSON::Any -> JSON::Any)).new
getter handlers : Hash(String, (String -> String)) = Hash(String, (String -> String)).new
Log = ::Log.for(self)

def initialize
Expand All @@ -18,7 +18,7 @@ module Lambda::Builder
end

# Associate the block/proc to the function name
def register_handler(name : String, &handler : JSON::Any -> JSON::Any)
def register_handler(name : String, &handler : String -> String)
self.handlers[name] = handler
end

Expand All @@ -38,7 +38,7 @@ module Lambda::Builder
end
end

def _process_request(proc : Proc(JSON::Any, JSON::Any))
def _process_request(proc : Proc(String, String))
client = HTTP::Client.new(host: @host, port: @port)

begin
Expand All @@ -48,11 +48,12 @@ module Lambda::Builder
aws_request_id = response.headers["Lambda-Runtime-Aws-Request-Id"]
base_url = "/2018-06-01/runtime/invocation/#{aws_request_id}"

input = JSON.parse response.body
body = proc.call input
# input = JSON.parse response.body
# body = proc.call input
body = proc.call response.body

Log.info { "preparing body #{body}" }
response = client.post("#{base_url}/response", body: body.to_json)
response = client.post("#{base_url}/response", body: body)
Log.debug { "response invocation response #{response.status_code} #{response.body}" }
rescue ex
body = %Q({ "statusCode": 500, "body" : "#{ex.message}" })
Expand Down