Skip to content

Commit 3214224

Browse files
committed
Revert "Update add_model_content to current API specs; include dict uploads"
This reverts commit 856afef.
1 parent 7f42e7b commit 3214224

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

src/sasctl/_services/model_repository.py

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
"""The Model Repository service supports registering and managing models."""
88

99
from warnings import warn
10-
from io import StringIO
11-
import json
1210

1311
from .service import Service
1412
from ..core import current_session, get, delete, sasctl_command, HTTPError
@@ -368,24 +366,22 @@ def create_model(
368366
)
369367

370368
@classmethod
371-
def add_model_content(cls, model, file, name, content_type="multipart/form-data", role=None):
372-
"""Add additional files to the model. Additional files can come in the form of
373-
a bytes-like, string, or dict object. String and dict objects will be converted to
374-
a bytes-like object for upload.
369+
def add_model_content(cls, model, file, name, role=None, content_type=None):
370+
"""Add additional files to the model.
375371
376372
Parameters
377373
----------
378374
model : str or dict
379375
The name or id of the model, or a dictionary representation of
380376
the model.
381-
file : str, dict, or bytes
377+
file : str or bytes
382378
A file related to the model, such as the model code.
383379
name : str
384380
Name of the file related to the model.
385-
content_type : str, optional
386-
An HTTP Content-Type value. Default value is multipart/form-data.
387-
role : str, optional
388-
Role of the model file, such as 'Python pickle'. Default value is None.
381+
role : str
382+
Role of the model file, such as 'Python pickle'.
383+
content_type : str
384+
an HTTP Content-Type value
389385
390386
Returns
391387
-------
@@ -400,45 +396,38 @@ def add_model_content(cls, model, file, name, content_type="multipart/form-data"
400396
else:
401397
model = cls.get_model(model)
402398
id_ = model["id"]
403-
404-
# Convert string file representations to bytes-like object
405-
if isinstance(file, str):
406-
file = StringIO(file)
407-
content_type="multipart/form-data"
408-
# Convert dict file representations to bytes-like object
409-
elif isinstance(file, dict):
410-
file = StringIO(json.dumps(file))
411-
content_type="multipart/form-data"
412-
413-
files = {"files": (name, file, content_type)}
414-
415-
if role is None:
416-
params = {}
399+
400+
if content_type is None and isinstance(file, bytes):
401+
content_type = "application/octet-stream"
402+
403+
if content_type is not None:
404+
files = {name: (name, file, content_type)}
417405
else:
418-
params = {"role": role}
419-
params = "&".join("{}={}".format(k, v) for k, v in params.items())
406+
files = {name: file}
407+
408+
metadata = {"role": role, "name": name}
420409

421-
# If the file already exists, a 409 error will be returned
410+
# return cls.post('/models/{}/contents'.format(id_), files=files, data=metadata)
411+
412+
# if the file already exists, a 409 error will be returned
422413
try:
423414
return cls.post(
424-
"/models/{}/contents".format(id_),
425-
files=files,
426-
params=params,
415+
"/models/{}/contents".format(id_), files=files, data=metadata
427416
)
428-
except HTTPError as err:
429-
# Delete the older duplicate model file and rerun the API call
430-
if err.code == 409:
417+
# delete the older duplicate model and rerun the API call
418+
except HTTPError as e:
419+
if e.code == 409:
431420
model_contents = cls.get_model_contents(id_)
432421
for item in model_contents:
433422
if item.name == name:
434423
cls.delete("/models/{}/contents/{}".format(id_, item.id))
435424
return cls.post(
436425
"/models/{}/contents".format(id_),
437426
files=files,
438-
params=params,
427+
data=metadata,
439428
)
440429
else:
441-
raise err
430+
raise e
442431

443432
@classmethod
444433
def default_repository(cls):

0 commit comments

Comments
 (0)