StashContext¶
Context manager for managing the StashClient lifecycle.
StashContext
¶
Context manager for Stash client.
This class provides a high-level interface for managing Stash client connections, including: - Connection configuration - Client lifecycle management - Interface access - Reference counting for safe concurrent usage
Example
# Create context with connection details
context = StashContext(conn={
"Scheme": "http",
"Host": "localhost",
"Port": 9999,
"ApiKey": "your_api_key",
})
# Use context manager (safe for concurrent tasks with singleton)
async with context as client:
performer = await client.find_performer("123")
# Access entity store for caching and advanced queries
store = context.store
scene = await store.get(Scene, "456")
# Or use directly
client = context.client
store = context.store
performer = await client.find_performer("123")
await context.close()
# Safe for concurrent tasks when using singleton
await asyncio.gather(
task1(context), # Each task uses async with
task2(context), # Reference counting prevents premature closure
)
Initialize context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
dict[str, Any] | None
|
Connection details dictionary (case-insensitive keys) |
None
|
verify_ssl
|
bool | str
|
Whether to verify SSL certificates (accepts bool or string like "true"/"false") |
True
|
Source code in stash_graphql_client/context.py
Attributes¶
interface
property
¶
interface: StashClient
Get Stash interface (alias for client).
Returns:
| Type | Description |
|---|---|
StashClient
|
StashClient instance |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If client is not initialized |
client
property
¶
client: StashClient
Get client instance.
Returns:
| Type | Description |
|---|---|
StashClient
|
StashClient instance |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If client is not initialized |
store
property
¶
store: StashEntityStore
Get entity store instance.
Returns:
| Type | Description |
|---|---|
StashEntityStore
|
StashEntityStore instance wired to StashObject identity map |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If store is not initialized |
capabilities
property
¶
Get detected server capabilities.
Returns:
| Type | Description |
|---|---|
ServerCapabilities
|
ServerCapabilities instance with feature flags for the connected server |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If client is not initialized |
ref_count
property
¶
Get current reference count.
Returns:
| Type | Description |
|---|---|
int
|
Number of active async context managers using this context |
Functions¶
get_client
async
¶
get_client() -> StashClient
Get initialized Stash client.
Returns:
| Type | Description |
|---|---|
StashClient
|
StashClient instance |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If client initialization fails |
Source code in stash_graphql_client/context.py
close
async
¶
Close client connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force
|
bool
|
If True, force close even if reference count > 0. Use with caution as this may break concurrent tasks. |
False
|
_internal
|
bool
|
Internal flag indicating call from aexit (already has lock) |
False
|
Note
When using the singleton context with async context managers, manual close() is not needed - reference counting handles cleanup. Only call manually when you created the context directly and are done with it.