นโยบาย PythonScript

คุณกำลังดูเอกสารประกอบ Apigee Edge
ไปที่ เอกสารประกอบเกี่ยวกับ Apigee X.
ข้อมูล

อะไร

นโยบายสคริปต์ Python ช่วยให้คุณเพิ่มฟังก์ชันการทำงานของ Python ที่กำหนดเองลงในโฟลว์พร็อกซี API ได้ โดยเฉพาะอย่างยิ่งเมื่อฟังก์ชันการทำงานที่คุณต้องการนั้นไม่ใช่สิ่งที่นโยบายสำเร็จรูปของ Edge ให้ไว้

การสนับสนุนภาษา Python มีให้บริการผ่าน Jython เวอร์ชัน 2.5.2 ไลบรารีของบุคคลที่สามที่คุณเพิ่มต้องเป็น "Python ที่แท้จริง" (ใช้ใน Python เท่านั้น) ดูข้อมูลเพิ่มเติมเกี่ยวกับการเพิ่มไลบรารีได้ที่ไฟล์ทรัพยากร

นโยบาย Python ไม่มีโค้ดจริง แต่นโยบาย Python อ้างอิง Python แทน และกำหนดขั้นตอนในโฟลว์ API ที่สคริปต์ Python จะทำงาน คุณสามารถอัปโหลด สคริปต์ของคุณผ่านตัวแก้ไขพร็อกซี UI การจัดการ หรือคุณสามารถรวมสคริปต์นั้นไว้ใน ไดเรกทอรี /resources/py ในพร็อกซี API ที่คุณพัฒนาในเครื่อง

ตัวอย่าง

นโยบาย 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> เพื่อติดป้ายกำกับนโยบายใน เครื่องมือแก้ไขพร็อกซี UI การจัดการด้วยชื่อที่เป็นภาษาธรรมชาติต่างกัน

ไม่มี ต้องระบุ
continueOnError

ตั้งค่าเป็น false เพื่อแสดงผลข้อผิดพลาดเมื่อนโยบายล้มเหลว เป็นเรื่องปกติ พฤติกรรมสำหรับนโยบายส่วนใหญ่

ตั้งค่าเป็น true เพื่อให้ดำเนินการตามขั้นตอนได้อย่างต่อเนื่องแม้จะมีนโยบายแล้วก็ตาม ล้มเหลว

เท็จ ไม่บังคับ
enabled

ตั้งค่าเป็น true เพื่อบังคับใช้นโยบาย

ตั้งค่าเป็น false เพื่อปิดนโยบาย นโยบายจะไม่ บังคับใช้ แม้ว่าจะยังคงแนบกับขั้นตอน

จริง ไม่บังคับ
async

แอตทริบิวต์นี้เลิกใช้งานแล้ว

เท็จ เลิกใช้

&lt;DisplayName&gt; องค์ประกอบ

ใช้เพิ่มเติมจากแอตทริบิวต์ name เพื่อติดป้ายกำกับนโยบายใน เครื่องมือแก้ไขพร็อกซี UI การจัดการด้วยชื่อที่เป็นภาษาธรรมชาติต่างกัน

<DisplayName>Policy Display Name</DisplayName>
ค่าเริ่มต้น

ไม่มี

หากไม่ใส่องค์ประกอบนี้ ค่าของแอตทริบิวต์ name ของนโยบายจะเป็น

การมีบุคคลอยู่ ไม่บังคับ
ประเภท สตริง

&lt;ResourceURL&gt; องค์ประกอบ

องค์ประกอบนี้ระบุไฟล์ Python หลักที่จะดำเนินการในโฟลว์ API คุณสามารถเก็บ ไฟล์นี้ที่ขอบเขตพร็อกซี API (ต่ำกว่า /apiproxy/resources/py ในพร็อกซี API หรือในส่วน Scripts ของแผง Navigator ของเครื่องมือแก้ไขพร็อกซี API) หรือที่ ขอบเขตขององค์กรหรือสภาพแวดล้อมเพื่อนำมาใช้ซ้ำในพร็อกซี API หลายรายการ ตามที่อธิบายไว้ใน ไฟล์ทรัพยากร โค้ดสามารถใช้ ออบเจ็กต์ เมธอด และพร็อพเพอร์ตี้ของโมเดลออบเจ็กต์ JavaScript

<ResourceURL>py://myscript.py</ResourceURL>
ค่าเริ่มต้น: ไม่มี
การตรวจหาบุคคล: ต้องระบุ
ประเภท: สตริง

&lt;IncludeURL&gt; องค์ประกอบ

ระบุไฟล์ Python ที่จะโหลดเป็นทรัพยากร Dependency ไปยังไฟล์ Python หลักที่ระบุด้วย องค์ประกอบ <ResourceURL> สคริปต์จะได้รับการประเมินตามลำดับ แสดงอยู่ในนโยบาย

รวมทรัพยากร Dependency ของ Python มากกว่า 1 รายการพร้อมด้วยทรัพยากรเพิ่มเติม องค์ประกอบ <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>

หัวข้อที่เกี่ยวข้อง