خط مشی PythonScript

شما در حال مشاهده اسناد Apigee Edge هستید.
به مستندات Apigee X بروید .
اطلاعات

چی

خط مشی Python Script به شما امکان می دهد عملکرد Python سفارشی شده را به جریان پروکسی API خود اضافه کنید، به خصوص زمانی که عملکرد مورد نیاز شما فراتر از آن چیزی است که خط مشی های خارج از جعبه Edge ارائه می کنند.

پشتیبانی از زبان پایتون از طریق Jython نسخه 2.5.2 ارائه می شود. کتابخانه های شخص ثالثی که اضافه می کنید باید "Pure Python" باشند (فقط در Python پیاده سازی شده است). برای اطلاعات بیشتر در مورد افزودن کتابخانه ها، فایل های منابع را ببینید.

یک خط مشی پایتون حاوی هیچ کد واقعی نیست. در عوض، یک خط مشی پایتون به یک منبع پایتون ارجاع می دهد و مرحله را در جریان API که در آن اسکریپت پایتون اجرا می شود، تعریف می کند. می‌توانید اسکریپت خود را از طریق ویرایشگر پروکسی مدیریت رابط کاربری آپلود کنید، یا می‌توانید آن را در فهرست /resources/py در پراکسی‌های API که به صورت محلی توسعه می‌دهید قرار دهید.

نمونه ها

خط مشی و اسکریپت پایتون

خط مشی اسکریپت پایتون

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Script name="Python-1">
        <DisplayName>Python-1</DisplayName>
        <ResourceURL>py://myscript.py</ResourceURL>
</Script>

در این مثال، عنصر ResourceURL منبع اسکریپت پایتون مربوطه را مشخص می کند.

اسکریپت پایتون

این نشان می دهد که چه چیزی ممکن است در خود اسکریپت پایتون بگنجانید.

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.

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 اجرا می شود. می‌توانید این فایل را در محدوده پروکسی API (در زیر /apiproxy/resources/py در بسته پراکسی API یا در بخش اسکریپت‌ها در پنجره ناوبر ویرایشگر پراکسی API)، یا در محدوده سازمان یا محیط برای استفاده مجدد در چندین پراکسی API ذخیره کنید. همانطور که در فایل های منابع توضیح داده شده است. کد شما می تواند از اشیاء، روش ها و ویژگی های مدل شی جاوا اسکریپت استفاده کند.

<ResourceURL>py://myscript.py</ResourceURL>
پیش فرض: هیچ کدام
حضور: مورد نیاز
نوع: رشته

عنصر <IncludeURL>

یک فایل پایتون را مشخص می کند که به عنوان وابستگی به فایل اصلی پایتون مشخص شده با عنصر <ResourceURL> بارگذاری شود. اسکریپت ها به ترتیبی که در خط مشی فهرست شده اند ارزیابی می شوند.

بیش از یک منبع وابستگی پایتون را با عناصر <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.

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>

موضوعات مرتبط