2018-10-16 12:38:32 +00:00
|
|
|
package gonextcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
req "github.com/levigross/grequests"
|
|
|
|
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
//AppsConfigList lists all the available apps
|
|
|
|
func (c *Client) AppsConfigList() (apps []string, err error) {
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.AppConfigResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//AppsConfigKeys returns the app's config keys
|
|
|
|
func (c *Client) AppsConfigKeys(id string) (keys []string, err error) {
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.AppConfigResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//AppsConfigValue get the config value for the given app's key
|
|
|
|
func (c *Client) AppsConfigValue(id, key string) (string, error) {
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil, id, key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var r types.AppcConfigValueResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//AppsConfigSetValue set the config value for the given app's key
|
|
|
|
func (c *Client) AppsConfigSetValue(id, key, value string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"value": value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := c.baseRequest(http.MethodPost, routes.appsConfig, ro, id, key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//AppsConfigDeleteValue delete the config value and (!! be careful !!) the key
|
|
|
|
func (c *Client) AppsConfigDeleteValue(id, key, value string) error {
|
|
|
|
_, err := c.baseRequest(http.MethodDelete, routes.appsConfig, nil, id, key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//AppsConfig returns all apps AppConfigDetails
|
|
|
|
func (c *Client) AppsConfig() (map[string]map[string]string, error) {
|
2018-10-16 16:16:18 +00:00
|
|
|
config := map[string]map[string]string{}
|
|
|
|
m := sync.Mutex{}
|
2018-10-16 12:38:32 +00:00
|
|
|
appsIDs, err := c.AppsConfigList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(appsIDs))
|
|
|
|
for i := range appsIDs {
|
|
|
|
go func(id string) {
|
|
|
|
defer wg.Done()
|
|
|
|
d, err := c.AppsConfigDetails(id)
|
|
|
|
if err == nil {
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Lock()
|
2018-10-16 12:38:32 +00:00
|
|
|
config[id] = d
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Unlock()
|
2018-10-16 12:38:32 +00:00
|
|
|
}
|
|
|
|
}(appsIDs[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//AppsConfigDetails returns all the config's key, values pair of the app
|
|
|
|
func (c *Client) AppsConfigDetails(appID string) (map[string]string, error) {
|
2018-10-16 16:16:18 +00:00
|
|
|
config := map[string]string{}
|
|
|
|
m := sync.Mutex{}
|
2018-10-16 12:38:32 +00:00
|
|
|
var err error
|
|
|
|
var ks []string
|
|
|
|
ks, err = c.AppsConfigKeys(appID)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(ks))
|
|
|
|
for i := range ks {
|
|
|
|
go func(key string) {
|
|
|
|
defer wg.Done()
|
2018-10-16 16:16:18 +00:00
|
|
|
v, err := c.AppsConfigValue(appID, key)
|
2018-10-16 12:38:32 +00:00
|
|
|
if err == nil {
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Lock()
|
2018-10-16 12:38:32 +00:00
|
|
|
config[key] = v
|
2018-10-16 16:16:18 +00:00
|
|
|
m.Unlock()
|
2018-10-16 12:38:32 +00:00
|
|
|
}
|
|
|
|
}(ks[i])
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return config, err
|
|
|
|
}
|