查看 Apigee Edge 說明文件。
前往
Apigee X說明文件。 資訊
結果
Python Script 政策可讓您將自訂的 Python 功能新增至 API Proxy 流程。 尤其是您需要的功能超出 Edge 立即可用的政策時 因此,使用 Kubernetes Engine 即可。
Python 語言支援透過 Jython 提供 2.5.2 版。您新增的第三方程式庫必須是「純 Python」(僅以 Python 實作)。 如要進一步瞭解如何新增程式庫,請參閱「資源檔案」。
Python 政策不含任何實際程式碼。而是會參照 Python 政策
資源,並定義執行 Python 指令碼時在 API 流程中的步驟。您可以上傳
撰寫指令碼的「管理」使用者介面 Proxy 編輯器,或是直接在
/resources/py 目錄 (位於本機開發的 API Proxy 中)。
範例
Python 政策和指令碼
Python Script 政策
<?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> 元素
使用
<ResourceURL> 元素。系統會按照
政策中就會列出幾個符號
包含一個以上的 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>