Diese Seite gilt für Apigee und Apigee Hybrid.
Apigee Edge-Dokumentation aufrufen
Was
Mit der PythonScript-Richtlinie können Sie Ihrem API-Proxy-Ablauf eine benutzerdefinierte Python-Funktion hinzufügen. Das ist besonders wichtig, wenn die benötigte Funktionalität über die von Apigee bereitgestellten Richtlinien hinausgehen.
Diese Richtlinie ist eine erweiterbare Richtlinie, deren Verwendung je nach Apigee-Lizenz Auswirkungen auf die Kosten oder die Nutzung haben kann. Informationen zu Richtlinientypen und Auswirkungen auf die Nutzung finden Sie unter Richtlinientypen.
Python-Sprachunterstützung wird über Jython Version 2.5.2 bereitgestellt. Bibliotheken von Drittanbietern, die Sie hinzufügen, müssen „reine Python“ sein (nur in Python implementiert). Weitere Informationen zum Hinzufügen von Bibliotheken finden Sie unter Ressourcendateien.
Eine Python-Richtlinie enthält keinen tatsächlichen Code. Stattdessen verweisen Python-Richtlinien auf Python-Ressourcen und definieren den Schritt im API-Ablauf, in dem das Python-Skript ausgeführt wird. Sie können Ihr Skript über den Proxy-Editor von Apigee hochladen oder es in die von Ihnen lokal entwickelten API-Proxys in das Verzeichnis /resources/py
aufnehmen.
Beispiele
Python-Richtlinie und -Skript
PythonScript-Richtlinie
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Script name="Python-1"> <DisplayName>Python-1</DisplayName> <ResourceURL>py://myscript.py</ResourceURL> </Script>
In diesem Beispiel gibt das Element „ResourceURL“ die relevante Python-Skriptressource an.
Python Script
Dies zeigt, was das Python-Skript selbst hinzugefügt werden kann.
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)
Elementverweis
<?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 Optionally, use the |
N/A | Required |
continueOnError |
Set to Set to |
false | Optional |
enabled |
Set to Set to |
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 |
---|---|
Presence | Optional |
Type | String |
<ResourceURL>-Element
Mit diesem Element wird die Python-Hauptdatei bestimmt, die im API-Ablauf ausgeführt wird. Sie können diese Datei im API-Proxy-Bereich (unter /apiproxy/resources/py
im API-Proxyset oder im Abschnitt "Skripts" im Navigationsbereich des API-Proxy-Editors) oder in den Organisations- oder Umgebungsbereichen zur Wiederverwendung in mehreren API-Proxys speichern, wie unter Ressourcendateien beschrieben. Ihr Code kann die Objekte, Methoden und Eigenschaften des JavaScript-Objektmodells verwenden.
<ResourceURL>py://myscript.py</ResourceURL>
Standard: | – |
Präsenz: | Erforderlich |
Typ: | String |
<IncludeURL>-Element
Gibt eine Python-Datei an, die als Abhängigkeit für die mit dem Element <ResourceURL>
angegebene Python-Hauptdatei geladen werden soll. Die Skripts werden in der Reihenfolge ausgewertet, in der sie in der Richtlinie aufgeführt sind.
Mehr als eine Python-Abhängigkeitsressource mit zusätzlichen <IncludeURL>
-Elementen hinzufügen.
<IncludeURL>py://myscript_dependency.py</IncludeURL>
Standard: | – |
Präsenz: | Optional |
Typ: | String |
Fehlercodes
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>