[json] Add endpoint to delete a setting (reset to defaults)

This commit is contained in:
ejurgensen 2020-05-16 00:04:37 +02:00
parent bb434297ca
commit d073781445
4 changed files with 77 additions and 4 deletions

View File

@ -2173,8 +2173,9 @@ curl -X GET "http://localhost:3689/api/config"
| --------- | ------------------------------------------------ | ------------------------------------ |
| GET | [/api/settings](#list-categories) | Get all available categories |
| GET | [/api/settings/{category-name}](#get-a-category) | Get all available options for a category |
| GET | [/api/settings/{category-name}/{option-name}](#get-a-option) | Get a single setting option |
| PUT | [/api/settings/{category-name}/{option-name}](#change-a-option-value) | Change the value of a setting option |
| GET | [/api/settings/{category-name}/{option-name}](#get-an-option) | Get a single setting option |
| PUT | [/api/settings/{category-name}/{option-name}](#change-an-option-value) | Change the value of a setting option |
| DELETE | [/api/settings/{category-name}/{option-name}](#delete-an-option) | Reset a setting option to its default |
@ -2264,7 +2265,7 @@ curl -X GET "http://localhost:3689/api/settings/webinterface"
```
### Get a option
### Get an option
Get a single settings option
@ -2294,7 +2295,7 @@ curl -X GET "http://localhost:3689/api/settings/webinterface/show_composer_now_p
```
### Change a option value
### Change an option value
Get a single settings option
@ -2323,6 +2324,28 @@ curl -X PUT "http://localhost:3689/api/settings/webinterface/show_composer_now_p
```
### Delete an option
Delete a single settings option (thus resetting it to default)
**Endpoint**
```http
DELETE /api/settings/{category-name}/{option-name}
```
**Response**
On success returns the HTTP `204 No Content` success status response code.
**Example**
```shell
curl -X DELETE "http://localhost:3689/api/settings/webinterface/show_composer_now_playing"
```
## Push notifications
If forked-daapd was built with websocket support, forked-daapd exposes a websocket at `localhost:3688` to inform clients of changes (e. g. player state or library updates).

View File

@ -998,6 +998,43 @@ jsonapi_reply_settings_option_put(struct httpd_request *hreq)
return HTTP_NOCONTENT;
}
static int
jsonapi_reply_settings_option_delete(struct httpd_request *hreq)
{
const char *categoryname;
const char *optionname;
struct settings_category *category;
struct settings_option *option;
int ret;
categoryname = hreq->uri_parsed->path_parts[2];
optionname = hreq->uri_parsed->path_parts[3];
category = settings_category_get(categoryname);
if (!category)
{
DPRINTF(E_LOG, L_WEB, "Invalid category name '%s' given\n", categoryname);
return HTTP_NOTFOUND;
}
option = settings_option_get(category, optionname);
if (!option)
{
DPRINTF(E_LOG, L_WEB, "Invalid option name '%s' given\n", optionname);
return HTTP_NOTFOUND;
}
ret = settings_option_delete(option);
if (ret < 0)
{
DPRINTF(E_LOG, L_WEB, "Error deleting option '%s'\n", optionname);
return HTTP_INTERNAL;
}
return HTTP_NOCONTENT;
}
/*
* Endpoint to retrieve informations about the library
*
@ -4167,6 +4204,7 @@ static struct httpd_uri_map adm_handlers[] =
{ EVHTTP_REQ_GET, "^/api/settings/[A-Za-z0-9_]+$", jsonapi_reply_settings_category_get },
{ EVHTTP_REQ_GET, "^/api/settings/[A-Za-z0-9_]+/[A-Za-z0-9_]+$", jsonapi_reply_settings_option_get },
{ EVHTTP_REQ_PUT, "^/api/settings/[A-Za-z0-9_]+/[A-Za-z0-9_]+$", jsonapi_reply_settings_option_put },
{ EVHTTP_REQ_DELETE, "^/api/settings/[A-Za-z0-9_]+/[A-Za-z0-9_]+$", jsonapi_reply_settings_option_delete },
{ EVHTTP_REQ_GET, "^/api/library$", jsonapi_reply_library },
{ EVHTTP_REQ_GET |
EVHTTP_REQ_PUT, "^/api/update$", jsonapi_reply_update },

View File

@ -241,3 +241,12 @@ settings_option_setstr(struct settings_option *option, const char *value)
return db_admin_set(option->name, value);
}
int
settings_option_delete(struct settings_option *option)
{
if (!option)
return -1;
return db_admin_delete(option->name);
}

View File

@ -64,4 +64,7 @@ settings_option_setbool(struct settings_option *option, bool value);
int
settings_option_setstr(struct settings_option *option, const char *value);
int
settings_option_delete(struct settings_option *option);
#endif /* __SETTINGS_H__ */