Questa pagina si applica ad Apigee e Apigee hybrid.
Visualizza la documentazione di
Apigee Edge.
I criteri per lo script Python ti consentono di aggiungere funzionalità Python personalizzate al flusso del proxy API, soprattutto quando la funzionalità di cui hai bisogno va oltre ciò che forniscono i criteri predefiniti di Apigee.
Queste norme sono estendibili e il loro utilizzo potrebbe avere implicazioni in termini di costi o utilizzo, a seconda della licenza Apigee. Per informazioni sui tipi di policy e sulle implicazioni di utilizzo, consulta Tipi di policy.
Il supporto del linguaggio Python è fornito tramite Jython versione 2.5.2. Le librerie di terze parti che aggiungi devono essere "pure Python" (implementate solo in Python). Per saperne di più sull'aggiunta di librerie, consulta File di risorse.
Un criterio Python non contiene codice effettivo. Un criterio Python fa invece riferimento a una risorsa Python e definisce il passaggio nel flusso API in cui viene eseguito lo script Python. Puoi caricare
lo script tramite l'editor proxy dell'interfaccia utente Apigee oppure puoi includerlo nella
directory /resources/py nei proxy API che sviluppi localmente.
Esempi
Norme e script Python
Norme relative agli script Python
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Script name="Python-1"> <DisplayName>Python-1</DisplayName> <ResourceURL>py://myscript.py</ResourceURL> </Script>
In questo esempio, l'elemento ResourceURL specifica la risorsa dello script Python pertinente.
Script Python
Mostra cosa potresti includere nello script Python stesso.
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)
Riferimento elemento
<?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>
La tabella seguente descrive gli attributi comuni a tutti gli elementi principali dei criteri:
| Attributo | Descrizione | Predefinito | Presence |
|---|---|---|---|
name |
Il nome interno del criterio. Il valore dell'attributo Se vuoi, utilizza l'elemento |
N/D | Obbligatorio |
continueOnError |
Imposta su Imposta su |
falso | Facoltativo |
enabled |
Imposta su Imposta su |
true | Facoltativo |
async |
Questo attributo è stato ritirato. |
falso | Deprecato |
Elemento <DisplayName>
Da utilizzare insieme all'attributo name per etichettare il criterio nell'editor proxy dell'interfaccia utente di gestione con un nome diverso in linguaggio naturale.
<DisplayName>Policy Display Name</DisplayName>
| Predefinito |
N/D Se ometti questo elemento, viene utilizzato il valore dell'attributo |
|---|---|
| Presence | Facoltativo |
| Tipo | Stringa |
Elemento<ResourceURL>
Questo elemento specifica il file Python principale che verrà eseguito nel flusso API. Puoi archiviare
questo file nell'ambito del proxy API (in /apiproxy/resources/py nel bundle del proxy API o nella sezione Script del riquadro di navigazione dell'editor del proxy API) oppure negli ambiti dell'organizzazione o dell'ambiente per il riutilizzo in più proxy API, come descritto in
File di risorse. Il codice può utilizzare gli oggetti, i metodi e le proprietà del modello oggetto JavaScript.
<ResourceURL>py://myscript.py</ResourceURL>
| Valore predefinito: | Nessuno |
| Presenza: | Obbligatorio |
| Tipo: | Stringa |
Elemento<IncludeURL>
Specifica un file Python da caricare come dipendenza del file Python principale specificato con l'elemento <ResourceURL>. Gli script verranno valutati nell'ordine in cui
sono elencati nel criterio.
Includi più di una risorsa di dipendenza Python con elementi <IncludeURL> aggiuntivi.
<IncludeURL>py://myscript_dependency.py</IncludeURL>
| Valore predefinito: | Nessuno |
| Presenza: | Facoltativo |
| Tipo: | Stringa |
Codici di errore
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. | build |
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. |
build |
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. |
build |
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>