SasaKaranovic / VU-Server

VU Dials Server Application
https://vudials.com
26 stars 12 forks source link

Please provide an option to upload the background image as a data stream #9

Closed feeph closed 8 months ago

feeph commented 8 months ago

Hello

First things first: Thank You! The documentation and the server app really helps to get started.

Unfortunately I'm running into an issue when trying to set a new background image using the Dial API. https://docs.vudials.com/api/dial_UID_image_set/ says you need to provide a filename as a parameter and to me it's not clear how that is supposed to work.

POST - http://localhost:5340/api/v0/dial/.../image/set?key=...&imgfile=my_awesome_image.png
                                                                       ^^^^^^^^^^^^^^^^^^^^

As far as I understand it is assumed the file is already stored in some magic location and this API call merely selects the stored file.

The documentation says

After the image has been uploaded

but I didn't find a description how to upload a file.

proposed solution:

Since /api/v0/dial/{uid}/image/get returns the actual image data as a data stream I would prefer if /api/v0/dial/{uid}/image/set requires the API user to upload the image data as a data stream as well. That way the server can store the image data wherever it's needed (e.g. cache the checksum in the SQLite database and upload the image data to the dials).

If it is preferred to keep the existing implementation then I would suggest to rename the API endpoints, e.g.:

feeph commented 8 months ago

I figured it out by reading through the code.

The documentation wasn't helpful because a crucial part was not documented.

# this is basically just an identifier
# (it can be anything we want it to be)
img_name = 'cpu-temp'

# load the image's content as a binary value
img_data = open('img/cpu-temp.png', 'rb')

# prepare payload for the POST request
#  - this part is not mentioned in the documentation
#  - the key MUST be 'imgfile'
payload = {
  'imgfile': img_data,
}

requests.post(
  'http://localhost:5340/api/v0/dial/{dial.uid}/image/set',
  params={
    'key': <apikey>,
    'imgfile': img_name,
  },
  files=payload,
)
SasaKaranovic commented 8 months ago

Hi @feeph

Thanks for the feedback! This issue is probably more appropriate for the documentation instead of VU server. Please feel free to suggest/contribute to the VU documentation to help others who have similar question.

SasaKaranovic commented 8 months ago

Added clarification in the VU API documentation that the image file should be uploaded as form-data.