> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jakan.co/llms.txt
> Use this file to discover all available pages before exploring further.

# StashContext

> Context manager for managing the StashClient lifecycle.

Context manager for managing the StashClient lifecycle.

## StashContext

```python theme={null}
StashContext(
    conn: dict[str, Any] | None = None,
    verify_ssl: bool | str = True,
)
```

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

<Note>
  **Example**

  ```python theme={null}
  # 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
  )
  ```
</Note>

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`  |

### Attributes

#### conn

```python theme={null}
conn = _normalize_conn_keys(conn or {})
```

#### verify\_ssl

```python theme={null}
verify_ssl = lower() in ('true', '1', 'yes')
```

#### interface

```python theme={null}
interface: StashClient
```

Get Stash interface (alias for client).

Returns:

| Type          | Description          |
| ------------- | -------------------- |
| `StashClient` | StashClient instance |

Raises:

| Type           | Description                  |
| -------------- | ---------------------------- |
| `RuntimeError` | If client is not initialized |

#### client

```python theme={null}
client: StashClient
```

Get client instance.

Returns:

| Type          | Description          |
| ------------- | -------------------- |
| `StashClient` | StashClient instance |

Raises:

| Type           | Description                  |
| -------------- | ---------------------------- |
| `RuntimeError` | If client is not initialized |

#### store

```python theme={null}
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

```python theme={null}
capabilities: ServerCapabilities
```

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

```python theme={null}
ref_count: int
```

Get current reference count.

Returns:

| Type  | Description                                                |
| ----- | ---------------------------------------------------------- |
| `int` | Number of active async context managers using this context |

### Functions

#### get\_client

```python theme={null}
get_client() -> StashClient
```

Get initialized Stash client.

Returns:

| Type          | Description          |
| ------------- | -------------------- |
| `StashClient` | StashClient instance |

Raises:

| Type           | Description                    |
| -------------- | ------------------------------ |
| `RuntimeError` | If client initialization fails |

#### close

```python theme={null}
close(force: bool = False, _internal: bool = False) -> None
```

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.
</Note>
