Add common.formdata so api.js can just use dicts.

This commit is contained in:
voussoir 2021-09-03 12:53:01 -07:00
parent 2ad85ad69a
commit 2ab0b38c84
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
2 changed files with 39 additions and 19 deletions

View file

@ -7,8 +7,7 @@ api.channels.add_channel =
function add_channel(channel_id, callback)
{
const url = "/add_channel";
const data = new FormData();
data.append("channel_id", channel_id);
const data = {"channel_id": channel_id};
return common.post(url, data, callback);
}
@ -16,7 +15,7 @@ api.channels.delete_channel =
function delete_channel(channel_id, callback)
{
const url = `/channel/${channel_id}/delete`;
const data = new FormData();
const data = {};
return common.post(url, data, callback);
}
@ -24,8 +23,7 @@ api.channels.refresh_channel =
function refresh_channel(channel_id, force, callback)
{
const url = `/channel/${channel_id}/refresh`;
const data = new FormData();
data.append("force", force)
const data = {"force": force};
return common.post(url, data, callback);
}
@ -33,8 +31,7 @@ api.channels.refresh_all_channels =
function refresh_all_channels(force, callback)
{
const url = "/refresh_all_channels";
const data = new FormData();
data.append("force", force)
const data = {"force": force};
return common.post(url, data, callback);
}
@ -42,8 +39,7 @@ api.channels.set_automark =
function set_automark(channel_id, state, callback)
{
const url = `/channel/${channel_id}/set_automark`;
const data = new FormData();
data.append("state", state);
const data = {"state": state};
return common.post(url, data, callback);
}
@ -51,8 +47,7 @@ api.channels.set_download_directory =
function set_download_directory(channel_id, download_directory, callback)
{
const url = `/channel/${channel_id}/set_download_directory`;
const data = new FormData();
data.append("download_directory", download_directory);
const data = {"download_directory": download_directory};
return common.post(url, data, callback);
}
@ -60,8 +55,7 @@ api.channels.set_queuefile_extension =
function set_queuefile_extension(channel_id, extension, callback)
{
const url = `/channel/${channel_id}/set_queuefile_extension`;
const data = new FormData();
data.append("extension", extension);
const data = {"extension": extension};
return common.post(url, data, callback);
}
@ -85,9 +79,7 @@ api.videos.mark_state =
function mark_state(video_ids, state, callback)
{
const url = "/mark_video_state";
const data = new FormData();
data.append("video_ids", video_ids);
data.append("state", state);
const data = {"video_ids": video_ids, "state": state};
return common.post(url, data, callback);
}
@ -95,7 +87,6 @@ api.videos.start_download =
function start_download(video_ids, callback)
{
const url = "/start_download";
const data = new FormData();
data.append("video_ids", video_ids);
const data = {"video_ids": video_ids};
return common.post(url, data, callback);
}

View file

@ -55,6 +55,25 @@ function refresh_or_alert(response)
// HTTP ////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
common.formdata =
function formdata(data)
{
fd = new FormData();
for (let [key, value] of Object.entries(data))
{
if (value === undefined)
{
continue;
}
if (value === null)
{
value = '';
}
fd.append(key, value);
}
return fd;
}
common._request =
function _request(method, url, callback)
{
@ -143,8 +162,18 @@ common.post =
function post(url, data, callback)
{
/*
`data`: a FormData object which you have already filled with values.
`data`:
a FormData object which you have already filled with values, or a
dictionary from which a FormData will be made, using common.formdata.
*/
if (data instanceof FormData || data === null)
{
;
}
else
{
data = common.formdata(data);
}
request = common._request("POST", url, callback);
request.send(data);
return request;