Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support new files #1546

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions client/qiskit_serverless/core/clients/serverless_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,15 @@ def function(
####### FILES #######
#####################

def files(self, provider: Optional[str] = None) -> List[str]:
def files(self, function: QiskitFunction) -> List[str]:
"""Returns list of available files produced by programs to download."""
return self._files_client.list(provider)
return self._files_client.list(function)

def provider_files(
self, function: QiskitFunction, provider: Optional[str] = None
) -> List[str]:
"""Returns list of available files produced by programs to download."""
return self._files_client.provider_list(function, provider)

def file_download(
self,
Expand Down
28 changes: 22 additions & 6 deletions client/qiskit_serverless/core/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
REQUESTS_STREAMING_TIMEOUT,
REQUESTS_TIMEOUT,
)
from qiskit_serverless.core.function import QiskitFunction
from qiskit_serverless.utils.json import safe_json_request_as_dict


Expand All @@ -54,6 +55,7 @@ def __init__(self, host: str, token: str, version: str):
self.host = host
self.version = version
self._token = token
self._files_url = os.path.join(self.host, "api", self.version, "files")

def download(
self,
Expand All @@ -66,7 +68,7 @@ def download(
tracer = trace.get_tracer("client.tracer")
with tracer.start_as_current_span("files.download"):
with requests.get(
f"{self.host}/api/{self.version}/files/download/",
os.path.join(self._files_url, "download"),
params={"file": file, "provider": provider},
stream=True,
headers={"Authorization": f"Bearer {self._token}"},
Expand All @@ -93,7 +95,7 @@ def upload(self, file: str, provider: Optional[str] = None) -> Optional[str]:
with tracer.start_as_current_span("files.upload"):
with open(file, "rb") as f:
with requests.post(
f"{self.host}/api/{self.version}/files/upload/",
os.path.join(self._files_url, "upload"),
files={"file": f},
data={"provider": provider},
stream=True,
Expand All @@ -105,14 +107,28 @@ def upload(self, file: str, provider: Optional[str] = None) -> Optional[str]:
return "Upload failed"
return "Can not open file"

def list(self, provider: Optional[str] = None) -> List[str]:
def list(self, function: QiskitFunction) -> List[str]:
"""Returns list of available files to download produced by programs,"""
tracer = trace.get_tracer("client.tracer")
with tracer.start_as_current_span("files.list"):
response_data = safe_json_request_as_dict(
request=lambda: requests.get(
f"{self.host}/api/{self.version}/files/",
params={"provider": provider},
self._files_url,
params={"title": function.title},
headers={"Authorization": f"Bearer {self._token}"},
timeout=REQUESTS_TIMEOUT,
)
)
return response_data.get("results", [])

def provider_list(self, function: QiskitFunction, provider: str) -> List[str]:
"""Returns list of available files to download produced by programs,"""
tracer = trace.get_tracer("client.tracer")
with tracer.start_as_current_span("files.provider_list"):
response_data = safe_json_request_as_dict(
request=lambda: requests.get(
os.path.join(self._files_url, "provider"),
params={"provider": provider, "title": function.title},
headers={"Authorization": f"Bearer {self._token}"},
timeout=REQUESTS_TIMEOUT,
)
Expand All @@ -125,7 +141,7 @@ def delete(self, file: str, provider: Optional[str] = None) -> Optional[str]:
with tracer.start_as_current_span("files.delete"):
response_data = safe_json_request_as_dict(
request=lambda: requests.delete(
f"{self.host}/api/{self.version}/files/delete/",
os.path.join(self._files_url, "delete"),
data={"file": file, "provider": provider},
headers={
"Authorization": f"Bearer {self._token}",
Expand Down
Loading