|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from deepset_mcp.api.exceptions import ResourceNotFoundError |
| 4 | +from deepset_mcp.api.protocols import AsyncClientProtocol, SecretResourceProtocol |
| 5 | +from deepset_mcp.api.secrets.models import Secret, SecretList |
| 6 | +from deepset_mcp.api.shared_models import NoContentResponse |
| 7 | +from deepset_mcp.api.transport import raise_for_status |
| 8 | + |
| 9 | + |
| 10 | +class SecretResource(SecretResourceProtocol): |
| 11 | + """Resource for managing secrets in deepset.""" |
| 12 | + |
| 13 | + def __init__(self, client: AsyncClientProtocol) -> None: |
| 14 | + """Initialize a SecretResource. |
| 15 | +
|
| 16 | + :param client: The API client to use for requests. |
| 17 | + """ |
| 18 | + self._client = client |
| 19 | + |
| 20 | + async def list( |
| 21 | + self, |
| 22 | + limit: int = 10, |
| 23 | + field: str = "created_at", |
| 24 | + order: str = "DESC", |
| 25 | + ) -> SecretList: |
| 26 | + """List secrets with pagination. |
| 27 | +
|
| 28 | + :param limit: Maximum number of secrets to return. |
| 29 | + :param field: Field to sort by. |
| 30 | + :param order: Sort order (ASC or DESC). |
| 31 | +
|
| 32 | + :returns: List of secrets with pagination info. |
| 33 | + """ |
| 34 | + params = { |
| 35 | + "limit": str(limit), |
| 36 | + "field": field, |
| 37 | + "order": order, |
| 38 | + } |
| 39 | + |
| 40 | + resp = await self._client.request( |
| 41 | + endpoint="v2/secrets", |
| 42 | + method="GET", |
| 43 | + response_type=dict[str, Any], |
| 44 | + params=params, |
| 45 | + ) |
| 46 | + |
| 47 | + raise_for_status(resp) |
| 48 | + |
| 49 | + if resp.json is None: |
| 50 | + raise ResourceNotFoundError("Failed to retrieve secrets.") |
| 51 | + |
| 52 | + return SecretList(**resp.json) |
| 53 | + |
| 54 | + async def create(self, name: str, secret: str) -> NoContentResponse: |
| 55 | + """Create a new secret. |
| 56 | +
|
| 57 | + :param name: The name of the secret. |
| 58 | + :param secret: The secret value. |
| 59 | +
|
| 60 | + :returns: NoContentResponse indicating successful creation. |
| 61 | + """ |
| 62 | + data = { |
| 63 | + "name": name, |
| 64 | + "secret": secret, |
| 65 | + } |
| 66 | + |
| 67 | + resp = await self._client.request( |
| 68 | + endpoint="v2/secrets", |
| 69 | + method="POST", |
| 70 | + data=data, |
| 71 | + response_type=None, |
| 72 | + ) |
| 73 | + |
| 74 | + raise_for_status(resp) |
| 75 | + return NoContentResponse(message="Secret created successfully.") |
| 76 | + |
| 77 | + async def get(self, secret_id: str) -> Secret: |
| 78 | + """Get a specific secret by ID. |
| 79 | +
|
| 80 | + :param secret_id: The ID of the secret to retrieve. |
| 81 | +
|
| 82 | + :returns: Secret information. |
| 83 | + """ |
| 84 | + resp = await self._client.request( |
| 85 | + endpoint=f"v2/secrets/{secret_id}", |
| 86 | + method="GET", |
| 87 | + response_type=dict[str, Any], |
| 88 | + ) |
| 89 | + |
| 90 | + raise_for_status(resp) |
| 91 | + |
| 92 | + if resp.json is None: |
| 93 | + raise ResourceNotFoundError(f"Secret '{secret_id}' not found.") |
| 94 | + |
| 95 | + return Secret(**resp.json) |
| 96 | + |
| 97 | + async def delete(self, secret_id: str) -> NoContentResponse: |
| 98 | + """Delete a secret by ID. |
| 99 | +
|
| 100 | + :param secret_id: The ID of the secret to delete. |
| 101 | +
|
| 102 | + :returns: NoContentResponse indicating successful deletion. |
| 103 | + """ |
| 104 | + resp = await self._client.request( |
| 105 | + endpoint=f"v2/secrets/{secret_id}", |
| 106 | + method="DELETE", |
| 107 | + response_type=None, |
| 108 | + ) |
| 109 | + |
| 110 | + raise_for_status(resp) |
| 111 | + return NoContentResponse(message="Secret deleted successfully.") |
0 commit comments