Skip to content

Commit 4e80db2

Browse files
committed
implemented 'upload_reviewable'
1 parent 2c586bd commit 4e80db2

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

ayon_api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
download_file,
6868
upload_file_from_stream,
6969
upload_file,
70+
upload_reviewable,
7071
trigger_server_restart,
7172
query_graphql,
7273
get_graphql_schema,
@@ -290,6 +291,7 @@
290291
"download_file",
291292
"upload_file_from_stream",
292293
"upload_file",
294+
"upload_reviewable",
293295
"trigger_server_restart",
294296
"query_graphql",
295297
"get_graphql_schema",

ayon_api/_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,29 @@ def upload_file(*args, **kwargs):
938938
return con.upload_file(*args, **kwargs)
939939

940940

941+
def upload_reviewable(*args, **kwargs):
942+
"""Upload reviewable file to server.
943+
944+
Args:
945+
project_name (str): Project name.
946+
version_id (str): Version id.
947+
filepath (str): Reviewable file path to upload.
948+
label (Optional[str]): Reviewable label. Filled automatically
949+
server side with filename.
950+
content_type (Optional[str]): MIME type of the file.
951+
filename (Optional[str]): User as original filename. Filename from
952+
'filepath' is used when not filled.
953+
progress (Optional[TransferProgress]): Progress.
954+
headers (Optional[Dict[str, Any]]): Headers.
955+
956+
Returns:
957+
RestApiResponse: Server response.
958+
959+
"""
960+
con = get_server_api_connection()
961+
return con.upload_reviewable(*args, **kwargs)
962+
963+
941964
def trigger_server_restart():
942965
"""Trigger server restart.
943966

ayon_api/server_api.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
get_default_settings_variant,
9191
get_default_site_id,
9292
NOT_SET,
93+
get_media_mime_type,
9394
)
9495

9596
PatternType = type(re.compile(""))
@@ -1931,6 +1932,77 @@ def upload_file(
19311932
endpoint, stream, progress, request_type, **kwargs
19321933
)
19331934

1935+
def upload_reviewable(
1936+
self,
1937+
project_name,
1938+
version_id,
1939+
filepath,
1940+
label=None,
1941+
content_type=None,
1942+
filename=None,
1943+
progress=None,
1944+
headers=None,
1945+
**kwargs
1946+
):
1947+
"""Upload reviewable file to server.
1948+
1949+
Args:
1950+
project_name (str): Project name.
1951+
version_id (str): Version id.
1952+
filepath (str): Reviewable file path to upload.
1953+
label (Optional[str]): Reviewable label. Filled automatically
1954+
server side with filename.
1955+
content_type (Optional[str]): MIME type of the file.
1956+
filename (Optional[str]): User as original filename. Filename from
1957+
'filepath' is used when not filled.
1958+
progress (Optional[TransferProgress]): Progress.
1959+
headers (Optional[Dict[str, Any]]): Headers.
1960+
1961+
Returns:
1962+
RestApiResponse: Server response.
1963+
1964+
"""
1965+
if not content_type:
1966+
content_type = get_media_mime_type(filepath)
1967+
1968+
if not content_type:
1969+
raise ValueError(
1970+
f"Could not determine MIME type of file '{filepath}'"
1971+
)
1972+
1973+
if headers is None:
1974+
headers = self.get_headers(content_type)
1975+
else:
1976+
# Make sure content-type is filled with file content type
1977+
content_type_key = next(
1978+
(
1979+
key
1980+
for key in headers
1981+
if key.lower() == "content-type"
1982+
),
1983+
"Content-Type"
1984+
)
1985+
headers[content_type_key] = content_type
1986+
1987+
# Fill original filename if not explicitly defined
1988+
if not filename:
1989+
filename = os.path.basename(filepath)
1990+
headers["x-file-name"] = filename
1991+
1992+
query = f"?label={label}" if label else ""
1993+
endpoint = (
1994+
f"/projects/{project_name}"
1995+
f"/versions/{version_id}/reviewables{query}"
1996+
)
1997+
return self.upload_file(
1998+
endpoint,
1999+
filepath,
2000+
progress=progress,
2001+
headers=headers,
2002+
request_type=RequestTypes.post,
2003+
**kwargs
2004+
)
2005+
19342006
def trigger_server_restart(self):
19352007
"""Trigger server restart.
19362008

0 commit comments

Comments
 (0)