matecsaj / ebay_rest

A pip package that conveniently wraps eBay’s RESTful APIs.
MIT License
43 stars 11 forks source link

How to upload a file? #60

Open matecsaj opened 1 year ago

matecsaj commented 1 year ago

Swagger with OpenAPI contracts from eBay generate the core of this library, and they are not currently able to accept a path-file. Please ask eBay to solve the root problem; many voices will convince them of the need to act.

To learn more search in the Unit Tests for 'sell_feed_upload_file'.

Workarounds are suggested in the ReadMe search for 'How to upload a file?'.

matecsaj commented 1 year ago

Citing John

I might be able to automate your patches. The generator I wrote already does various patches before and after running Swagger to permanently fix some known problems.
https://github.com/matecsaj/ebay_rest/blob/main/scripts/generate_code.py

After you make your modifications, please send me the before and after files. To make this more viable, please:

  1. Repurpose existing KW arguments; for example, use the filename parameter of the file upload command to pass in the fullpath+filename+extension.
  2. Don’t delete or modify existing lines of Python code; insert new blocks of code where needed.
  3. Minimize the number of change locations.
  4. Note that when there is a choice, it is almost always more reliable to patch the OpenAPI contract rather than the Python Code that Swagger generates.

Citing Wang

I didn't really download the ebay_rest library, I just learning it before use it in product, but I could not understand how the upload_file works because I could not find the code it actually open/read the file, again I never wrote a test code to verify if it works or not.

For the patch, I suggest this

In file https://github.com/matecsaj/ebay_rest/blob/main/src/ebay_rest/api/sell_feed/api/task_api.py#L642

after Line 615 local_var_files = {}

append following code

local_var_files = {params.get('name'): params.get('file_name')}

Again I never downloaded this library so I just feel it should work.

andreinl commented 9 months ago

After a few days of trying this is the code that works:

test_file = open(file_name, 'rb').read()
result = api.sell_feed_upload_file(
    file=(file_name, test_file),
    content_type="multipart/form-data",
    task_id=task_id,
    type='form-data',
    file_name=file_name,
)

To make it work you need to add e few lines in sell_feed/api/task_api.py to make upload_file_with_http_info() aware of the 'file' attribute:

somewhere after the line 602: all_params.append('file')

and after the line 652:

if 'file' in params:
    form_params.append(('file', params['file']))  # noqa: E501
matecsaj commented 9 months ago

Once again, thank you! I'm intensely working on another project, so please forgive me for letting this be for several weeks. If you need a fresh release with this change sooner or are willing to submit a PR, please use scripts/generate_code.py to patch sell_feed/api/task_api.py and provide a unit test.