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>
預設值:
外觀狀態: 選用
類型: 字串

錯誤代碼

This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.

Runtime errors

These errors can occur when the policy executes.

Fault code HTTP status Cause Fix
steps.script.ScriptEvaluationFailed 500 The PythonScript policy can throw several different types of ScriptExecutionFailed errors. Commonly seen types of errors include NameError and ZeroDivisionError.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name Cause Fix
InvalidResourceUrlFormat If the format of the resource URL specified within the <ResourceURL> or the <IncludeURL> element of the PythonScript policy is invalid, then the deployment of the API proxy fails.
InvalidResourceUrlReference If the <ResourceURL> or the <IncludeURL> elements refer to a PythonScript file that does not exist, then the deployment of the API proxy fails. The referenced source file must exist either the API proxy, environment, or organization level.

Fault variables

These variables are set when this policy triggers an error at runtime. For more information, see What you need to know about policy errors.

Variables Where Example
fault.name="fault_name" fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. fault.name Matches "ScriptExecutionFailed"
pythonscript.policy_name.failed policy_name is the user-specified name of the policy that threw the fault. pythonscript.PythonScript-1.failed = true

Example error response

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

Example fault rule

<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>