![](https://docs.apigee.com/static/api-platform/images/icon_policy_python.jpg?authuser=5&hl=ja)
概要
Python Script ポリシーを使用すると、カスタマイズした Python 機能を API プロキシフローに追加できます。これは、Edge の既定ポリシーで提供される機能では足りない場合に便利です。
Python の言語サポートは Jython バージョン 2.5.2 を通じて提供されます。サードパーティ ライブラリを追加する場合は、純粋な Python にする必要があります(Python でのみ実装される必要があります)。ライブラリの追加の詳細については、リソース ファイルをご覧ください。
Python ポリシーには実際のコードを記述しません。Python ポリシーでは、Python リソースを参照し、Python スクリプトを実行する API フローのステップを定義します。Management UI のプロキシ エディタを通じてスクリプトをアップロードするか、ローカルで開発している API プロキシの /resources/py
ディレクトリにスクリプトを含めることができます。
サンプル
Python ポリシーとスクリプト
Python Script ポリシー
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Script name="Python-1"> <DisplayName>Python-1</DisplayName> <ResourceURL>py://myscript.py</ResourceURL> </Script>
この例では、関連する Python スクリプト リソースを ResourceURL 要素に指定しています。
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 |
ポリシーの内部名。 管理 UI プロキシ エディタで |
なし | 必須 |
continueOnError |
ポリシーが失敗したときにエラーを返す場合は、 ポリシーが失敗した後もフローの実行を続行する場合は、 |
false | 任意 |
enabled |
ポリシーを適用するには、 ポリシーを無効にするには、 |
true | 任意 |
async |
この属性は非推奨となりました。 |
false | 非推奨 |
<DisplayName> 要素
管理 UI プロキシ エディタで name
属性と一緒に使用して、ポリシーのラベルに使用する自然言語名を指定します。
<DisplayName>Policy Display Name</DisplayName>
デフォルト |
なし この要素を省略した場合、ポリシーの |
---|---|
要否 | 任意 |
種類 | 文字列 |
<ResourceURL> 要素
この要素には、API フローで実行されるメインの Python ファイルを指定します。このファイルを API プロキシ スコープ(API プロキシ バンドルの /apiproxy/resources/py
の下、あるいは API プロキシ エディタのナビゲーション パネルの [スクリプト] セクション)で、あるいは組織または環境スコープで保存して、複数の API プロキシ間で再使用できます。詳しくは、リソース ファイルの説明をご覧ください。コードでは、JavaScript オブジェクト モデルのオブジェクト、メソッド、プロパティを使用できます。
<ResourceURL>py://myscript.py</ResourceURL>
デフォルト: | なし |
要否: | 必須 |
型: | 文字列 |
<IncludeURL> 要素
<ResourceURL>
要素で指定されたメインの Python ファイルに依存関係として読み込ませる Python ファイルを指定します。このスクリプトは、ポリシーに指定した順番で評価されます。
複数の <IncludeURL>
要素を使用して複数の Python 依存関係を含めることができます。
<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. | 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>