Azure API 管理引用查询参数

问题描述 投票:0回答:1

我创建了一个Azure API管理服务来连接到Autotask的API。 现在我想查询用户的所有票证,但我必须使用分页检索票证,因为我尝试制作的工具不支持一次那么多的数据。

使用分页时,自动任务 API 会返回一个“nextPageUrl”,您必须在下一个 API 调用中调用它。 因此,我已向 API Mng 添加了一个查询参数,并且在入站策略中我想添加一个策略,将后端 URL 更改为给定的 nextPageUrl(如果给定)。但如果没有给出 URL,它应该只保留当前的 url。

所以这里我有查询参数: enter image description here

这是我在入境政策中添加的部分:

<choose>
        <when condition="@(!string.IsNullOrEmpty(context.Request.Url.Query.GetValueOrDefault('nextPageUrl', '')))">
            <set-backend-service base-url="@{context.Request.Url.Query.GetValueOrDefault('nextPageUrl', '')}" />
        </when>
        <otherwise>
            <set-backend-service base-url="https://webservices.autotask.net/ATServicesRest/v1.0/Tickets/query" />
        </otherwise>
    </choose>

我在这件事上不是专业人士,我尝试用 GPT 和一些研究来解决它,但这似乎并没有像我想要的那样工作。有人知道我如何正确编写该政策吗?

azure azure-api-management
1个回答
0
投票

如果 Autotask API 返回 nextPageUrl 那么您需要将其存储在变量中并使用给定的策略来满足您的要求。

<policies>
    <inbound>
        <base />
        <set-variable name="nextPageUrl" value="{nextPageUrl}" />
        <choose>
            <when condition="@(!string.IsNullOrEmpty((string)context.Variables["nextPageUrl"]))">
                <set-backend-service base-url="@(context.Variables.GetValueOrDefault<string>("nextPageUrl"))" />
            </when>
            <otherwise>
                <set-backend-service base-url="https://webservices.autotask.net/ATServicesRest/v1.0/Tickets/query" />
            </otherwise>
        </choose>
    </inbound>
</policies>

enter image description here enter image description here

如果 Autotask API 包含 nextPageUrl(如

<Autotask API>?nextPageUrl
),那么在这种情况下,您可以使用查询参数从 URL 获取 nextPageUrl 并在您的策略中使用它。

对于查询参数方式,需要传入

nextPageUrl
,如下图

enter image description here

您应该有以下查询参数策略。

<policies>
    <inbound>
        <base />
        <choose>
            <when condition="@(!string.IsNullOrEmpty(context.Request.Url.Query.GetValueOrDefault("nextPageUrl", "")))">
                <set-backend-service base-url="@(context.Request.Url.Query.GetValueOrDefault("nextPageUrl", ""))" />
            </when>
            <otherwise>
                <set-backend-service base-url="https://webservices.autotask.net/ATServicesRest/v1.0/Tickets/query" />
            </otherwise>
        </choose>
    </inbound>
</policies>
© www.soinside.com 2019 - 2024. All rights reserved.