|
| 1 | +"""Implementation of ArtifactClient""" |
| 2 | + |
| 3 | +from typing import BinaryIO, Optional |
| 4 | + |
| 5 | +from nisystemlink.clients import core |
| 6 | +from nisystemlink.clients.core._uplink._base_client import BaseClient |
| 7 | +from nisystemlink.clients.core._uplink._methods import get, post, response_handler |
| 8 | +from nisystemlink.clients.core.helpers._iterator_file_like import IteratorFileLike |
| 9 | +from requests.models import Response |
| 10 | +from uplink import Part, Path |
| 11 | + |
| 12 | +from . import models |
| 13 | + |
| 14 | + |
| 15 | +class ArtifactClient(BaseClient): |
| 16 | + def __init__(self, configuration: Optional[core.HttpConfiguration] = None): |
| 17 | + """Initialize an instance. |
| 18 | +
|
| 19 | + Args: |
| 20 | + configuration: Defines the web server to connect to and information about |
| 21 | + how to connect. If not provided, the |
| 22 | + :class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>` |
| 23 | + is used to obtain the configuration. |
| 24 | +
|
| 25 | + Raises: |
| 26 | + ApiException: if unable to communicate with the Notebook execution Service. |
| 27 | + """ |
| 28 | + if configuration is None: |
| 29 | + configuration = core.HttpConfigurationManager.get_configuration() |
| 30 | + |
| 31 | + super().__init__(configuration, base_path="/ninbartifact/v1/") |
| 32 | + |
| 33 | + @post("artifacts") |
| 34 | + def __upload_artifact( |
| 35 | + self, workspace: Part, artifact: Part |
| 36 | + ) -> models.UploadArtifactResponse: |
| 37 | + """Uploads an artifact using multipart/form-data headers to send the file payload in the HTTP body. |
| 38 | +
|
| 39 | + Args: |
| 40 | + workspace: The workspace containing the artifact. |
| 41 | + artifact: The artifact to upload. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + UploadArtifactResponse: The response containing the artifact ID. |
| 45 | +
|
| 46 | + """ |
| 47 | + |
| 48 | + def upload_artifact( |
| 49 | + self, workspace: str, artifact: BinaryIO |
| 50 | + ) -> models.UploadArtifactResponse: |
| 51 | + """Uploads an artifact. |
| 52 | +
|
| 53 | + Args: |
| 54 | + workspace: The workspace containing the artifact. |
| 55 | + artifact: The artifact to upload. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + UploadArtifactResponse: The response containing the artifact ID. |
| 59 | +
|
| 60 | + """ |
| 61 | + response = self.__upload_artifact( |
| 62 | + workspace=workspace, |
| 63 | + artifact=artifact, |
| 64 | + ) |
| 65 | + |
| 66 | + return response |
| 67 | + |
| 68 | + def _iter_content_filelike_wrapper(response: Response) -> IteratorFileLike: |
| 69 | + return IteratorFileLike(response.iter_content(chunk_size=4096)) |
| 70 | + |
| 71 | + @response_handler(_iter_content_filelike_wrapper) |
| 72 | + @get("artifacts/{id}") |
| 73 | + def download_artifact(self, id: Path) -> IteratorFileLike: |
| 74 | + """Downloads an artifact. |
| 75 | +
|
| 76 | + Args: |
| 77 | + id: The ID of the artifact to download. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + A file-like object for reading the artifact content. |
| 81 | +
|
| 82 | + """ |
| 83 | + ... |
0 commit comments