You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add context sharing across hook type. (#1514)
* feat: add context sharing across hook type.
Signed-off-by: Teryl Taylor <[email protected]>
* docs: updated docs, added logging and tests.
Signed-off-by: Teryl Taylor <[email protected]>
* fix: update test assertions for cross-hook context sharing
Update unit test assertions to use ANY for plugin_global_context
parameter since the HttpAuthMiddleware now correctly creates and
stores a GlobalContext in request.state for cross-hook context
sharing.
Also fix integration test bugs where service constructors were
incorrectly passed plugin_manager as a keyword argument (services
get the plugin manager from a global singleton, not constructor).
Signed-off-by: Mihai Criveti <[email protected]>
---------
Signed-off-by: Teryl Taylor <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Teryl Taylor <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
Copy file name to clipboardExpand all lines: docs/docs/using/plugins/index.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -863,7 +863,7 @@ class MyPlugin(Plugin):
863
863
864
864
### Plugin Context and State
865
865
866
-
Each hook function has a `context` object of type `PluginContext` which is designed to allow plugins to pass state between one another (across pre/post hook pairs) or for a plugin to pass state information to itself across pre/post hook pairs. The plugin context looks as follows:
866
+
Each hook function has a `context` object of type `PluginContext` which is designed to allow plugins to pass state between one another across all hook types in a request, or for a plugin to pass state information to itself across different hooks. The plugin context looks as follows:
867
867
868
868
```python
869
869
class GlobalContext(BaseModel):
@@ -900,10 +900,9 @@ class PluginContext(BaseModel):
As can be seen, the `PluginContext` has both a `state` dictionary and a `global_context` object that also has a `state` dictionary. A single plugin can share state between pre/post hook pairs by using the
904
-
the `PluginContext` state dictionary. It can share state with other plugins using the `context.global_context.state` dictionary. Metadata for the specific hook site is passed in through the `metadata` dictionaries in the `context.global_context.metadata`. It is meant to be read-only. The `context.metadata` is plugin specific metadata and can be used to store metadata information such as timing information.
903
+
As can be seen, the `PluginContext` has both a `state` dictionary and a `global_context` object that also has a `state` dictionary. A single plugin can share state across all hooks in a request by using the `PluginContext` state dictionary. It can share state with other plugins using the `context.global_context.state` dictionary. Metadata for the specific hook site is passed in through the `metadata` dictionaries in the `context.global_context.metadata`. It is meant to be read-only. The `context.metadata` is plugin specific metadata and can be used to store metadata information such as timing information.
905
904
906
-
The following shows how plugins can maintain state between pre/post hooks:
905
+
The following shows how plugins can maintain state across different hooks:
Currently, the tool pre/post hooks have access to tool and gateway metadata through the global context metadata dictionary. They are accessible as follows:
928
+
Tool hooks have access to tool and gateway metadata through the global context metadata dictionary. They are accessible as follows:
930
929
931
930
It can be accessed inside of the tool hooks through:
0 commit comments