您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件。 info
結果
您可以使用 Python 指令碼政策,在 API Proxy 流程中新增自訂 Python 功能,特別是當您需要的功能超出 Edge 隨附政策的範圍時。
系統會透過 Jython 2.5.2 版提供 Python 語言支援。您新增的第三方程式庫必須是「純 Python」(僅以 Python 實作)。如要進一步瞭解如何新增程式庫,請參閱「資源檔案」。
Python 政策不含實際程式碼。Python 政策會參照 Python 資源,並定義 Python 指令碼執行的 API 流程步驟。您可以透過管理 UI Proxy 編輯器上傳指令碼,也可以將指令碼納入您在本機開發的 API Proxy /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>
下表說明所有政策父項元素的共同屬性:
| 屬性 | 說明 | 預設 | 存在必要性 |
|---|---|---|---|
name |
政策的內部名稱。 視需要使用 |
不適用 | 必填 |
continueOnError |
如果設為「 如果設為 |
false | 選用 |
enabled |
如要強制執行政策,請設為 設為 |
true | 選用 |
async |
此屬性已淘汰。 |
false | 已淘汰 |
<DisplayName>元素
除 name 屬性外,一併使用
管理 UI Proxy 編輯器,使用不同的自然語言名稱。
<DisplayName>Policy Display Name</DisplayName>
| 預設 |
不適用 如果省略這個元素,政策的 |
|---|---|
| 存在必要性 | 選用 |
| 類型 | 字串 |
<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 Edge 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. | build |
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. |
build |
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. |
build |
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>