מדיניות PythonScript

מדיניות ניתנת להרחבה

הדף הזה רלוונטי ל-Apigee ול-Apigee Hybrid.

לעיון במסמכי התיעוד של Apigee Edge

מדיניות Python Script מאפשרת להוסיף פונקציונליות מותאמת אישית של Python לזרימת ה-proxy ל-API, במיוחד אם הפונקציונליות שאתם צריכים חורגת מהפונקציונליות שמספקות מדיניות Apigee מוכנה מראש.

המדיניות הזו היא מדיניות שניתנת להרחבה, והשימוש בה עשוי להשפיע על העלויות או על הניצול, בהתאם לרישיון שלכם ל-Apigee. למידע על סוגי מדיניות והשלכות השימוש, אפשר לעיין במאמר בנושא סוגי מדיניות.

התמיכה בשפת Python ניתנת דרך Jython בגרסה 2.5.2. ספריות של צד שלישי שמוסיפים צריכות להיות 'Python טהור' (כלומר, מיושמות רק ב-Python). מידע נוסף על הוספת ספריות זמין במאמר קבצי משאבים.

מדיניות Python לא מכילה קוד בפועל. במקום זאת, מדיניות Python מפנה למשאב Python ומגדירה את השלב בתהליך העבודה של ה-API שבו מופעל סקריפט Python. אפשר להעלות את הסקריפט דרך כלי העריכה של ה-proxy בממשק המשתמש של Apigee, או לכלול אותו בספרייה /resources/py ב-API proxies שאתם מפתחים באופן מקומי.

דוגמאות

מדיניות וסקריפט של 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

政策的内部名称。name 属性的值可以包含字母、数字、空格、连字符、下划线和英文句点。此值不能超过 255 个字符。

(可选)使用 <DisplayName> 元素在管理界面代理编辑器中给政策添加不同的自然语言名称标签。

不适用 需要
continueOnError

设置为 false 可在政策失败时返回错误。这是大多数政策的预期行为。

设置为 true,即使在政策失败后,仍可以继续执行流。另请参阅:

false 可选
enabled

设置为 true 可强制执行政策。

设为 false关闭政策。即使政策仍附加到某个流,也不会强制执行该政策。

true 可选
async

此特性已弃用。

false 已弃用

<DisplayName> 元素

用于在 name 属性之外在管理界面代理编辑器中给政策添加不同的自然语言名称标签。

<DisplayName>Policy Display Name</DisplayName>
默认

不适用

如果省略此元素,则会使用政策的 name 属性的值。

状态 可选
类型 字符串

רכיב<ResourceURL>

האלמנט הזה מציין את קובץ ה-Python הראשי שיפעל בתהליך של ה-API. אפשר לאחסן את הקובץ הזה בהיקף של שרת ה-proxy ל-API (בקטע /apiproxy/resources/py בחבילת שרת ה-proxy ל-API או בקטע Scripts בחלונית Navigator של כלי העריכה של שרת ה-proxy ל-API), או בהיקף של הארגון או הסביבה לשימוש חוזר בכמה שרתי proxy ל-API, כמו שמתואר בקובצי משאבים. הקוד יכול להשתמש באובייקטים, בשיטות ובמאפיינים של מודל האובייקטים של JavaScript.

<ResourceURL>py://myscript.py</ResourceURL>
ברירת מחדל: ללא
נוכחות: חובה
סוג: String

רכיב<IncludeURL>

מציין קובץ Python שייטען כתלות בקובץ ה-Python הראשי שצוין באמצעות הרכיב <ResourceURL>. הסקריפטים ייבדקו לפי הסדר שבו הם מופיעים במדיניות.

כוללים יותר ממקור תלות אחד של Python עם רכיבי <IncludeURL> נוספים.

<IncludeURL>py://myscript_dependency.py</IncludeURL>
ברירת מחדל: ללא
נוכחות: אופציונלי
סוג: String

קודי שגיאה

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>