Moved all methods inside sub Interfaces and structs

This commit is contained in:
Adphi 2018-10-21 14:53:12 +02:00
parent 8600032042
commit f9201552f9
15 changed files with 481 additions and 308 deletions

51
apps.go
View File

@ -6,9 +6,24 @@ import (
"net/http" "net/http"
) )
//AppList return the list of the Nextcloud Apps //AppsI available methods
func (c *Client) AppList() ([]string, error) { type AppsI interface {
res, err := c.baseRequest(http.MethodGet, routes.apps, nil) List() ([]string, error)
ListEnabled() ([]string, error)
ListDisabled() ([]string, error)
Infos(name string) (types.App, error)
Enable(name string) error
Disable(name string) error
}
//Apps contains all Apps available actions
type Apps struct {
c *Client
}
//List return the list of the Nextcloud Apps
func (a *Apps) List() ([]string, error) {
res, err := a.c.baseRequest(http.MethodGet, routes.apps, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -17,12 +32,12 @@ func (c *Client) AppList() ([]string, error) {
return r.Ocs.Data.Apps, nil return r.Ocs.Data.Apps, nil
} }
//AppListEnabled lists the enabled apps //ListEnabled lists the enabled apps
func (c *Client) AppListEnabled() ([]string, error) { func (a *Apps) ListEnabled() ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"filter": "enabled"}, Params: map[string]string{"filter": "enabled"},
} }
res, err := c.baseRequest(http.MethodGet, routes.apps, ro) res, err := a.c.baseRequest(http.MethodGet, routes.apps, ro)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -31,12 +46,12 @@ func (c *Client) AppListEnabled() ([]string, error) {
return r.Ocs.Data.Apps, nil return r.Ocs.Data.Apps, nil
} }
//AppListDisabled lists the disabled apps //ListDisabled lists the disabled apps
func (c *Client) AppListDisabled() ([]string, error) { func (a *Apps) ListDisabled() ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"filter": "disabled"}, Params: map[string]string{"filter": "disabled"},
} }
res, err := c.baseRequest(http.MethodGet, routes.apps, ro) res, err := a.c.baseRequest(http.MethodGet, routes.apps, ro)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -45,9 +60,9 @@ func (c *Client) AppListDisabled() ([]string, error) {
return r.Ocs.Data.Apps, nil return r.Ocs.Data.Apps, nil
} }
//AppInfos return the app's details //Infos return the app's details
func (c *Client) AppInfos(name string) (types.App, error) { func (a *Apps) Infos(name string) (types.App, error) {
res, err := c.baseRequest(http.MethodGet, routes.apps, nil, name) res, err := a.c.baseRequest(http.MethodGet, routes.apps, nil, name)
if err != nil { if err != nil {
return types.App{}, err return types.App{}, err
} }
@ -56,14 +71,14 @@ func (c *Client) AppInfos(name string) (types.App, error) {
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
//AppEnable enables an app //Enable enables an app
func (c *Client) AppEnable(name string) error { func (a *Apps) Enable(name string) error {
_, err := c.baseRequest(http.MethodPost, routes.apps, nil, name) _, err := a.c.baseRequest(http.MethodPost, routes.apps, nil, name)
return err return err
} }
//AppDisable disables an app //Disable disables an app
func (c *Client) AppDisable(name string) error { func (a *Apps) Disable(name string) error {
_, err := c.baseRequest(http.MethodDelete, routes.apps, nil, name) _, err := a.c.baseRequest(http.MethodDelete, routes.apps, nil, name)
return err return err
} }

15
apps_test.go Normal file
View File

@ -0,0 +1,15 @@
package gonextcloud
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestClientAppsList(t *testing.T) {
if err := initClient(); err != nil {
t.FailNow()
}
l, err := c.Apps.List()
assert.NoError(t, err)
assert.NotEmpty(t, l)
}

View File

@ -7,9 +7,25 @@ import (
"sync" "sync"
) )
//AppsConfigList lists all the available apps //AppsConfigI available methods
func (c *Client) AppsConfigList() (apps []string, err error) { type AppsConfigI interface {
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil) List() (apps []string, err error)
Keys(id string) (keys []string, err error)
Value(id, key string) (string, error)
SetValue(id, key, value string) error
DeleteValue(id, key, value string) error
Get() (map[string]map[string]string, error)
Details(appID string) (map[string]string, error)
}
//AppsConfig contains all Apps Configuration available actions
type AppsConfig struct {
c *Client
}
//List lists all the available apps
func (a *AppsConfig) List() (apps []string, err error) {
res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -18,9 +34,9 @@ func (c *Client) AppsConfigList() (apps []string, err error) {
return r.Ocs.Data.Data, nil return r.Ocs.Data.Data, nil
} }
//AppsConfigKeys returns the app's config keys //Keys returns the app's config keys
func (c *Client) AppsConfigKeys(id string) (keys []string, err error) { func (a *AppsConfig) Keys(id string) (keys []string, err error) {
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil, id) res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -29,9 +45,9 @@ func (c *Client) AppsConfigKeys(id string) (keys []string, err error) {
return r.Ocs.Data.Data, nil return r.Ocs.Data.Data, nil
} }
//AppsConfigValue get the config value for the given app's key //Value get the config value for the given app's key
func (c *Client) AppsConfigValue(id, key string) (string, error) { func (a *AppsConfig) Value(id, key string) (string, error) {
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil, id, key) res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil, id, key)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -40,28 +56,28 @@ func (c *Client) AppsConfigValue(id, key string) (string, error) {
return r.Ocs.Data.Data, nil return r.Ocs.Data.Data, nil
} }
//AppsConfigSetValue set the config value for the given app's key //SetValue set the config value for the given app's key
func (c *Client) AppsConfigSetValue(id, key, value string) error { func (a *AppsConfig) SetValue(id, key, value string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"value": value, "value": value,
}, },
} }
_, err := c.baseRequest(http.MethodPost, routes.appsConfig, ro, id, key) _, err := a.c.baseRequest(http.MethodPost, routes.appsConfig, ro, id, key)
return err return err
} }
//AppsConfigDeleteValue delete the config value and (!! be careful !!) the key //DeleteValue delete the config value and (!! be careful !!) the key
func (c *Client) AppsConfigDeleteValue(id, key, value string) error { func (a *AppsConfig) DeleteValue(id, key, value string) error {
_, err := c.baseRequest(http.MethodDelete, routes.appsConfig, nil, id, key) _, err := a.c.baseRequest(http.MethodDelete, routes.appsConfig, nil, id, key)
return err return err
} }
//AppsConfig returns all apps AppConfigDetails //Get returns all apps AppConfigDetails
func (c *Client) AppsConfig() (map[string]map[string]string, error) { func (a *AppsConfig) Get() (map[string]map[string]string, error) {
config := map[string]map[string]string{} config := map[string]map[string]string{}
m := sync.Mutex{} m := sync.Mutex{}
appsIDs, err := c.AppsConfigList() appsIDs, err := a.List()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -70,7 +86,7 @@ func (c *Client) AppsConfig() (map[string]map[string]string, error) {
for i := range appsIDs { for i := range appsIDs {
go func(id string) { go func(id string) {
defer wg.Done() defer wg.Done()
d, err := c.AppsConfigDetails(id) d, err := a.Details(id)
if err == nil { if err == nil {
m.Lock() m.Lock()
config[id] = d config[id] = d
@ -82,13 +98,13 @@ func (c *Client) AppsConfig() (map[string]map[string]string, error) {
return config, err return config, err
} }
//AppsConfigDetails returns all the config's key, values pair of the app //Details returns all the config's key, values pair of the app
func (c *Client) AppsConfigDetails(appID string) (map[string]string, error) { func (a *AppsConfig) Details(appID string) (map[string]string, error) {
config := map[string]string{} config := map[string]string{}
m := sync.Mutex{} m := sync.Mutex{}
var err error var err error
var ks []string var ks []string
ks, err = c.AppsConfigKeys(appID) ks, err = a.Keys(appID)
if err != nil { if err != nil {
return config, err return config, err
} }
@ -97,7 +113,7 @@ func (c *Client) AppsConfigDetails(appID string) (map[string]string, error) {
for i := range ks { for i := range ks {
go func(key string) { go func(key string) {
defer wg.Done() defer wg.Done()
v, err := c.AppsConfigValue(appID, key) v, err := a.Value(appID, key)
if err == nil { if err == nil {
m.Lock() m.Lock()
config[key] = v config[key] = v

View File

@ -10,7 +10,7 @@ func TestAppsConfig(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
ac, err := c.AppsConfig() ac, err := c.AppsConfig.Get()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, ac) assert.NotEmpty(t, ac)
} }
@ -20,7 +20,7 @@ func TestAppsConfigList(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
a, err := c.AppsConfigList() a, err := c.AppsConfig.List()
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, a, "files") assert.Contains(t, a, "files")
} }
@ -30,7 +30,7 @@ func TestAppsConfigKeys(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
ks, err := c.AppsConfigKeys("activity") ks, err := c.AppsConfig.Keys("activity")
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, ks, "enabled") assert.Contains(t, ks, "enabled")
} }
@ -40,7 +40,7 @@ func TestAppsConfigValue(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
k, err := c.AppsConfigValue("files", "enabled") k, err := c.AppsConfig.Value("files", "enabled")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "yes", k) assert.Equal(t, "yes", k)
} }
@ -50,7 +50,7 @@ func TestAppConfigDetails(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
d, err := c.AppsConfigDetails("activity") d, err := c.AppsConfig.Details("activity")
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, d) assert.NotEmpty(t, d)
} }

View File

@ -15,6 +15,14 @@ type Client struct {
headers map[string]string headers map[string]string
capabilities *types.Capabilities capabilities *types.Capabilities
version *types.Version version *types.Version
Apps *Apps
AppsConfig *AppsConfig
GroupFolders *GroupFolders
Notifications *Notifications
Shares *Shares
Users *Users
Groups *Groups
} }
// NewClient create a new Client from the Nextcloud Instance URL // NewClient create a new Client from the Nextcloud Instance URL
@ -27,12 +35,19 @@ func NewClient(hostname string) (*Client, error) {
} }
} }
c := Client{ c := &Client{
baseURL: baseURL, baseURL: baseURL,
headers: map[string]string{ headers: map[string]string{
"OCS-APIREQUEST": "true", "OCS-APIREQUEST": "true",
"Accept": "application/json", "Accept": "application/json",
}, },
} }
return &c, nil c.Apps = &Apps{c}
c.AppsConfig = &AppsConfig{c}
c.GroupFolders = &GroupFolders{c}
c.Notifications = &Notifications{c}
c.Shares = &Shares{c}
c.Users = &Users{c}
c.Groups = &Groups{c}
return c, nil
} }

2
doc.go
View File

@ -30,7 +30,7 @@ For example, to list all the Nextcloud's instance users:
} }
defer c.Logout() defer c.Logout()
users, err := c.UserList() users, err := c.List()
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -74,7 +74,7 @@ var (
{ {
"user list", "user list",
func(t *testing.T) { func(t *testing.T) {
us, err := c.UserList() us, err := c.Users.List()
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, us, config.Login) assert.Contains(t, us, config.Login)
}, },
@ -83,7 +83,7 @@ var (
{ {
"existing user", "existing user",
func(t *testing.T) { func(t *testing.T) {
u, err := c.User(config.Login) u, err := c.Users.Get(config.Login)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, u) assert.NotNil(t, u)
}, },
@ -92,7 +92,7 @@ var (
{ {
"empty user", "empty user",
func(t *testing.T) { func(t *testing.T) {
u, err := c.User("") u, err := c.Users.Get("")
assert.Error(t, err) assert.Error(t, err)
assert.Empty(t, u) assert.Empty(t, u)
}, },
@ -101,7 +101,7 @@ var (
{ {
"TestNonExistingUser", "TestNonExistingUser",
func(t *testing.T) { func(t *testing.T) {
_, err := c.User(config.NotExistingUser) _, err := c.Users.Get(config.NotExistingUser)
assert.Error(t, err) assert.Error(t, err)
}, },
}, },
@ -109,7 +109,7 @@ var (
{ {
"TestUserSearch", "TestUserSearch",
func(t *testing.T) { func(t *testing.T) {
us, err := c.UserSearch(config.Login) us, err := c.Users.Search(config.Login)
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, us, config.Login) assert.Contains(t, us, config.Login)
}, },
@ -118,7 +118,7 @@ var (
{ {
"TestUserCreate", "TestUserCreate",
func(t *testing.T) { func(t *testing.T) {
err := c.UserCreate(config.NotExistingUser, password, nil) err := c.Users.Create(config.NotExistingUser, password, nil)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
@ -129,7 +129,7 @@ var (
// return // return
// } // }
// username := fmt.Sprintf("%s-2", config.NotExistingUser) // username := fmt.Sprintf("%s-2", config.NotExistingUser)
// user := &types.User{ // user := &types.Users{
// ID: username, // ID: username,
// Displayname: strings.ToUpper(username), // Displayname: strings.ToUpper(username),
// Email: "some@address.com", // Email: "some@address.com",
@ -138,9 +138,9 @@ var (
// Phone: "42 42 242 424", // Phone: "42 42 242 424",
// Website: "my.site.com", // Website: "my.site.com",
// } // }
// err := c.UserCreate(username, password, user) // err := c.Users.Create(username, password, user)
// assert.NoError(t, err) // assert.NoError(t, err)
// u, err := c.User(username) // u, err := c.Users.Get(username)
// assert.NoError(t, err) // assert.NoError(t, err)
// o := structs.Map(user) // o := structs.Map(user)
// r := structs.Map(u) // r := structs.Map(u)
@ -151,7 +151,7 @@ var (
// assert.Equal(t, o[k], r[k]) // assert.Equal(t, o[k], r[k])
// } // }
// // Clean up // // Clean up
// err = c.UserDelete(u.ID) // err = c.Users.Delete(u.ID)
// assert.NoError(t, err) // assert.NoError(t, err)
// }, // },
//}, //},
@ -163,9 +163,9 @@ var (
// return // return
// } // }
// username := fmt.Sprintf("%s-2", config.NotExistingUser) // username := fmt.Sprintf("%s-2", config.NotExistingUser)
// err := c.UserCreate(username, password, nil) // err := c.Users.Create(username, password, nil)
// assert.NoError(t, err) // assert.NoError(t, err)
// user := &types.User{ // user := &types.Users{
// ID: username, // ID: username,
// Displayname: strings.ToUpper(username), // Displayname: strings.ToUpper(username),
// Email: "some@address.com", // Email: "some@address.com",
@ -174,9 +174,9 @@ var (
// Phone: "42 42 242 424", // Phone: "42 42 242 424",
// Website: "my.site.com", // Website: "my.site.com",
// } // }
// err = c.UserUpdate(user) // err = c.Users.Update(user)
// assert.NoError(t, err) // assert.NoError(t, err)
// u, err := c.User(username) // u, err := c.Users.Get(username)
// assert.NoError(t, err) // assert.NoError(t, err)
// o := structs.Map(user) // o := structs.Map(user)
// r := structs.Map(u) // r := structs.Map(u)
@ -187,14 +187,14 @@ var (
// assert.Equal(t, o[k], r[k]) // assert.Equal(t, o[k], r[k])
// } // }
// // Clean up // // Clean up
// err = c.UserDelete(u.ID) // err = c.Users.Delete(u.ID)
// assert.NoError(t, err) // assert.NoError(t, err)
// }, // },
//}, //},
{ {
"TestUserCreateExisting", "TestUserCreateExisting",
func(t *testing.T) { func(t *testing.T) {
err := c.UserCreate(config.Login, password, nil) err := c.Users.Create(config.Login, password, nil)
assert.Error(t, err) assert.Error(t, err)
}, },
}, },
@ -202,7 +202,7 @@ var (
{ {
"TestGroupList", "TestGroupList",
func(t *testing.T) { func(t *testing.T) {
gs, err := c.GroupList() gs, err := c.Groups.List()
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, gs, "admin") assert.Contains(t, gs, "admin")
}, },
@ -211,7 +211,7 @@ var (
{ {
"TestGroupCreate", "TestGroupCreate",
func(t *testing.T) { func(t *testing.T) {
err := c.GroupCreate(config.NotExistingGroup) err := c.Groups.Create(config.NotExistingGroup)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
@ -220,9 +220,9 @@ var (
"TestUserUpdateEmail", "TestUserUpdateEmail",
func(t *testing.T) { func(t *testing.T) {
email := "my@mail.com" email := "my@mail.com"
err := c.UserUpdateEmail(config.NotExistingUser, email) err := c.Users.UpdateEmail(config.NotExistingUser, email)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -236,9 +236,9 @@ var (
"TestUserUpdateDisplayName", "TestUserUpdateDisplayName",
func(t *testing.T) { func(t *testing.T) {
displayName := "Display Name" displayName := "Display Name"
err := c.UserUpdateDisplayName(config.NotExistingUser, displayName) err := c.Users.UpdateDisplayName(config.NotExistingUser, displayName)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -252,9 +252,9 @@ var (
"TestUserUpdatePhone", "TestUserUpdatePhone",
func(t *testing.T) { func(t *testing.T) {
phone := "+33 42 42 42 42" phone := "+33 42 42 42 42"
err := c.UserUpdatePhone(config.NotExistingUser, phone) err := c.Users.UpdatePhone(config.NotExistingUser, phone)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -268,9 +268,9 @@ var (
"TestUserUpdateAddress", "TestUserUpdateAddress",
func(t *testing.T) { func(t *testing.T) {
address := "Main Street, Galifrey" address := "Main Street, Galifrey"
err := c.UserUpdateAddress(config.NotExistingUser, address) err := c.Users.UpdateAddress(config.NotExistingUser, address)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -284,9 +284,9 @@ var (
"TestUserUpdateWebSite", "TestUserUpdateWebSite",
func(t *testing.T) { func(t *testing.T) {
website := "www.doctor.who" website := "www.doctor.who"
err := c.UserUpdateWebSite(config.NotExistingUser, website) err := c.Users.UpdateWebSite(config.NotExistingUser, website)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -299,9 +299,9 @@ var (
"TestUserUpdateTwitter", "TestUserUpdateTwitter",
func(t *testing.T) { func(t *testing.T) {
twitter := "@doctorwho" twitter := "@doctorwho"
err := c.UserUpdateTwitter(config.NotExistingUser, twitter) err := c.Users.UpdateTwitter(config.NotExistingUser, twitter)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -314,10 +314,10 @@ var (
"TestUserUpdateQuota", "TestUserUpdateQuota",
func(t *testing.T) { func(t *testing.T) {
quota := 1024 * 1024 * 1024 quota := 1024 * 1024 * 1024
err := c.UserUpdateQuota(config.NotExistingUser, quota) err := c.Users.UpdateQuota(config.NotExistingUser, quota)
assert.NoError(t, err) assert.NoError(t, err)
// TODO : Find better verification : A never connected User does not have quota available // TODO : Find better verification : A never connected Users does not have quota available
//u, err := c.User(config.NotExistingUser) //u, err := c.Users(config.NotExistingUser)
//assert.NoError(t, err) //assert.NoError(t, err)
//assert.Equal(t, quota, u.Quota.Quota) //assert.Equal(t, quota, u.Quota.Quota)
}, },
@ -326,15 +326,15 @@ var (
"TestUserUpdatePassword", "TestUserUpdatePassword",
func(t *testing.T) { func(t *testing.T) {
password := "newcomplexpassword" password := "newcomplexpassword"
err := c.UserUpdatePassword(config.NotExistingUser, password) err := c.Users.UpdatePassword(config.NotExistingUser, password)
assert.NoError(t, err) assert.NoError(t, err)
}}, }},
{ {
"TestUserGroupAdd", "TestUserGroupAdd",
func(t *testing.T) { func(t *testing.T) {
err := c.UserGroupAdd(config.NotExistingUser, config.NotExistingGroup) err := c.Users.GroupAdd(config.NotExistingUser, config.NotExistingGroup)
assert.NoError(t, err) assert.NoError(t, err)
gs, err := c.UserGroupList(config.NotExistingUser) gs, err := c.Users.GroupList(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, gs, config.NotExistingGroup) assert.Contains(t, gs, config.NotExistingGroup)
}, },
@ -342,7 +342,7 @@ var (
{ {
"TestUserGroupSubAdminList", "TestUserGroupSubAdminList",
func(t *testing.T) { func(t *testing.T) {
gs, err := c.UserGroupSubAdminList(config.NotExistingUser) gs, err := c.Users.GroupSubAdminList(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
assert.Empty(t, gs) assert.Empty(t, gs)
}, },
@ -350,9 +350,9 @@ var (
{ {
"TestUserGroupPromote", "TestUserGroupPromote",
func(t *testing.T) { func(t *testing.T) {
err := c.UserGroupPromote(config.NotExistingUser, config.NotExistingGroup) err := c.Users.GroupPromote(config.NotExistingUser, config.NotExistingGroup)
assert.NoError(t, err) assert.NoError(t, err)
gs, err := c.UserGroupSubAdminList(config.NotExistingUser) gs, err := c.Users.GroupSubAdminList(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, gs, config.NotExistingGroup) assert.Contains(t, gs, config.NotExistingGroup)
}, },
@ -360,7 +360,7 @@ var (
{ {
"TestUserGroupDemote", "TestUserGroupDemote",
func(t *testing.T) { func(t *testing.T) {
err := c.UserGroupDemote(config.NotExistingUser, config.NotExistingGroup) err := c.Users.GroupDemote(config.NotExistingUser, config.NotExistingGroup)
assert.NoError(t, err) assert.NoError(t, err)
//gs, err := c.UserGroupSubAdminList(config.NotExistingUser) //gs, err := c.UserGroupSubAdminList(config.NotExistingUser)
//assert.NoError(t, err) //assert.NoError(t, err)
@ -370,9 +370,9 @@ var (
{ {
"TestUserDisable", "TestUserDisable",
func(t *testing.T) { func(t *testing.T) {
err := c.UserDisable(config.NotExistingUser) err := c.Users.Disable(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -384,9 +384,9 @@ var (
{ {
"TestUserEnable", "TestUserEnable",
func(t *testing.T) { func(t *testing.T) {
err := c.UserEnable(config.NotExistingUser) err := c.Users.Enable(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
u, err := c.User(config.NotExistingUser) u, err := c.Users.Get(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
if err != nil { if err != nil {
t.Fail() t.Fail()
@ -398,14 +398,14 @@ var (
{ {
"TestGroupDelete", "TestGroupDelete",
func(t *testing.T) { func(t *testing.T) {
err := c.GroupDelete(config.NotExistingGroup) err := c.Groups.Delete(config.NotExistingGroup)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"TestUserDelete", "TestUserDelete",
func(t *testing.T) { func(t *testing.T) {
err := c.UserDelete(config.NotExistingUser) err := c.Users.Delete(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
@ -424,7 +424,7 @@ var (
if err := initClient(); err != nil { if err := initClient(); err != nil {
return return
} }
s, err := c.SharesList() s, err := c.Shares.List()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, s) assert.NotNil(t, s)
}, },
@ -482,9 +482,9 @@ func TestUserCreateWithoutPassword(t *testing.T) {
t.SkipNow() t.SkipNow()
} }
// Nextcloud does not seems to like recreating a deleted user // Nextcloud does not seems to like recreating a deleted user
err := c.UserCreateWithoutPassword(config.NotExistingUser, config.Email, strings.Title(config.NotExistingUser)) err := c.Users.CreateWithoutPassword(config.NotExistingUser, config.Email, strings.Title(config.NotExistingUser))
assert.NoError(t, err) assert.NoError(t, err)
err = c.UserDelete(config.NotExistingUser) err = c.Users.Delete(config.NotExistingUser)
assert.NoError(t, err) assert.NoError(t, err)
} }
@ -493,7 +493,7 @@ func TestUserListDetails(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
us, err := c.UserListDetails() us, err := c.Users.ListDetails()
assert.NoError(t, err) assert.NoError(t, err)
assert.Contains(t, us, config.Login) assert.Contains(t, us, config.Login)
} }
@ -503,7 +503,7 @@ func TestGroupListDetails(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
gs, err := c.GroupListDetails() gs, err := c.Groups.ListDetails()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, gs) assert.NotEmpty(t, gs)
} }

View File

@ -8,9 +8,26 @@ import (
"strconv" "strconv"
) )
//GroupFoldersList returns the groups folders //GroupFoldersI available methods
func (c *Client) GroupFoldersList() (map[int]types.GroupFolder, error) { type GroupFoldersI interface {
res, err := c.baseRequest(http.MethodGet, routes.groupfolders, nil) List() (map[int]types.GroupFolder, error)
Get(id int) (types.GroupFolder, error)
Create(name string) (id int, err error)
Rename(groupID int, name string) error
AddGroup(folderID int, groupName string) error
RemoveGroup(folderID int, groupName string) error
SetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error
SetQuota(folderID int, quota int) error
}
//GroupFolders contains all Groups Folders available actions
type GroupFolders struct {
c *Client
}
//List returns the groups folders
func (g *GroupFolders) List() (map[int]types.GroupFolder, error) {
res, err := g.c.baseRequest(http.MethodGet, routes.groupfolders, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -20,9 +37,9 @@ func (c *Client) GroupFoldersList() (map[int]types.GroupFolder, error) {
return gfs, nil return gfs, nil
} }
//GroupFolders returns the group folder details //Get returns the group folder details
func (c *Client) GroupFolders(id int) (types.GroupFolder, error) { func (g *GroupFolders) Get(id int) (types.GroupFolder, error) {
res, err := c.baseRequest(http.MethodGet, routes.groupfolders, nil, strconv.Itoa(id)) res, err := g.c.baseRequest(http.MethodGet, routes.groupfolders, nil, strconv.Itoa(id))
if err != nil { if err != nil {
return types.GroupFolder{}, err return types.GroupFolder{}, err
} }
@ -34,15 +51,15 @@ func (c *Client) GroupFolders(id int) (types.GroupFolder, error) {
return r.Ocs.Data.FormatGroupFolder(), nil return r.Ocs.Data.FormatGroupFolder(), nil
} }
//GroupFoldersCreate creates a group folder //Create creates a group folder
func (c *Client) GroupFoldersCreate(name string) (id int, err error) { func (g *GroupFolders) Create(name string) (id int, err error) {
// TODO: Validate Folder name // TODO: Validate Folder name
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"mountpoint": name, "mountpoint": name,
}, },
} }
res, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro) res, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -52,15 +69,15 @@ func (c *Client) GroupFoldersCreate(name string) (id int, err error) {
return id, nil return id, nil
} }
//GroupFoldersRename renames the group folder //Rename renames the group folder
func (c *Client) GroupFoldersRename(groupID int, name string) error { func (g *GroupFolders) Rename(groupID int, name string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"mountpoint": name, "mountpoint": name,
}, },
} }
// GroupFolders's response does not give any clues about success or failure // GroupFolders's response does not give any clues about success or failure
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(groupID), "mountpoint") _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(groupID), "mountpoint")
if err != nil { if err != nil {
return err return err
} }
@ -69,55 +86,55 @@ func (c *Client) GroupFoldersRename(groupID int, name string) error {
//TODO func (c *Client) GroupFoldersDelete(id int) error { //TODO func (c *Client) GroupFoldersDelete(id int) error {
//GroupFoldersAddGroup adds group to folder //AddGroup adds group to folder
func (c *Client) GroupFoldersAddGroup(folderID int, groupName string) error { func (g *GroupFolders) AddGroup(folderID int, groupName string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"group": groupName, "group": groupName,
}, },
} }
// GroupFolders's response does not give any clues about success or failure // GroupFolders's response does not give any clues about success or failure
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups") _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups")
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
//GroupFoldersRemoveGroup remove a group from the group folder //RemoveGroup remove a group from the group folder
func (c *Client) GroupFoldersRemoveGroup(folderID int, groupName string) error { func (g *GroupFolders) RemoveGroup(folderID int, groupName string) error {
// GroupFolders's response does not give any clues about success or failure // GroupFolders's response does not give any clues about success or failure
_, err := c.baseRequest(http.MethodDelete, routes.groupfolders, nil, strconv.Itoa(folderID), "groups", groupName) _, err := g.c.baseRequest(http.MethodDelete, routes.groupfolders, nil, strconv.Itoa(folderID), "groups", groupName)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
//GroupFoldersSetGroupPermissions set groups permissions //SetGroupPermissions set groups permissions
func (c *Client) GroupFoldersSetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error { func (g *GroupFolders) SetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"permissions": strconv.Itoa(int(permission)), "permissions": strconv.Itoa(int(permission)),
}, },
} }
// GroupFolders's response does not give any clues about success or failure // GroupFolders's response does not give any clues about success or failure
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups", groupName) _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups", groupName)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
//GroupFoldersSetQuota set quota on the group folder. quota in bytes, use -3 for unlimited //SetQuota set quota on the group folder. quota in bytes, use -3 for unlimited
func (c *Client) GroupFoldersSetQuota(folderID int, quota int) error { func (g *GroupFolders) SetQuota(folderID int, quota int) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"quota": strconv.Itoa(int(quota)), "quota": strconv.Itoa(int(quota)),
}, },
} }
// GroupFolders's response does not give any clues about success or failure // GroupFolders's response does not give any clues about success or failure
_, err := c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "quota") _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "quota")
if err != nil { if err != nil {
return err return err
} }

View File

@ -16,14 +16,14 @@ var (
func(t *testing.T) { func(t *testing.T) {
// Recreate client // Recreate client
var err error var err error
groupID, err = c.GroupFoldersCreate("API") groupID, err = c.GroupFolders.Create("API")
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"TestGroupFoldersList", "TestGroupFoldersList",
func(t *testing.T) { func(t *testing.T) {
gfs, err := c.GroupFoldersList() gfs, err := c.GroupFolders.List()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, gfs[groupID]) assert.NotNil(t, gfs[groupID])
}, },
@ -31,7 +31,7 @@ var (
{ {
"TestGroupFolders", "TestGroupFolders",
func(t *testing.T) { func(t *testing.T) {
gf, err := c.GroupFolders(groupID) gf, err := c.GroupFolders.Get(groupID)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, gf) assert.NotNil(t, gf)
}, },
@ -39,35 +39,35 @@ var (
{ {
"TestGroupFolderRename", "TestGroupFolderRename",
func(t *testing.T) { func(t *testing.T) {
err := c.GroupFoldersRename(groupID, "API_Renamed") err := c.GroupFolders.Rename(groupID, "API_Renamed")
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"TestGroupFoldersAddGroup", "TestGroupFoldersAddGroup",
func(t *testing.T) { func(t *testing.T) {
err := c.GroupFoldersAddGroup(groupID, "admin") err := c.GroupFolders.AddGroup(groupID, "admin")
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"TestGroupFoldersSetGroupPermissions", "TestGroupFoldersSetGroupPermissions",
func(t *testing.T) { func(t *testing.T) {
err := c.GroupFoldersSetGroupPermissions(groupID, "admin", types.ReadPermission) err := c.GroupFolders.SetGroupPermissions(groupID, "admin", types.ReadPermission)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"TestGroupFoldersSetQuota", "TestGroupFoldersSetQuota",
func(t *testing.T) { func(t *testing.T) {
err := c.GroupFoldersSetQuota(groupID, 100) err := c.GroupFolders.SetQuota(groupID, 100)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
{ {
"TestGroupFolderRemoveGroup", "TestGroupFolderRemoveGroup",
func(t *testing.T) { func(t *testing.T) {
err := c.GroupFoldersRemoveGroup(groupID, "admin") err := c.GroupFolders.RemoveGroup(groupID, "admin")
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },

View File

@ -6,9 +6,25 @@ import (
"net/http" "net/http"
) )
//GroupList lists the Nextcloud groups //GroupsI available methods
func (c *Client) GroupList() ([]string, error) { type GroupsI interface {
res, err := c.baseRequest(http.MethodGet, routes.groups, nil) List() ([]string, error)
ListDetails() ([]types.Group, error)
Users(name string) ([]string, error)
Search(search string) ([]string, error)
Create(name string) error
Delete(name string) error
SubAdminList(name string) ([]string, error)
}
//Groups contains all Groups available actions
type Groups struct {
c *Client
}
//List lists the Nextcloud groups
func (g *Groups) List() ([]string, error) {
res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -17,9 +33,9 @@ func (c *Client) GroupList() ([]string, error) {
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
//GroupListDetails lists the Nextcloud groups //ListDetails lists the Nextcloud groups
func (c *Client) GroupListDetails() ([]types.Group, error) { func (g *Groups) ListDetails() ([]types.Group, error) {
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, "details") res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil, "details")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -28,9 +44,9 @@ func (c *Client) GroupListDetails() ([]types.Group, error) {
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
//GroupUsers list the group's users //Users list the group's users
func (c *Client) GroupUsers(name string) ([]string, error) { func (g *Groups) Users(name string) ([]string, error) {
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, name) res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -39,12 +55,12 @@ func (c *Client) GroupUsers(name string) ([]string, error) {
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
//GroupSearch return the list of groups matching the search string //Search return the list of groups matching the search string
func (c *Client) GroupSearch(search string) ([]string, error) { func (g *Groups) Search(search string) ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"search": search}, Params: map[string]string{"search": search},
} }
res, err := c.baseRequest(http.MethodGet, routes.groups, ro) res, err := g.c.baseRequest(http.MethodGet, routes.groups, ro)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -53,24 +69,24 @@ func (c *Client) GroupSearch(search string) ([]string, error) {
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
//GroupCreate creates a group //Create creates a group
func (c *Client) GroupCreate(name string) error { func (g *Groups) Create(name string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"groupid": name, "groupid": name,
}, },
} }
return c.groupBaseRequest(http.MethodPost, ro) return g.baseRequest(http.MethodPost, ro)
} }
//GroupDelete deletes the group //Delete deletes the group
func (c *Client) GroupDelete(name string) error { func (g *Groups) Delete(name string) error {
return c.groupBaseRequest(http.MethodDelete, nil, name) return g.baseRequest(http.MethodDelete, nil, name)
} }
//GroupSubAdminList lists the group's subadmins //SubAdminList lists the group's subadmins
func (c *Client) GroupSubAdminList(name string) ([]string, error) { func (g *Groups) SubAdminList(name string) ([]string, error) {
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, name, "subadmins") res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil, name, "subadmins")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -79,7 +95,7 @@ func (c *Client) GroupSubAdminList(name string) ([]string, error) {
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
func (c *Client) groupBaseRequest(method string, ro *req.RequestOptions, subRoute ...string) error { func (g *Groups) baseRequest(method string, ro *req.RequestOptions, subRoute ...string) error {
_, err := c.baseRequest(method, routes.groups, ro, subRoute...) _, err := g.c.baseRequest(method, routes.groups, ro, subRoute...)
return err return err
} }

View File

@ -8,12 +8,28 @@ import (
"strconv" "strconv"
) )
//NotificationsList returns all the notifications //NotificationsI available methods
func (c *Client) NotificationsList() ([]types.Notification, error) { type NotificationsI interface {
if err := c.notificationsAvailable(); err != nil { List() ([]types.Notification, error)
Get(id int) (types.Notification, error)
Delete(id int) error
DeleteAll() error
Create(userID, title, message string) error
AdminAvailable() error
Available() error
}
//Notifications contains all Notifications available actions
type Notifications struct {
c *Client
}
//List returns all the notifications
func (n *Notifications) List() ([]types.Notification, error) {
if err := n.Available(); err != nil {
return nil, err return nil, err
} }
res, err := c.baseRequest(http.MethodGet, routes.notifications, nil) res, err := n.c.baseRequest(http.MethodGet, routes.notifications, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -22,12 +38,12 @@ func (c *Client) NotificationsList() ([]types.Notification, error) {
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
//Notifications returns the notification corresponding to the id //Get returns the notification corresponding to the id
func (c *Client) Notifications(id int) (types.Notification, error) { func (n *Notifications) Get(id int) (types.Notification, error) {
if err := c.notificationsAvailable(); err != nil { if err := n.Available(); err != nil {
return types.Notification{}, err return types.Notification{}, err
} }
res, err := c.baseRequest(http.MethodGet, routes.notifications, nil, strconv.Itoa(id)) res, err := n.c.baseRequest(http.MethodGet, routes.notifications, nil, strconv.Itoa(id))
if err != nil { if err != nil {
return types.Notification{}, err return types.Notification{}, err
} }
@ -36,27 +52,27 @@ func (c *Client) Notifications(id int) (types.Notification, error) {
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
//NotificationsDelete deletes the notification corresponding to the id //Delete deletes the notification corresponding to the id
func (c *Client) NotificationsDelete(id int) error { func (n *Notifications) Delete(id int) error {
if err := c.notificationsAvailable(); err != nil { if err := n.Available(); err != nil {
return err return err
} }
_, err := c.baseRequest(http.MethodDelete, routes.notifications, nil, strconv.Itoa(id)) _, err := n.c.baseRequest(http.MethodDelete, routes.notifications, nil, strconv.Itoa(id))
return err return err
} }
//NotificationsDeleteAll deletes all notifications //DeleteAll deletes all notifications
func (c *Client) NotificationsDeleteAll() error { func (n *Notifications) DeleteAll() error {
if err := c.notificationsAvailable(); err != nil { if err := n.Available(); err != nil {
return err return err
} }
_, err := c.baseRequest(http.MethodDelete, routes.notifications, nil) _, err := n.c.baseRequest(http.MethodDelete, routes.notifications, nil)
return err return err
} }
//NotificationsCreate creates a notification (if the user is an admin) //Create creates a notification (if the user is an admin)
func (c *Client) NotificationsCreate(userID, title, message string) error { func (n *Notifications) Create(userID, title, message string) error {
if err := c.adminNotificationsAvailable(); err != nil { if err := n.AdminAvailable(); err != nil {
return err return err
} }
ro := &req.RequestOptions{ ro := &req.RequestOptions{
@ -65,18 +81,21 @@ func (c *Client) NotificationsCreate(userID, title, message string) error {
"longMessage": message, "longMessage": message,
}, },
} }
_, err := c.baseRequest(http.MethodPost, routes.adminNotifications, ro, userID) _, err := n.c.baseRequest(http.MethodPost, routes.adminNotifications, ro, userID)
return err return err
} }
func (c *Client) adminNotificationsAvailable() error { //AdminAvailable returns an error if the admin-notifications app is not installed
if len(c.capabilities.Notifications.AdminNotifications) == 0 { func (n *Notifications) AdminAvailable() error {
if len(n.c.capabilities.Notifications.AdminNotifications) == 0 {
return errors.New("'admin notifications' not available on this instance") return errors.New("'admin notifications' not available on this instance")
} }
return nil return nil
} }
func (c *Client) notificationsAvailable() error {
if len(c.capabilities.Notifications.OcsEndpoints) == 0 { //Available returns an error if the notifications app is not installed
func (n *Notifications) Available() error {
if len(n.c.capabilities.Notifications.OcsEndpoints) == 0 {
return errors.New("notifications not available on this instance") return errors.New("notifications not available on this instance")
} }
return nil return nil

View File

@ -17,14 +17,14 @@ var (
{ {
"notificationCreate", "notificationCreate",
func(t *testing.T) { func(t *testing.T) {
err := c.NotificationsCreate(config.Login, title, message) err := c.Notifications.Create(config.Login, title, message)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, { }, {
"notificationDelete", "notificationDelete",
func(t *testing.T) { func(t *testing.T) {
// Get created Notification ID // Get created Notification ID
ns, err := c.NotificationsList() ns, err := c.Notifications.List()
if err != nil { if err != nil {
t.SkipNow() t.SkipNow()
} }
@ -37,7 +37,7 @@ var (
if createdID == 0 { if createdID == 0 {
t.SkipNow() t.SkipNow()
} }
err = c.NotificationsDelete(createdID) err = c.Notifications.Delete(createdID)
assert.NoError(t, err) assert.NoError(t, err)
}, },
}, },
@ -49,10 +49,10 @@ func TestNotificationsList(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := c.notificationsAvailable(); err != nil { if err := c.Notifications.Available(); err != nil {
t.SkipNow() t.SkipNow()
} }
ns, err := c.NotificationsList() ns, err := c.Notifications.List()
assert.NoError(t, err) assert.NoError(t, err)
if len(ns) > 0 { if len(ns) > 0 {
notificationID = ns[0].NotificationID notificationID = ns[0].NotificationID
@ -67,10 +67,10 @@ func TestNotifications(t *testing.T) {
if err := initClient(); err != nil { if err := initClient(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := c.notificationsAvailable(); err != nil { if err := c.Notifications.Available(); err != nil {
t.SkipNow() t.SkipNow()
} }
n, err := c.Notifications(notificationID) n, err := c.Notifications.Get(notificationID)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, n) assert.NotEmpty(t, n)
} }
@ -81,7 +81,7 @@ func TestNotifications(t *testing.T) {
// if err := initClient(); err != nil { // if err := initClient(); err != nil {
// t.Fatal(err) // t.Fatal(err)
// } // }
// if err := c.adminNotificationsAvailable(); err != nil { // if err := c.AdminAvailable(); err != nil {
// t.SkipNow() // t.SkipNow()
// } // }
// for _, test := range tests { // for _, test := range tests {

View File

@ -9,9 +9,35 @@ import (
"sync" "sync"
) )
//SharesList list all shares of the logged in user //SharesI available methods
func (c *Client) SharesList() ([]types.Share, error) { type SharesI interface {
res, err := c.baseRequest(http.MethodGet, routes.shares, nil) List() ([]types.Share, error)
GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error)
Get(shareID string) (types.Share, error)
Create(
path string,
shareType types.ShareType,
permission types.SharePermission,
shareWith string,
publicUpload bool,
password string,
) (types.Share, error)
Delete(shareID int) error
Update(shareUpdate types.ShareUpdate) error
UpdateExpireDate(shareID int, expireDate string) error
UpdatePublicUpload(shareID int, public bool) error
UpdatePassword(shareID int, password string) error
UpdatePermissions(shareID int, permissions types.SharePermission) error
}
//Shares contains all Shares available actions
type Shares struct {
c *Client
}
//List list all shares of the logged in user
func (s *Shares) List() ([]types.Share, error) {
res, err := s.c.baseRequest(http.MethodGet, routes.shares, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -20,8 +46,8 @@ func (c *Client) SharesList() ([]types.Share, error) {
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
//Shares return shares from a specific file or folder //GetFromPath return shares from a specific file or folder
func (c *Client) Shares(path string, reshares bool, subfiles bool) ([]types.Share, error) { func (s *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{ Params: map[string]string{
"path": path, "path": path,
@ -29,7 +55,7 @@ func (c *Client) Shares(path string, reshares bool, subfiles bool) ([]types.Shar
"subfiles": strconv.FormatBool(subfiles), "subfiles": strconv.FormatBool(subfiles),
}, },
} }
res, err := c.baseRequest(http.MethodGet, routes.shares, ro) res, err := s.c.baseRequest(http.MethodGet, routes.shares, ro)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -38,9 +64,9 @@ func (c *Client) Shares(path string, reshares bool, subfiles bool) ([]types.Shar
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
//Share Get information about a known Share //Get information about a known Share
func (c *Client) Share(shareID string) (types.Share, error) { func (s *Shares) Get(shareID string) (types.Share, error) {
res, err := c.baseRequest(http.MethodGet, routes.shares, nil, shareID) res, err := s.c.baseRequest(http.MethodGet, routes.shares, nil, shareID)
if err != nil { if err != nil {
return types.Share{}, err return types.Share{}, err
} }
@ -49,8 +75,8 @@ func (c *Client) Share(shareID string) (types.Share, error) {
return r.Ocs.Data[0], nil return r.Ocs.Data[0], nil
} }
//ShareCreate create a share //Create create a share
func (c *Client) ShareCreate( func (s *Shares) Create(
path string, path string,
shareType types.ShareType, shareType types.ShareType,
permission types.SharePermission, permission types.SharePermission,
@ -72,7 +98,7 @@ func (c *Client) ShareCreate(
"permissions": strconv.Itoa(int(permission)), "permissions": strconv.Itoa(int(permission)),
}, },
} }
res, err := c.baseRequest(http.MethodPost, routes.shares, ro) res, err := s.c.baseRequest(http.MethodPost, routes.shares, ro)
if err != nil { if err != nil {
return types.Share{}, err return types.Share{}, err
} }
@ -81,21 +107,21 @@ func (c *Client) ShareCreate(
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
//ShareDelete Remove the given share. //Delete Remove the given share.
func (c *Client) ShareDelete(shareID int) error { func (s *Shares) Delete(shareID int) error {
_, err := c.baseRequest(http.MethodDelete, routes.shares, nil, strconv.Itoa(shareID)) _, err := s.c.baseRequest(http.MethodDelete, routes.shares, nil, strconv.Itoa(shareID))
return err return err
} }
// ShareUpdate update share details // Update update share details
// expireDate expireDate expects a well formatted date string, e.g. YYYY-MM-DD // expireDate expireDate expects a well formatted date string, e.g. YYYY-MM-DD
func (c *Client) ShareUpdate(shareUpdate types.ShareUpdate) error { func (s *Shares) Update(shareUpdate types.ShareUpdate) error {
errs := make(chan types.UpdateError) errs := make(chan types.UpdateError)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(4) wg.Add(4)
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := c.ShareUpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil { if err := s.UpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil {
errs <- types.UpdateError{ errs <- types.UpdateError{
Field: "password", Field: "password",
Error: err, Error: err,
@ -104,7 +130,7 @@ func (c *Client) ShareUpdate(shareUpdate types.ShareUpdate) error {
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := c.ShareUpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil { if err := s.UpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil {
errs <- types.UpdateError{ errs <- types.UpdateError{
Field: "expireDate", Field: "expireDate",
Error: err, Error: err,
@ -113,7 +139,7 @@ func (c *Client) ShareUpdate(shareUpdate types.ShareUpdate) error {
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := c.ShareUpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil { if err := s.UpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil {
errs <- types.UpdateError{ errs <- types.UpdateError{
Field: "permissions", Field: "permissions",
Error: err, Error: err,
@ -122,7 +148,7 @@ func (c *Client) ShareUpdate(shareUpdate types.ShareUpdate) error {
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := c.ShareUpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil { if err := s.UpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil {
errs <- types.UpdateError{ errs <- types.UpdateError{
Field: "publicUpload", Field: "publicUpload",
Error: err, Error: err,
@ -136,31 +162,31 @@ func (c *Client) ShareUpdate(shareUpdate types.ShareUpdate) error {
return types.NewUpdateError(errs) return types.NewUpdateError(errs)
} }
// ShareUpdateExpireDate updates the share's expire date //UpdateExpireDate updates the share's expire date
// expireDate expects a well formatted date string, e.g. YYYY-MM-DD // expireDate expects a well formatted date string, e.g. YYYY-MM-DD
func (c *Client) ShareUpdateExpireDate(shareID int, expireDate string) error { func (s *Shares) UpdateExpireDate(shareID int, expireDate string) error {
return c.baseShareUpdate(strconv.Itoa(shareID), "expireDate", expireDate) return s.baseShareUpdate(strconv.Itoa(shareID), "expireDate", expireDate)
} }
//ShareUpdatePublicUpload enable or disable public upload //UpdatePublicUpload enable or disable public upload
func (c *Client) ShareUpdatePublicUpload(shareID int, public bool) error { func (s *Shares) UpdatePublicUpload(shareID int, public bool) error {
return c.baseShareUpdate(strconv.Itoa(shareID), "publicUpload", strconv.FormatBool(public)) return s.baseShareUpdate(strconv.Itoa(shareID), "publicUpload", strconv.FormatBool(public))
} }
//ShareUpdatePassword updates share password //UpdatePassword updates share password
func (c *Client) ShareUpdatePassword(shareID int, password string) error { func (s *Shares) UpdatePassword(shareID int, password string) error {
return c.baseShareUpdate(strconv.Itoa(shareID), "password", password) return s.baseShareUpdate(strconv.Itoa(shareID), "password", password)
} }
//ShareUpdatePermissions update permissions //UpdatePermissions update permissions
func (c *Client) ShareUpdatePermissions(shareID int, permissions types.SharePermission) error { func (s *Shares) UpdatePermissions(shareID int, permissions types.SharePermission) error {
return c.baseShareUpdate(strconv.Itoa(shareID), "permissions", strconv.Itoa(int(permissions))) return s.baseShareUpdate(strconv.Itoa(shareID), "permissions", strconv.Itoa(int(permissions)))
} }
func (c *Client) baseShareUpdate(shareID string, key string, value string) error { func (s *Shares) baseShareUpdate(shareID string, key string, value string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{key: value}, Data: map[string]string{key: value},
} }
_, err := c.baseRequest(http.MethodPut, routes.shares, ro, shareID) _, err := s.c.baseRequest(http.MethodPut, routes.shares, ro, shareID)
return err return err
} }

View File

@ -1,6 +1,6 @@
package types package types
//User //Users
type User struct { type User struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
ID string `json:"id"` ID string `json:"id"`

204
users.go
View File

@ -13,9 +13,43 @@ import (
"sync" "sync"
) )
// UserList return the Nextcloud'user list //UsersI available methods
func (c *Client) UserList() ([]string, error) { type UsersI interface {
res, err := c.baseRequest(http.MethodGet, routes.users, nil) List() ([]string, error)
ListDetails() (map[string]types.User, error)
Get(name string) (*types.User, error)
Search(search string) ([]string, error)
Create(username string, password string, user *types.User) error
CreateWithoutPassword(username, email, displayName string) error
Delete(name string) error
Enable(name string) error
Disable(name string) error
SendWelcomeEmail(name string) error
Update(user *types.User) error
UpdateEmail(name string, email string) error
UpdateDisplayName(name string, displayName string) error
UpdatePhone(name string, phone string) error
UpdateAddress(name string, address string) error
UpdateWebSite(name string, website string) error
UpdateTwitter(name string, twitter string) error
UpdatePassword(name string, password string) error
UpdateQuota(name string, quota int) error
GroupList(name string) ([]string, error)
GroupAdd(name string, group string) error
GroupRemove(name string, group string) error
GroupPromote(name string, group string) error
GroupDemote(name string, group string) error
GroupSubAdminList(name string) ([]string, error)
}
//Users contains all Users available actions
type Users struct {
c *Client
}
// List return the Nextcloud'user list
func (u *Users) List() ([]string, error) {
res, err := u.c.baseRequest(http.MethodGet, routes.users, nil)
//res, err := c.session.Get(u.String(), nil) //res, err := c.session.Get(u.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -25,9 +59,9 @@ func (c *Client) UserList() ([]string, error) {
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
//UserListDetails return a map of user with details //ListDetails return a map of user with details
func (c *Client) UserListDetails() (map[string]types.User, error) { func (u *Users) ListDetails() (map[string]types.User, error) {
res, err := c.baseRequest(http.MethodGet, routes.users, nil, "details") res, err := u.c.baseRequest(http.MethodGet, routes.users, nil, "details")
//res, err := c.session.Get(u.String(), nil) //res, err := c.session.Get(u.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -37,12 +71,12 @@ func (c *Client) UserListDetails() (map[string]types.User, error) {
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
// User return the details about the specified user // Get return the details about the specified user
func (c *Client) User(name string) (*types.User, error) { func (u *Users) Get(name string) (*types.User, error) {
if name == "" { if name == "" {
return nil, &types.APIError{Message: "name cannot be empty"} return nil, &types.APIError{Message: "name cannot be empty"}
} }
res, err := c.baseRequest(http.MethodGet, routes.users, nil, name) res, err := u.c.baseRequest(http.MethodGet, routes.users, nil, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -56,12 +90,12 @@ func (c *Client) User(name string) (*types.User, error) {
return &r.Ocs.Data, nil return &r.Ocs.Data, nil
} }
// UserSearch returns the users whose name match the search string // Search returns the users whose name match the search string
func (c *Client) UserSearch(search string) ([]string, error) { func (u *Users) Search(search string) ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"search": search}, Params: map[string]string{"search": search},
} }
res, err := c.baseRequest(http.MethodGet, routes.users, ro) res, err := u.c.baseRequest(http.MethodGet, routes.users, ro)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -70,16 +104,16 @@ func (c *Client) UserSearch(search string) ([]string, error) {
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
// UserCreate create a new user // Create create a new user
func (c *Client) UserCreate(username string, password string, user *types.User) error { func (u *Users) Create(username string, password string, user *types.User) error {
// Create base User // Create base Users
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"userid": username, "userid": username,
"password": password, "password": password,
}, },
} }
if err := c.userBaseRequest(http.MethodPost, ro); err != nil { if err := u.baseRequest(http.MethodPost, ro); err != nil {
return err return err
} }
// Check if we need to add user details information // Check if we need to add user details information
@ -87,13 +121,13 @@ func (c *Client) UserCreate(username string, password string, user *types.User)
return nil return nil
} }
// Add user details information // Add user details information
return c.UserUpdate(user) return u.Update(user)
} }
// UserCreateWithoutPassword create a user without provisioning a password, the email address must be provided to send // CreateWithoutPassword create a user without provisioning a password, the email address must be provided to send
// an init password email // an init password email
func (c *Client) UserCreateWithoutPassword(username, email, displayName string) error { func (u *Users) CreateWithoutPassword(username, email, displayName string) error {
if c.version.Major < 14 { if u.c.version.Major < 14 {
return errors.New("unsupported method: requires Nextcloud 14+") return errors.New("unsupported method: requires Nextcloud 14+")
} }
if username == "" || email == "" { if username == "" || email == "" {
@ -107,40 +141,40 @@ func (c *Client) UserCreateWithoutPassword(username, email, displayName string)
}, },
} }
if err := c.userBaseRequest(http.MethodPost, ro); err != nil { if err := u.baseRequest(http.MethodPost, ro); err != nil {
return err return err
} }
return nil return nil
} }
//UserDelete delete the user //Delete delete the user
func (c *Client) UserDelete(name string) error { func (u *Users) Delete(name string) error {
return c.userBaseRequest(http.MethodDelete, nil, name) return u.baseRequest(http.MethodDelete, nil, name)
} }
//UserEnable enables the user //Enable enables the user
func (c *Client) UserEnable(name string) error { func (u *Users) Enable(name string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{}, Data: map[string]string{},
} }
return c.userBaseRequest(http.MethodPut, ro, name, "enable") return u.baseRequest(http.MethodPut, ro, name, "enable")
} }
//UserDisable disables the user //Disable disables the user
func (c *Client) UserDisable(name string) error { func (u *Users) Disable(name string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{}, Data: map[string]string{},
} }
return c.userBaseRequest(http.MethodPut, ro, name, "disable") return u.baseRequest(http.MethodPut, ro, name, "disable")
} }
//UserSendWelcomeEmail (re)send the welcome mail to the user (return an error if the user has not configured his email) //SendWelcomeEmail (re)send the welcome mail to the user (return an error if the user has not configured his email)
func (c *Client) UserSendWelcomeEmail(name string) error { func (u *Users) SendWelcomeEmail(name string) error {
return c.userBaseRequest(http.MethodPost, nil, name, "welcome") return u.baseRequest(http.MethodPost, nil, name, "welcome")
} }
//UserUpdate takes a *types.User struct to update the user's information //Update takes a *types.Users struct to update the user's information
func (c *Client) UserUpdate(user *types.User) error { func (u *Users) Update(user *types.User) error {
m := structs.Map(user) m := structs.Map(user)
errs := make(chan types.UpdateError) errs := make(chan types.UpdateError)
var wg sync.WaitGroup var wg sync.WaitGroup
@ -149,7 +183,7 @@ func (c *Client) UserUpdate(user *types.User) error {
wg.Add(1) wg.Add(1)
go func(key string, value string) { go func(key string, value string) {
defer wg.Done() defer wg.Done()
if err := c.userUpdateAttribute(user.ID, strings.ToLower(key), value); err != nil { if err := u.updateAttribute(user.ID, strings.ToLower(key), value); err != nil {
errs <- types.UpdateError{ errs <- types.UpdateError{
Field: key, Field: key,
Error: err, Error: err,
@ -165,49 +199,49 @@ func (c *Client) UserUpdate(user *types.User) error {
return types.NewUpdateError(errs) return types.NewUpdateError(errs)
} }
//UserUpdateEmail update the user's email //UpdateEmail update the user's email
func (c *Client) UserUpdateEmail(name string, email string) error { func (u *Users) UpdateEmail(name string, email string) error {
return c.userUpdateAttribute(name, "email", email) return u.updateAttribute(name, "email", email)
} }
//UserUpdateDisplayName update the user's display name //UpdateDisplayName update the user's display name
func (c *Client) UserUpdateDisplayName(name string, displayName string) error { func (u *Users) UpdateDisplayName(name string, displayName string) error {
return c.userUpdateAttribute(name, "displayname", displayName) return u.updateAttribute(name, "displayname", displayName)
} }
//UserUpdatePhone update the user's phone //UpdatePhone update the user's phone
func (c *Client) UserUpdatePhone(name string, phone string) error { func (u *Users) UpdatePhone(name string, phone string) error {
return c.userUpdateAttribute(name, "phone", phone) return u.updateAttribute(name, "phone", phone)
} }
//UserUpdateAddress update the user's address //UpdateAddress update the user's address
func (c *Client) UserUpdateAddress(name string, address string) error { func (u *Users) UpdateAddress(name string, address string) error {
return c.userUpdateAttribute(name, "address", address) return u.updateAttribute(name, "address", address)
} }
//UserUpdateWebSite update the user's website //UpdateWebSite update the user's website
func (c *Client) UserUpdateWebSite(name string, website string) error { func (u *Users) UpdateWebSite(name string, website string) error {
return c.userUpdateAttribute(name, "website", website) return u.updateAttribute(name, "website", website)
} }
//UserUpdateTwitter update the user's twitter //UpdateTwitter update the user's twitter
func (c *Client) UserUpdateTwitter(name string, twitter string) error { func (u *Users) UpdateTwitter(name string, twitter string) error {
return c.userUpdateAttribute(name, "twitter", twitter) return u.updateAttribute(name, "twitter", twitter)
} }
//UserUpdatePassword update the user's password //UpdatePassword update the user's password
func (c *Client) UserUpdatePassword(name string, password string) error { func (u *Users) UpdatePassword(name string, password string) error {
return c.userUpdateAttribute(name, "password", password) return u.updateAttribute(name, "password", password)
} }
//UserUpdateQuota update the user's quota (bytes) //UpdateQuota update the user's quota (bytes)
func (c *Client) UserUpdateQuota(name string, quota int) error { func (u *Users) UpdateQuota(name string, quota int) error {
return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota)) return u.updateAttribute(name, "quota", strconv.Itoa(quota))
} }
//UserGroupList lists the user's groups //GroupList lists the user's groups
func (c *Client) UserGroupList(name string) ([]string, error) { func (u *Users) GroupList(name string) ([]string, error) {
res, err := c.baseRequest(http.MethodGet, routes.users, nil, name, "groups") res, err := u.c.baseRequest(http.MethodGet, routes.users, nil, name, "groups")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -216,54 +250,54 @@ func (c *Client) UserGroupList(name string) ([]string, error) {
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
//UserGroupAdd adds a the user to the group //GroupAdd adds a the user to the group
func (c *Client) UserGroupAdd(name string, group string) error { func (u *Users) GroupAdd(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"groupid": group, "groupid": group,
}, },
} }
return c.userBaseRequest(http.MethodPost, ro, name, "groups") return u.baseRequest(http.MethodPost, ro, name, "groups")
} }
//UserGroupRemove removes the user from the group //GroupRemove removes the user from the group
func (c *Client) UserGroupRemove(name string, group string) error { func (u *Users) GroupRemove(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"groupid": group, "groupid": group,
}, },
} }
return c.userBaseRequest(http.MethodDelete, ro, name, "groups") return u.baseRequest(http.MethodDelete, ro, name, "groups")
} }
//UserGroupPromote promotes the user as group admin //GroupPromote promotes the user as group admin
func (c *Client) UserGroupPromote(name string, group string) error { func (u *Users) GroupPromote(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"groupid": group, "groupid": group,
}, },
} }
return c.userBaseRequest(http.MethodPost, ro, name, "subadmins") return u.baseRequest(http.MethodPost, ro, name, "subadmins")
} }
//UserGroupDemote demotes the user //GroupDemote demotes the user
func (c *Client) UserGroupDemote(name string, group string) error { func (u *Users) GroupDemote(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"groupid": group, "groupid": group,
}, },
} }
return c.userBaseRequest(http.MethodDelete, ro, name, "subadmins") return u.baseRequest(http.MethodDelete, ro, name, "subadmins")
} }
//UserGroupSubAdminList lists the groups where he is subadmin //GroupSubAdminList lists the groups where he is subadmin
func (c *Client) UserGroupSubAdminList(name string) ([]string, error) { func (u *Users) GroupSubAdminList(name string) ([]string, error) {
if !c.loggedIn() { if !u.c.loggedIn() {
return nil, errUnauthorized return nil, errUnauthorized
} }
u := c.baseURL.ResolveReference(routes.users) ur := u.c.baseURL.ResolveReference(routes.users)
u.Path = path.Join(u.Path, name, "subadmins") ur.Path = path.Join(ur.Path, name, "subadmins")
res, err := c.session.Get(u.String(), nil) res, err := u.c.session.Get(ur.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -272,18 +306,18 @@ func (c *Client) UserGroupSubAdminList(name string) ([]string, error) {
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
func (c *Client) userUpdateAttribute(name string, key string, value string) error { func (u *Users) updateAttribute(name string, key string, value string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"key": key, "key": key,
"value": value, "value": value,
}, },
} }
return c.userBaseRequest(http.MethodPut, ro, name) return u.baseRequest(http.MethodPut, ro, name)
} }
func (c *Client) userBaseRequest(method string, ro *req.RequestOptions, subRoutes ...string) error { func (u *Users) baseRequest(method string, ro *req.RequestOptions, subRoutes ...string) error {
_, err := c.baseRequest(method, routes.users, ro, subRoutes...) _, err := u.c.baseRequest(method, routes.users, ro, subRoutes...)
return err return err
} }