PythonScript 政策

本頁內容適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

政策圖示

結果

您可以使用 Python 指令碼政策,在 API Proxy 流程中新增自訂 Python 功能,特別是當您需要的功能超出 Apigee 立即可用政策的範圍時。

這項政策是可擴充政策,使用這項政策可能會產生費用或影響用量,具體情況取決於您的 Apigee 授權。如要瞭解政策類型和使用方式的影響,請參閱「政策類型」。

Python 語言支援是透過 Jython 2.5.2 版提供。您新增的第三方程式庫必須是「純 Python」(僅以 Python 實作)。如要進一步瞭解如何新增程式庫,請參閱「資源檔案」。

Python 政策不含實際程式碼,Python 政策會參照 Python 資源,並定義 Python 指令碼執行的 API 流程步驟。您可以透過 Apigee UI 代理伺服器編輯器上傳指令碼,也可以將指令碼納入您在本機開發的 API 代理伺服器 /resources/py 目錄中。

範例

Python 政策和指令碼

Python 指令碼政策

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Script name="Python-1">
        <DisplayName>Python-1</DisplayName>
        <ResourceURL>py://myscript.py</ResourceURL>
</Script>

在本例中,ResourceURL 元素會指定相關的 Python 指令碼資源。

Python 指令碼

這會顯示您可能在 Python 指令碼中加入的內容。

import base64

username = flow.getVariable("request.formparam.client_id")
password = flow.getVariable("request.formparam.client_secret")

base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authorization = "Basic "+base64string

flow.setVariable("authorizationParam",authorization)

元素參考資料

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Script name="Python-1">
    <DisplayName>Python-1</DisplayName>
    <ResourceURL>py://myscript.py</ResourceURL>
    <IncludeURL>py://myscript_dependency.py</IncludeURL>
</Script>

The following table describes attributes that are common to all policy parent elements:

Attribute Description Default Presence
name

The internal name of the policy. The value of the name attribute can contain letters, numbers, spaces, hyphens, underscores, and periods. This value cannot exceed 255 characters.

Optionally, use the <DisplayName> element to label the policy in the management UI proxy editor with a different, natural-language name.

N/A Required
continueOnError

Set to false to return an error when a policy fails. This is expected behavior for most policies.

Set to true to have flow execution continue even after a policy fails. See also:

false Optional
enabled

Set to true to enforce the policy.

Set to false to turn off the policy. The policy will not be enforced even if it remains attached to a flow.

true Optional
async

This attribute is deprecated.

false Deprecated

<DisplayName> element

Use in addition to the name attribute to label the policy in the management UI proxy editor with a different, natural-language name.

<DisplayName>Policy Display Name</DisplayName>
Default

N/A

If you omit this element, the value of the policy's name attribute is used.

Presence Optional
Type String

<ResourceURL> 元素

這個元素會指定在 API 流程中執行的主要 Python 檔案。您可以將這個檔案儲存在 API Proxy 範圍 (位於 API Proxy 套件的 /apiproxy/resources/py 下方,或 API Proxy 編輯器的「Navigator」窗格的「Scripts」部分),也可以儲存在機構或環境範圍,以便在多個 API Proxy 中重複使用,詳情請參閱「資源檔案」。您的程式碼可以使用 JavaScript 物件模型的物件、方法和屬性。

<ResourceURL>py://myscript.py</ResourceURL>
預設值:
外觀狀態: 必填
類型: 字串

<IncludeURL> 元素

指定要載入的 Python 檔案,做為以 <ResourceURL> 元素指定的主要 Python 檔案的依附元件。系統會按照政策中列出的順序評估指令碼。

加入多個 Python 依附元件資源,並包含額外的 <IncludeURL> 元素。

<IncludeURL>py://myscript_dependency.py</IncludeURL>
預設值:
外觀狀態: 選用
類型: 字串

錯誤代碼

本節說明這項政策觸發錯誤時,Apigee 傳回的錯誤代碼和錯誤訊息,以及 Apigee 設定的錯誤變數。如果您要開發錯誤處理規則,就必須瞭解這項資訊。如需更多資訊,請參閱「關於政策錯誤的相關資訊」和「處理錯誤」。

執行階段錯誤

政策執行時可能會發生這些錯誤。

錯誤代碼 HTTP 狀態 原因 修正
steps.script.ScriptEvaluationFailed 500 PythonScript 政策可能會擲回幾種不同類型的 ScriptExecutionFailed 錯誤。常見的錯誤類型包括 NameErrorZeroDivisionError

部署錯誤

部署含有這項政策的 Proxy 時,可能會發生這些錯誤。

錯誤名稱 原因 修正
InvalidResourceUrlFormat 如果 <ResourceURL> 中指定的資源網址格式或 PythonScript 政策的 <IncludeURL> 元素無效,API Proxy 的部署作業就會失敗。
InvalidResourceUrlReference 如果 <ResourceURL><IncludeURL> 元素參照不存在的 PythonScript 檔案,API 代理程式會無法部署。參照的來源檔案必須位於 API 代理程式、環境或機構層級。

錯誤變數

當這項政策在執行階段觸發錯誤時,系統就會設定這些變數。詳情請參閱「關於政策錯誤的相關資訊」。

變數 地點 範例
fault.name="fault_name" fault_name 是錯誤名稱,如上方「執行階段錯誤」表格所列。錯誤名稱是錯誤代碼的最後一個部分。 fault.name Matches "ScriptExecutionFailed"
pythonscript.policy_name.failed policy_name 是擲回錯誤的政策的使用者指定名稱。 pythonscript.PythonScript-1.failed = true

錯誤回應範例

{
  "fault": {
    "faultstring": "Execution of SetResponse failed with error: Pythonscript runtime error: "ReferenceError: "status" is not defined.\"",
    "detail": {
      "errorcode": "steps.script.ScriptExecutionFailed"
    }
  }
}

錯誤規則範例

<FaultRule name="PythonScript Policy Faults">
    <Step>
        <Name>AM-CustomErrorResponse</Name>
        <Condition>(fault.name Matches "ScriptExecutionFailed") </Condition>
    </Step>
    <Condition>(pythonscript.PythonScript-1.failed = true) </Condition>
</FaultRule>