PythonScript 政策

本页面适用于 ApigeeApigee Hybrid

查看 Apigee Edge 文档。

政策图标

是什么

通过 Python 脚本政策,您可以向 API 代理流添加自定义 Python 功能,尤其是当您需要的功能超出了 Apigee 开箱即用的政策提供的范围时。

此政策是一项可扩展政策,使用此政策可能会影响费用或使用情况,具体取决于您的 Apigee 许可。如需了解政策类型和使用情况影响,请参阅政策类型

Python 语言支持通过 Jython 2.5.2 版提供。您添加的第三方库必须为“纯 Python”(即仅用 Python 实现的库)。 如需详细了解如何添加库,请参阅资源文件

Python 政策不包含实际代码。相反,Python 政策会引用 Python 资源,并在执行 Python 脚本的 API 流中定义步骤。您可以通过 Apigee 界面代理编辑器上传脚本,也可以在本地开发的 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 代理范围(API 代理软件包的 /apiproxy/resources/py 下方或 API 代理编辑器导航工具窗格的“脚本”部分中)或组织或环境范围中存储此文件,以便在多个 API 代理中重复使用,如资源文件中所述。您的代码可以使用 JavaScript 对象模型的对象、方法和属性。

<ResourceURL>py://myscript.py</ResourceURL>
默认:
状态: 必需
类型: 字符串

<IncludeURL> 元素

指定要作为使用 <ResourceURL> 元素指定的主要 Python 文件的依赖项加载的 Python 库文件。脚本将按政策中的列出顺序进行评估。

使用附加 <IncludeURL> 元素添加多个 Python 依赖项资源。

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