-
Notifications
You must be signed in to change notification settings - Fork 64
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
[FEATURE] open-api-gateway - allow all operations to use the same lambda integration #238
Comments
…unction for all operations (ts) The generated Typescript API client now includes a 'handlerRouter' which can be used to route requests to the appropriate handler if users desire to deploy a single shared lambda for all API integrations. This is type-safe, ensuring all operations are mapped to a handler produced by the appropriate handler wrapper method. Additionally we supply a convenience method 'Operations.all' to make it easy to share the same integration for all operations. re #238
I've raised a PR for this with only TypeScript support for now. Rather than opt for a single import { Operations } from 'my-api-typescript-client';
export class SampleApi extends Api {
constructor(scope: Construct, id: string) {
super(scope, id, {
defaultAuthorizer: Authorizers.iam(),
integrations: Operations.all({
integration: Integrations.lambda(new NodejsFunction(scope, 'apiHandlerFunction')),
})
});
}
} In your lambda function, you'd export the import { handlerRouter, sayHelloHandler, sayGoodbyeHandler } from "my-api-typescript-client";
import { corsInterceptor } from "./interceptors";
import { sayGoodbye } from "./handlers/say-goodbye";
const sayHello = sayHelloHandler(async ({ input }) => {
return {
statusCode: 200,
body: {
message: `Hello ${input.requestParameters.name}!`,
},
};
});
export const handler = handlerRouter({
// Interceptors declared in this list will apply to all operations
interceptors: [corsInterceptor],
// Assign handlers to each operation here
handlers: {
sayHello,
sayGoodbye,
},
}); You get a type error if you forget to route an operation: Or if you accidentally assign the wrong handler to an operation: |
…unction for all operations The generated API client now includes a 'handlerRouter' which can be used to route requests to the appropriate handler if users desire to deploy a single shared lambda for all API integrations. In TypeScript, this is type-safe, ensuring all operations are mapped to a handler produced by the appropriate handler wrapper method. Additionally we supply a convenience method 'Operations.all' to make it easy to share the same integration for all operations. This change includes support for TypeScript and Python - Java coming soon! re #238
This is fantastic. Thanks for the speedy response and implementation.
|
…unction for all operations (#240) The generated API client now includes a 'handlerRouter' which can be used to route requests to the appropriate handler if users desire to deploy a single shared lambda for all API integrations. In TypeScript, this is type-safe, ensuring all operations are mapped to a handler produced by the appropriate handler wrapper method. Additionally we supply a convenience method 'Operations.all' to make it easy to share the same integration for all operations. This change includes support for TypeScript and Python - Java coming soon! re #238
…da function for all operations Adds a HandlerRouter class to the generated Java client which can be extended and used as the lambda entrypoint for all operations. The router requires you to define a method which returns a handler for every operation. The router can also be decorated with interceptors to apply to all operations. fix #238
…da function for all operations (#242) Adds a HandlerRouter class to the generated Java client which can be extended and used as the lambda entrypoint for all operations. The router requires you to define a method which returns a handler for every operation. The router can also be decorated with interceptors to apply to all operations. fix #238
Python and Java are now also supported! For completeness, here's how one might use them: Python
self.api = Api(self, 'Api',
default_authorizer=Authorizers.iam(),
integrations=Operations.all(
OpenApiIntegration(
integration=Integrations.lambda_(Function(...))
),
),
) And the from myapi_python.apis.tags.default_api_operation_config import say_hello_handler, SayHelloRequest, ApiResponse, SayHelloOperationResponses, handler_router, HandlerRouterHandlers
from myapi_python.model.api_error import ApiError
from myapi_python.model.hello_response import HelloResponse
from other_handlers import say_goodbye
from my_interceptors import cors_interceptor
@say_hello_handler
def say_hello(input: SayHelloRequest, **kwargs) -> SayHelloOperationResponses:
return ApiResponse(
status_code=200,
body=HelloResponse(message="Hello {}!".format(input.request_parameters["name"])),
headers={}
)
handler = handler_router(
# Interceptors defined here will apply to all operations
interceptors=[cors_interceptor],
handlers=HandlerRouterHandlers(
say_hello=say_hello,
say_goodbye=say_goodbye
)
) Java
public class SampleApi extends Api {
public SampleApi(Construct scope, String id) {
super(scope, id, ApiProps.builder()
.defaultAuthorizer(Authorizers.iam())
.corsOptions(CorsOptions.builder()
.allowOrigins(Arrays.asList("*"))
.allowMethods(Arrays.asList("*"))
.build())
.integrations(Operations.all(
OpenApiIntegration.builder()
.integration(Integrations.lambda(
new Function(...)
))
.build())
.build())
.build());
}
} You can set the lambda entrypoint to a class which extends the import com.generated.api.myapijava.client.api.Handlers.SayGoodbye;
import com.generated.api.myapijava.client.api.Handlers.HandlerRouter;
import com.generated.api.myapijava.client.api.Handlers.Interceptors;
import com.generated.api.myapijava.client.api.Handlers.SayHello;
import java.util.Arrays;
import java.util.List;
// Interceptors defined here apply to all operations
@Interceptors({ TimingInterceptor.class })
public class ApiHandlerRouter extends HandlerRouter {
// You must implement a method to return a handler for every operation
@Override
public SayHello sayHello() {
return new SayHelloHandler();
}
@Override
public SayGoodbye sayGoodbye() {
return new SayGoodbyeHandler();
}
} Versions
|
Describe the feature
Today an
Api
requires a lambda function and associated integration for each operation. This proposed feature should allow a developer to send all operations to a single lambda integration.Use Case
As a developer
Api
operations.Proposed Solution
The text was updated successfully, but these errors were encountered: