Skip to content

Commit 8ee169e

Browse files
committed
feature: add upload
1 parent b1fb896 commit 8ee169e

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

datatorch/api/api.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import logging, requests, os, cgi
1+
import logging, os, cgi, requests
22
from pathlib import Path
33

4-
from typing import overload, cast
4+
from typing import IO, overload, cast
5+
from urllib import request
56
from urllib.parse import urlencode
67

78
from .client import Client
@@ -135,6 +136,16 @@ def download_file(
135136

136137
return name, result
137138

139+
def upload_to_default_filesource(self, project: Project, file: IO, **kwargs):
140+
"""Takes in a project and file, and uploads the file to DataTorch Storage"""
141+
storageId = project.storage_link_default().id
142+
pathToUploadTo = ""
143+
endpoint = f"{self.api_url}/file/v1/upload/{storageId}?path={pathToUploadTo}&import=false&datasetId="
144+
print(endpoint)
145+
# r = requests.post(endpoint, files={"file": file}, user=self.viewer)
146+
r = requests.post(endpoint, files={"file": file}, headers={self.token_header: self._api_token}, stream=True)
147+
print(r.text)
148+
138149
# def files(self, where: Where = None, limit: int = 400) -> List[File]:
139150
# return []
140151

datatorch/api/client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Union, cast
1+
from typing import List, Union, cast, IO
22

33
from gql import Client as GqlClient, gql
44
from gql.transport.transport import Transport
@@ -10,7 +10,6 @@
1010
from datatorch.core import user_settings
1111
from typing import Any, TypeVar, Type
1212

13-
1413
T = TypeVar("T")
1514

1615

@@ -135,4 +134,4 @@ def to_class(self, Entity, results: Union[dict, list, None], path: str = ""):
135134
if type(results) == list:
136135
return list(map(lambda e: Entity(e, self), results))
137136

138-
return Entity(results, self)
137+
return Entity(results, self)

datatorch/api/entity/project.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@
5050
"""
5151
)
5252

53+
_STORAGE_LINK_DEFAULT = StorageLink.add_fragment(
54+
"""
55+
query GetStorageLinkDefault($projectId: ID!) {
56+
project: projectById(id: $projectId) {
57+
storageLinkDefault {
58+
...StorageLinkFields
59+
}
60+
}
61+
}
62+
"""
63+
)
64+
5365
_DATASET_FILES = File.add_fragment(
5466
"""
5567
query GetProjectFiles(
@@ -142,6 +154,17 @@ def storage_links(self) -> List[StorageLink]:
142154
),
143155
)
144156

157+
def storage_link_default(self) -> StorageLink:
158+
return cast(
159+
StorageLink,
160+
self.client.query_to_class(
161+
StorageLink,
162+
_STORAGE_LINK_DEFAULT,
163+
path="project.storageLinkDefault",
164+
params={"projectId": self.id},
165+
),
166+
)
167+
145168
def add(self, entity: AddableEntity):
146169
"""Add entity to project"""
147170

@@ -156,4 +179,4 @@ def add(self, entity: AddableEntity):
156179
return
157180

158181
if isinstance(entity, Label):
159-
self.create
182+
self.create

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
setup(
2828
name="datatorch",
29-
version="0.4.3",
30-
description="A CLI and library for interacting with DataTorch",
29+
version="0.4.4",
30+
description="A CLI and library for interacting with DataTorch.",
3131
author="DataTorch",
3232
author_email="[email protected]",
3333
entry_points={

0 commit comments

Comments
 (0)