Kebijakan PythonScript

Halaman ini berlaku untuk Apigee dan Apigee hybrid.

Baca dokumentasi Apigee Edge.

ikon kebijakan

Apa

Kebijakan Skrip Python memungkinkan Anda menambahkan fungsi Python yang disesuaikan ke alur proxy API, terutama jika fungsi yang Anda butuhkan melebihi yang disediakan kebijakan siap pakai Apigee.

Kebijakan ini adalah Kebijakan yang dapat diperluas dan penggunaan kebijakan ini mungkin memiliki implikasi biaya atau penggunaan, bergantung pada lisensi Apigee Anda. Untuk informasi tentang jenis kebijakan dan implikasi penggunaan, lihat Jenis kebijakan.

Dukungan bahasa Python disediakan melalui Jython versi 2.5.2. Library pihak ketiga yang Anda tambahkan harus "Python murni" (hanya diterapkan di Python). Untuk mengetahui informasi selengkapnya tentang cara menambahkan library, lihat File resource.

Kebijakan Python tidak berisi kode sebenarnya. Sebagai gantinya, kebijakan Python mereferensikan resource Python dan menentukan Langkah dalam alur API tempat skrip Python dijalankan. Anda dapat mengupload skrip melalui editor proxy UI Apigee, atau menyertakannya dalam direktori /resources/py di proxy API yang Anda kembangkan secara lokal.

Sampel

Kebijakan & skrip Python

Kebijakan Skrip Python

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

Dalam contoh ini, elemen ResourceURL menentukan resource skrip Python yang relevan.

Skrip Python

Ini menunjukkan apa yang dapat Anda sertakan dalam skrip Python itu sendiri.

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)

Referensi elemen

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

Tabel berikut menjelaskan atribut yang umum untuk semua elemen induk kebijakan:

Atribut Deskripsi Default Kehadiran
name

Nama internal kebijakan. Nilai atribut name dapat berisi huruf, angka, spasi, tanda hubung, garis bawah, dan titik. Nilai ini tidak boleh melebihi 255 karakter.

Secara opsional, gunakan elemen <DisplayName> untuk memberi label pada kebijakan di editor proxy UI pengelolaan dengan nama bahasa alami yang berbeda.

T/A Wajib
continueOnError

Tetapkan ke false untuk menampilkan error saat kebijakan gagal. Perilaku ini wajar terjadi untuk sebagian besar kebijakan.

Tetapkan ke true agar eksekusi alur berlanjut meskipun setelah kebijakan gagal. Lihat juga:

false Opsional
enabled

Tetapkan ke true untuk menerapkan kebijakan.

Tetapkan ke false untuk menonaktifkan kebijakan. Kebijakan tidak akan diterapkan meskipun tetap terlampir ke alur.

benar Opsional
async

Atribut ini tidak digunakan lagi.

false Tidak digunakan lagi

Elemen <DisplayName>

Gunakan selain atribut name untuk melabeli kebijakan di editor proxy UI pengelolaan dengan nama bahasa alami yang berbeda.

<DisplayName>Policy Display Name</DisplayName>
Default

T/A

Jika Anda menghapus elemen ini, nilai atribut name kebijakan akan digunakan.

Kehadiran Opsional
Jenis String

Elemen <ResourceURL>

Elemen ini menentukan file Python utama yang akan dieksekusi dalam alur API. Anda dapat menyimpan file ini di cakupan proxy API (di bagian /apiproxy/resources/py dalam paket proxy API atau di bagian Skrip pada panel Navigator editor proxy API), atau di cakupan organisasi atau lingkungan untuk digunakan kembali di beberapa proxy API, seperti yang dijelaskan dalam File resource. Kode Anda dapat menggunakan objek, metode, dan properti model objek JavaScript.

<ResourceURL>py://myscript.py</ResourceURL>
Default: Tidak ada
Kehadiran: Wajib
Jenis: String

Elemen <IncludeURL>

Menentukan file Python yang akan dimuat sebagai dependensi ke file Python utama yang ditentukan dengan elemen <ResourceURL>. Skrip akan dievaluasi sesuai urutan pencantuman dalam kebijakan.

Sertakan lebih dari satu resource dependensi Python dengan elemen <IncludeURL> tambahan.

<IncludeURL>py://myscript_dependency.py</IncludeURL>
Default: Tidak ada
Kehadiran: Opsional
Jenis: String

Kode error

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>