Deepomatic / deepomatic-client-python

Python Client
Other
14 stars 0 forks source link

Automatically json.dumps when sending a dict in a multipart request #93

Open maingoh opened 3 years ago

maingoh commented 3 years ago

When sending a multipart request (with files) if we want to attach a metadata field or any field that is nested, currently the user should json.dumps it or it might be ignored by the server (hard to debug). We should probably improve https://github.com/Deepomatic/deepomatic-client-python/blob/f4797245ca968872f4e0bbf31fabf7e186f90737/deepomatic/api/http_helper.py#L242 so that the user can do:

Example:

client.http_helper.post(files={'file': open('file', 'rb')}, data={'metadata': {'foo': 'bar'}}, content_type=''multipart/mixed'')

Instead of:

client.http_helper.post(files={'file': open('file', 'rb')}, data={'metadata': json.dumps({'foo': 'bar'})}, content_type=''multipart/mixed'')
maingoh commented 3 years ago

This is also the case when trying to post a file using ImageInput with a bbox (or other nested field). If the encoding is binary we should automatically dumps the bbox dict.

qtreh commented 3 years ago

data = self.dump_json_for_multipart(data) converts fields to keys and values such that:

http_helper.dump_json_for_multipart({'level_0': {'level_1': {'level_2': "niveau 2"}}, 'level_0_bis': 'niveau 0'})
# => returns {'level_0.level_1.level_2': 'niveau 2', 'level_0_bis': 'niveau 0'}

If we have a bbox in input, it will separate the coordinates in 4 keys: 'image.input.bbox.xmin': 0.25, 'image.input.bbox.xmax': 0.65, ... this is not good, and it fails anyway because floats make Vulcan fail.

I tried dumping the data and files to stringified json, but it doesn't work if the image has a binary as source instead of a url.

The best way to solve this is still to convert precise jsons we want to pass, like the bbox. So I modified the ImageInput.get_input() method to convert the bbox in those cases.