我想在AWS CDK API Gateway定义中重用集成uri的动态资源值。
假设我有两个服务:
service-footballservice-tennis两者都有一个端点"播放器"
现在我想为足球运动员和网球运动员定义一个api网关。我尝试这样动态地定义它:
endpt = rest_api.root.add_resource(path_part='endpoint')
sport_endpt = endpt.add_resource("{sport}")
players_endpt = sport_endpt.add_resource("players")
players_endpt.add_method(
http_method='GET',
...
integration=apigw.Integration(
type=_apigw.IntegrationType.HTTP,
integration_http_method='GET',
uri=uri + '/service-*HERE_THE_SPORT_PARAM*/players',
)
)
在集成部分,我想用动态资源值{sport}动态地创建uri部分,这样我就可以得到/service-football/players或/service-tennis/players这样的东西,我可以通过创建具有相同约定的服务端点来轻松地扩展它们。
如果您想要一个单一的API端点定义,您需要将sport包含为路径参数,因此它可能是BASE_URI/service/{sport}/players
。
对应的cdk将是
endpt = rest_api.root.add_resource(path_part='endpoint')
service_endpt = endpt.add_resource(path_part='service')
sport_endpt = service_endpt.add_resource("{sport}")
players_endpt = sport_endpt.add_resource("players")
players_endpt.add_method(
http_method='GET',
...
integration=apigw.Integration(
type=_apigw.IntegrationType.HTTP,
integration_http_method='GET',
uri=uri + '/service/{sport}/players',
)
)