【问题标题】:AWS API Gateway: Use dynamic part of resource in integration uriAWS API Gateway:在集成 uri 中使用资源的动态部分
【发布时间】:2021-12-14 14:19:00
【问题描述】:

我想在 AWS CDK API 网关定义中为集成 uri 重新使用动态资源值。

假设我有两个服务:

服务足球 服务网球 两者都有一个端点“玩家”。

现在我想要为足球运动员和网球运动员提供一个单一的 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 之类的东西,我只需通过创建具有相同约定的服务端点。

【问题讨论】:

    标签: dynamic aws-api-gateway aws-cdk api-gateway


    【解决方案1】:

    如果您想要单个 API 端点定义,则需要将运动作为路径参数包含在内,因此类似于 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',
        )
    )
    

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 2020-08-21
      • 2018-05-25
      • 2019-06-09
      • 2021-05-29
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多