Class ALambdaApiGatewayFunction
ALambdaApiGatewayFunction is the abstract base class for API Gateway functions (both V1 and V2).
Implements
Inherited Members
Namespace: LambdaSharp.ApiGateway
Assembly: LambdaSharp.ApiGateway.dll
Syntax
public abstract class ALambdaApiGatewayFunction : ALambdaFunction, ILambdaSharpLogger
Remarks
If the Lambda function was configured for REST API or WebSocket routes with method invocations, then the Lambda function will act as a controller and route the incoming requests to the specified methods. Alternatively, a derived class can override ProcessProxyRequestAsync(APIGatewayProxyRequest) to process requests.
Constructors
ALambdaApiGatewayFunction(ILambdaJsonSerializer)
Initializes a new ALambdaApiGatewayFunction instance using the default implementation of ILambdaFunctionDependencyProvider.
Declaration
protected ALambdaApiGatewayFunction(ILambdaJsonSerializer serializer)
Parameters
| Type | Name | Description |
|---|---|---|
| ILambdaJsonSerializer | serializer | Custom implementation of ILambdaJsonSerializer. |
ALambdaApiGatewayFunction(ILambdaJsonSerializer, ILambdaFunctionDependencyProvider)
Initializes a new ALambdaApiGatewayFunction instance using a custom implementation of ILambdaFunctionDependencyProvider.
Declaration
protected ALambdaApiGatewayFunction(ILambdaJsonSerializer serializer, ILambdaFunctionDependencyProvider provider)
Parameters
| Type | Name | Description |
|---|---|---|
| ILambdaJsonSerializer | serializer | Custom implementation of ILambdaJsonSerializer. |
| ILambdaFunctionDependencyProvider | provider | Custom implementation of ILambdaFunctionDependencyProvider. |
Properties
CurrentRequest
Retrieve the current Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest for the request.
Declaration
protected APIGatewayProxyRequest CurrentRequest { get; }
Property Value
| Type | Description |
|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest instance. |
Remarks
This property is only set during the invocation of ProcessMessageAsync(APIGatewayProxyRequest). Otherwise, it returns null.
LambdaSerializer
An instance of ILambdaJsonSerializer used for serializing/deserializing JSON data.
Declaration
protected ILambdaJsonSerializer LambdaSerializer { get; }
Property Value
| Type | Description |
|---|---|
| ILambdaJsonSerializer | The ILambdaJsonSerializer instance. |
Methods
Abort(APIGatewayProxyResponse)
The Abort(APIGatewayProxyResponse) method stops the request processing and sets the specified Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance as the response.
Declaration
protected virtual Exception Abort(APIGatewayProxyResponse response)
Parameters
| Type | Name | Description |
|---|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse | response | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance to use for the response. |
Returns
| Type | Description |
|---|---|
| System.Exception | Nothing. See remarks. |
Remarks
This method never returns as the abort exception is thrown immediately. The System.Exception instance is shown as returned to make it easier to tell the compiler the control flow.
Examples
throw Abort(new APIGatewayProxyResponse {
StatusCode = 404,
Body = "item not found"
});
AbortBadRequest(String)
The AbortBadRequest(String) stops the request processing with a 400 - Bad Request response.
Declaration
protected virtual Exception AbortBadRequest(string message)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | message | The response message. |
Returns
| Type | Description |
|---|---|
| System.Exception | Nothing. See remarks. |
Remarks
This method never returns as the abort exception is thrown immediately. The System.Exception instance is shown as returned to make it easier to tell the compiler the control flow.
Examples
throw AbortBadRequest("invalid selection");
AbortForbidden(String)
The AbortForbidden(String) stops the request processing with a 403 - Forbidden response.
Declaration
protected virtual Exception AbortForbidden(string message)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | message | The response message. |
Returns
| Type | Description |
|---|---|
| System.Exception | Nothing. See remarks. |
Remarks
This method never returns as the abort exception is thrown immediately. The System.Exception instance is shown as returned to make it easier to tell the compiler the control flow.
Examples
throw AbortForbidden("you are not authorized");
AbortNotFound(String)
The AbortNotFound(String) stops the request processing with a 404 - Not Found response.
Declaration
protected virtual Exception AbortNotFound(string message)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | message | The response message. |
Returns
| Type | Description |
|---|---|
| System.Exception | Nothing. See remarks. |
Remarks
This method never returns as the abort exception is thrown immediately. The System.Exception instance is shown as returned to make it easier to tell the compiler the control flow.
Examples
throw AbortNotFound("item not found");
CreateBadParameterResponse(APIGatewayProxyRequest, String, String)
The CreateBadParameterResponse(APIGatewayProxyRequest, String, String) method creates a
Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance to report a 400 - Bad Request error, because
a request parameter was invalid or missing.
Declaration
protected virtual APIGatewayProxyResponse CreateBadParameterResponse(APIGatewayProxyRequest request, string parameterName, string message)
Parameters
| Type | Name | Description |
|---|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest | request | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest instance. |
| System.String | parameterName | The name of the parameter. |
| System.String | message | The error message. |
Returns
| Type | Description |
|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance. |
CreateInvocationExceptionResponse(APIGatewayProxyRequest, Exception)
The CreateInvocationExceptionResponse(APIGatewayProxyRequest, Exception) method creates a
Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance to report a 500 - Internal Server Error error, because
an unhandled exception occurred during the request processing.
Declaration
protected virtual APIGatewayProxyResponse CreateInvocationExceptionResponse(APIGatewayProxyRequest request, Exception exception)
Parameters
| Type | Name | Description |
|---|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest | request | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest instance. |
| System.Exception | exception | The raised exception. |
Returns
| Type | Description |
|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance. |
CreateInvocationTargetInstance(Type)
The CreateInvocationTargetInstance(Type) method is invoked to create instances for mapped method invocations. This is invoked only once per requested type.
Declaration
public virtual object CreateInvocationTargetInstance(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The requested type to instantiate. |
Returns
| Type | Description |
|---|---|
| System.Object | The instance for the target method. |
Remarks
If the requested type is the type of the Lambda function, this method returns the Lambda function instance. Otherwise, it attempts to instantiate the requested type using that Lambda function instance as the sole the constructor parmeter. This pattern allows the Lambda function to be the Dependency Provider for the created instance.
CreateResponse(Int32, String)
The CreateResponse(Int32, String) method creates a Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance set to the specified status code and message in the response body.
Declaration
protected virtual APIGatewayProxyResponse CreateResponse(int statusCode, string message)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | statusCode | The HTTP status code of the response. |
| System.String | message | The response mesage. |
Returns
| Type | Description |
|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance. |
Examples
For a REST API route, the response body looks as follows.
{
"message": "item not found",
"requestId": "123abc"
}
For a WebSocket route, the response body contains in addition the connection ID.
{
"message": "item not found",
"requestId": "123abc",
"connectionId": "456def"
}
CreateRouteNotFoundResponse(APIGatewayProxyRequest, String)
The CreateRouteNotFoundResponse(APIGatewayProxyRequest, String) method creates the
Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance to report a 404 - Not Found error for the requested
REST API or WebSocket route.
Declaration
protected virtual APIGatewayProxyResponse CreateRouteNotFoundResponse(APIGatewayProxyRequest request, string route)
Parameters
| Type | Name | Description |
|---|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest | request | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest instance. |
| System.String | route | The REST API or WebSocket route that could not be found. |
Returns
| Type | Description |
|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse instance. |
InitializeEpilogueAsync()
The InitializeEpilogueAsync() method is invoked to complet the initialization of the Lambda function. This is the last of three methods that are invoked to initialize the Lambda function.
Declaration
protected override async Task InitializeEpilogueAsync()
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task | The task object representing the asynchronous operation. |
Overrides
InitializePrologueAsync(ILambdaConfigSource)
The InitializePrologueAsync(ILambdaConfigSource) method is invoked to prepare the Lambda function for initialization. This is the first of three methods that are invoked to initialize the Lambda function.
Declaration
protected override async Task InitializePrologueAsync(ILambdaConfigSource envSource)
Parameters
| Type | Name | Description |
|---|---|---|
| ILambdaConfigSource | envSource | The ILambdaConfigSource instance from which to read the configuration settings. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task | The task object representing the asynchronous operation. |
Overrides
ProcessMessageAsync(APIGatewayProxyRequest)
The ProcessMessageAsync(APIGatewayProxyRequest) method provides specific behavior for this base class.
Declaration
public async Task<APIGatewayProxyResponse> ProcessMessageAsync(APIGatewayProxyRequest request)
Parameters
| Type | Name | Description |
|---|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest | request | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest instance. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse> | The task object representing the asynchronous operation. |
ProcessMessageStreamAsync(Stream)
The ProcessMessageStreamAsync(Stream) deserializes the request stream into a Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest instance and invokes the ProcessMessageAsync(APIGatewayProxyRequest) method.
Declaration
public sealed override async Task<Stream> ProcessMessageStreamAsync(Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | stream | The stream with the request payload. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.IO.Stream> | The task object representing the asynchronous operation. |
Overrides
Remarks
This method is sealed and cannot be overridden.
ProcessProxyRequestAsync(APIGatewayProxyRequest)
The ProcessProxyRequestAsync(APIGatewayProxyRequest) method is invoked when the received request is not mapped to a method invocation.
Declaration
public virtual Task<APIGatewayProxyResponse> ProcessProxyRequestAsync(APIGatewayProxyRequest request)
Parameters
| Type | Name | Description |
|---|---|---|
| Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest | request | The Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest instance. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse> | The task object representing the asynchronous operation. |
Remarks
The default implementation response with a 404 error.