Skip to content

Commit a8181f7

Browse files
committed
Proper fix for add_model_content
1 parent 3214224 commit a8181f7

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/sasctl/_services/model_repository.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def add_model_content(cls, model, file, name, role=None, content_type=None):
374374
model : str or dict
375375
The name or id of the model, or a dictionary representation of
376376
the model.
377-
file : str or bytes
377+
file : str, dict, or bytes
378378
A file related to the model, such as the model code.
379379
name : str
380380
Name of the file related to the model.
@@ -399,22 +399,24 @@ def add_model_content(cls, model, file, name, role=None, content_type=None):
399399

400400
if content_type is None and isinstance(file, bytes):
401401
content_type = "application/octet-stream"
402+
elif isinstance(file, dict):
403+
import json
404+
file = json.dumps(file)
402405

403-
if content_type is not None:
404-
files = {name: (name, file, content_type)}
405-
else:
406-
files = {name: file}
407-
408-
metadata = {"role": role, "name": name}
409-
410-
# return cls.post('/models/{}/contents'.format(id_), files=files, data=metadata)
406+
files = {"files": (name, file, content_type)}
411407

412-
# if the file already exists, a 409 error will be returned
408+
if role is None:
409+
params = {}
410+
else:
411+
params = {"role": role}
412+
params = "&".join("{}={}".format(k, v) for k, v in params.items())
413+
414+
# If the file already exists, a 409 error will be returned
413415
try:
414416
return cls.post(
415-
"/models/{}/contents".format(id_), files=files, data=metadata
417+
"/models/{}/contents".format(id_), files=files, params=params
416418
)
417-
# delete the older duplicate model and rerun the API call
419+
# Delete the older duplicate model and rerun the API call
418420
except HTTPError as e:
419421
if e.code == 409:
420422
model_contents = cls.get_model_contents(id_)
@@ -424,7 +426,7 @@ def add_model_content(cls, model, file, name, role=None, content_type=None):
424426
return cls.post(
425427
"/models/{}/contents".format(id_),
426428
files=files,
427-
data=metadata,
429+
params=params,
428430
)
429431
else:
430432
raise e

0 commit comments

Comments
 (0)