-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/ni/nisystemlink-clients-p…
…ython into users/aarnesen/testmonitor-product-api
- Loading branch information
Showing
35 changed files
with
1,305 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.. _api_tag_page: | ||
|
||
nisystemlink.clients.file | ||
====================== | ||
|
||
.. autoclass:: nisystemlink.clients.file.FileClient | ||
:exclude-members: __init__ | ||
|
||
.. automethod:: __init__ | ||
.. automethod:: api_info | ||
.. automethod:: get_files | ||
.. automethod:: delete_file | ||
.. automethod:: delete_files | ||
.. automethod:: upload_file | ||
.. automethod:: download_file | ||
.. automethod:: update_metadata | ||
|
||
.. automodule:: nisystemlink.clients.file.models | ||
:members: | ||
:imported-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import io | ||
|
||
from nisystemlink.clients.artifact import ArtifactClient | ||
from nisystemlink.clients.core import HttpConfiguration | ||
|
||
|
||
# Setup the server configuration to point to your instance of SystemLink Enterprise | ||
server_configuration = HttpConfiguration( | ||
server_uri="https://yourserver.yourcompany.com", | ||
api_key="YourAPIKeyGeneratedFromSystemLink", | ||
) | ||
client = ArtifactClient(configuration=server_configuration) | ||
|
||
# Define the workspace and artifact content | ||
workspace = "your workspace ID" | ||
artifact_stream = io.BytesIO(b"test content") | ||
|
||
# Upload the artifact | ||
upload_response = client.upload_artifact(workspace=workspace, artifact=artifact_stream) | ||
if upload_response and upload_response.id: | ||
print(f"Uploaded artifact ID: {upload_response.id}") | ||
|
||
# Download the artifact using the ID from the upload response | ||
artifact_id = upload_response.id | ||
download_response = client.download_artifact(artifact_id) | ||
if download_response: | ||
downloaded_content = download_response.read() | ||
print(f"Downloaded artifact content: {downloaded_content.decode('utf-8')}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Example to download a file from SystemLink.""" | ||
|
||
from shutil import copyfileobj | ||
|
||
from nisystemlink.clients.file import FileClient | ||
|
||
client = FileClient() | ||
|
||
file_id = "a55adc7f-5068-4202-9d70-70ca6a06bee9" | ||
|
||
# Fetch the file metadata to get the name | ||
files = client.get_files(ids=[file_id]) | ||
|
||
if not files.available_files: | ||
raise Exception(f"File ID {file_id} not found.") | ||
|
||
|
||
file_name = "Untitled" | ||
|
||
file_properties = files.available_files[0].properties | ||
|
||
if file_properties: | ||
file_name = file_properties["Name"] | ||
|
||
# Download the file using FileId with content inline | ||
content = client.download_file(id=file_id) | ||
|
||
# Write the content to a file | ||
with open(file_name, "wb") as f: | ||
copyfileobj(content, f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._artifact_client import ArtifactClient | ||
|
||
# flake8: noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"""Implementation of ArtifactClient""" | ||
|
||
from typing import BinaryIO, Optional | ||
|
||
from nisystemlink.clients import core | ||
from nisystemlink.clients.core._uplink._base_client import BaseClient | ||
from nisystemlink.clients.core._uplink._methods import get, post, response_handler | ||
from nisystemlink.clients.core.helpers._iterator_file_like import IteratorFileLike | ||
from requests.models import Response | ||
from uplink import Part, Path | ||
|
||
from . import models | ||
|
||
|
||
class ArtifactClient(BaseClient): | ||
def __init__(self, configuration: Optional[core.HttpConfiguration] = None): | ||
"""Initialize an instance. | ||
Args: | ||
configuration: Defines the web server to connect to and information about | ||
how to connect. If not provided, the | ||
:class:`HttpConfigurationManager <nisystemlink.clients.core.HttpConfigurationManager>` | ||
is used to obtain the configuration. | ||
Raises: | ||
ApiException: if unable to communicate with the Notebook execution Service. | ||
""" | ||
if configuration is None: | ||
configuration = core.HttpConfigurationManager.get_configuration() | ||
|
||
super().__init__(configuration, base_path="/ninbartifact/v1/") | ||
|
||
@post("artifacts") | ||
def __upload_artifact( | ||
self, workspace: Part, artifact: Part | ||
) -> models.UploadArtifactResponse: | ||
"""Uploads an artifact using multipart/form-data headers to send the file payload in the HTTP body. | ||
Args: | ||
workspace: The workspace containing the artifact. | ||
artifact: The artifact to upload. | ||
Returns: | ||
UploadArtifactResponse: The response containing the artifact ID. | ||
""" | ||
|
||
def upload_artifact( | ||
self, workspace: str, artifact: BinaryIO | ||
) -> models.UploadArtifactResponse: | ||
"""Uploads an artifact. | ||
Args: | ||
workspace: The workspace containing the artifact. | ||
artifact: The artifact to upload. | ||
Returns: | ||
UploadArtifactResponse: The response containing the artifact ID. | ||
""" | ||
response = self.__upload_artifact( | ||
workspace=workspace, | ||
artifact=artifact, | ||
) | ||
|
||
return response | ||
|
||
def _iter_content_filelike_wrapper(response: Response) -> IteratorFileLike: | ||
return IteratorFileLike(response.iter_content(chunk_size=4096)) | ||
|
||
@response_handler(_iter_content_filelike_wrapper) | ||
@get("artifacts/{id}") | ||
def download_artifact(self, id: Path) -> IteratorFileLike: | ||
"""Downloads an artifact. | ||
Args: | ||
id: The ID of the artifact to download. | ||
Returns: | ||
A file-like object for reading the artifact content. | ||
""" | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._upload_artifact_response import UploadArtifactResponse | ||
|
||
# flake8: noqa |
8 changes: 8 additions & 0 deletions
8
nisystemlink/clients/artifact/models/_upload_artifact_response.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from nisystemlink.clients.core._uplink._json_model import JsonModel | ||
|
||
|
||
class UploadArtifactResponse(JsonModel): | ||
"""Response for an artifact upload request.""" | ||
|
||
id: str | ||
"""Information about the uploaded artifact.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from nisystemlink.clients.core.helpers import IteratorFileLike | ||
from requests.models import Response | ||
|
||
|
||
def file_like_response_handler(response: Response) -> IteratorFileLike: | ||
"""Response handler for File-Like content.""" | ||
return IteratorFileLike(response.iter_content(chunk_size=4096)) |
Oops, something went wrong.