add aws_lamba.invoke_async method

This commit is contained in:
Chris Keogh 2017-09-13 16:44:22 +12:00
parent ace54787c0
commit a379d76cfc
3 changed files with 48 additions and 1 deletions

View File

@ -33,13 +33,20 @@ class LambdaResponse(BaseResponse):
else:
raise ValueError("Cannot handle request")
def invoke_async(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
if request.method == 'POST':
return self._invoke_async(request, full_url)
else:
raise ValueError("Cannot handle request")
def _invoke(self, request, full_url):
response_headers = {}
lambda_backend = self.get_lambda_backend(full_url)
path = request.path if hasattr(request, 'path') else request.path_url
function_name = path.split('/')[-2]
if lambda_backend.has_function(function_name):
fn = lambda_backend.get_function(function_name)
payload = fn.invoke(self.body, self.headers, response_headers)
@ -48,6 +55,20 @@ class LambdaResponse(BaseResponse):
else:
return 404, response_headers, "{}"
def _invoke_async(self, request, full_url):
response_headers = {}
lambda_backend = self.get_lambda_backend(full_url)
path = request.path if hasattr(request, 'path') else request.path_url
function_name = path.split('/')[-3]
if lambda_backend.has_function(function_name):
fn = lambda_backend.get_function(function_name)
fn.invoke(self.body, self.headers, response_headers)
response_headers['Content-Length'] = str(0)
return 202, response_headers, ""
else:
return 404, response_headers, "{}"
def _list_functions(self, request, full_url, headers):
lambda_backend = self.get_lambda_backend(full_url)
return 200, {}, json.dumps({

View File

@ -11,4 +11,5 @@ url_paths = {
'{0}/(?P<api_version>[^/]+)/functions/?$': response.root,
'{0}/(?P<api_version>[^/]+)/functions/(?P<function_name>[\w_-]+)/?$': response.function,
'{0}/(?P<api_version>[^/]+)/functions/(?P<function_name>[\w_-]+)/invocations/?$': response.invoke,
'{0}/(?P<api_version>[^/]+)/functions/(?P<function_name>[\w_-]+)/invoke-async/?$': response.invoke_async,
}

View File

@ -469,3 +469,28 @@ def test_invoke_lambda_error():
assert 'FunctionError' in result
assert result['FunctionError'] == 'Handled'
@mock_lambda
def test_invoke_async_function():
conn = boto3.client('lambda', 'us-west-2')
conn.create_function(
FunctionName='testFunction',
Runtime='python2.7',
Role='test-iam-role',
Handler='lambda_function.handler',
Code={
'ZipFile': get_test_zip_file1(),
},
Description='test lambda function',
Timeout=3,
MemorySize=128,
Publish=True,
)
success_result = conn.invoke_async(
FunctionName='testFunction',
InvokeArgs=json.dumps({ 'test': 'event' })
)
print(success_result)
success_result['Status'].should.equal(202)