added comments

This commit is contained in:
Philippe-Adrien Nousse 2018-07-30 10:03:38 +02:00
parent ce62b5147c
commit 5a57427053
7 changed files with 46 additions and 18 deletions

20
apps.go
View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
) )
//AppList return the list of the Nextcloud Apps
func (c *Client) AppList() ([]string, error) { func (c *Client) AppList() ([]string, error) {
res, err := c.baseRequest(routes.apps, "", "", nil, http.MethodGet) res, err := c.baseRequest(routes.apps, "", "", nil, http.MethodGet)
if err != nil { if err != nil {
@ -16,6 +17,7 @@ func (c *Client) AppList() ([]string, error) {
return r.Ocs.Data.Apps, nil return r.Ocs.Data.Apps, nil
} }
//AppListEnabled lists the enabled apps
func (c *Client) AppListEnabled() ([]string, error) { func (c *Client) AppListEnabled() ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"filter": "enabled"}, Params: map[string]string{"filter": "enabled"},
@ -29,6 +31,7 @@ func (c *Client) AppListEnabled() ([]string, error) {
return r.Ocs.Data.Apps, nil return r.Ocs.Data.Apps, nil
} }
//AppListDisabled lists the disabled apps
func (c *Client) AppListDisabled() ([]string, error) { func (c *Client) AppListDisabled() ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"filter": "disabled"}, Params: map[string]string{"filter": "disabled"},
@ -42,6 +45,7 @@ func (c *Client) AppListDisabled() ([]string, error) {
return r.Ocs.Data.Apps, nil return r.Ocs.Data.Apps, nil
} }
//AppInfos return the app's details
func (c *Client) AppInfos(name string) (types.App, error) { func (c *Client) AppInfos(name string) (types.App, error) {
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodGet) res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodGet)
if err != nil { if err != nil {
@ -52,25 +56,17 @@ func (c *Client) AppInfos(name string) (types.App, error) {
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
//AppEnable enables an app
func (c *Client) AppEnable(name string) error { func (c *Client) AppEnable(name string) error {
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodPut) _, err := c.baseRequest(routes.apps, name, "", nil, http.MethodPut)
if err != nil {
return err return err
} }
var r types.BaseResponse
res.JSON(&r)
return nil
}
//AppDisable disables an app
func (c *Client) AppDisable(name string) error { func (c *Client) AppDisable(name string) error {
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodDelete) _, err := c.baseRequest(routes.apps, name, "", nil, http.MethodDelete)
if err != nil {
return err return err
} }
var r types.BaseResponse
res.JSON(&r)
return nil
}
func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error { func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error {
_, err := c.baseRequest(routes.apps, name, route, ro, method) _, err := c.baseRequest(routes.apps, name, route, ro, method)

View File

@ -8,6 +8,7 @@ import (
var unauthorized = fmt.Errorf("login first") var unauthorized = fmt.Errorf("login first")
// Login perform login and create a session with the Nextcloud API.
func (c *Client) Login(username string, password string) error { func (c *Client) Login(username string, password string) error {
c.username = username c.username = username
c.password = password c.password = password
@ -34,6 +35,7 @@ func (c *Client) Login(username string, password string) error {
return nil return nil
} }
// Logout logs out from the Nextcloud API, close the session and delete session's cookie
func (c *Client) Logout() error { func (c *Client) Logout() error {
c.session.CloseIdleConnections() c.session.CloseIdleConnections()
c.session.HTTPClient.Jar = nil c.session.HTTPClient.Jar = nil

View File

@ -46,6 +46,7 @@ import (
"net/url" "net/url"
) )
// Client is the API client that performs all operations against a Nextcloud server.
type Client struct { type Client struct {
baseURL *url.URL baseURL *url.URL
username string username string
@ -55,6 +56,7 @@ type Client struct {
capabilities *types.Capabilities capabilities *types.Capabilities
} }
// NewClient create a new Client from the Nextcloud Instance URL
func NewClient(hostname string) (*Client, error) { func NewClient(hostname string) (*Client, error) {
baseURL, err := url.ParseRequestURI(hostname) baseURL, err := url.ParseRequestURI(hostname)
if err != nil { if err != nil {

View File

@ -29,6 +29,7 @@ type Config struct {
NotExistingGroup string `yaml:"not-existing-group"` NotExistingGroup string `yaml:"not-existing-group"`
} }
// LoadConfig loads the test configuration
func LoadConfig() error { func LoadConfig() error {
f, err := os.Open("./config.yml") f, err := os.Open("./config.yml")
if err != nil { if err != nil {
@ -38,10 +39,7 @@ func LoadConfig() error {
if err != nil { if err != nil {
return err return err
} }
if err := yaml.Unmarshal(b, &config); err != nil { return yaml.Unmarshal(b, &config)
return err
}
return nil
} }
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
) )
//GroupList lists the Nextcloud groups
func (c *Client) GroupList() ([]string, error) { func (c *Client) GroupList() ([]string, error) {
res, err := c.baseRequest(routes.groups, "", "", nil, http.MethodGet) res, err := c.baseRequest(routes.groups, "", "", nil, http.MethodGet)
if err != nil { if err != nil {
@ -16,6 +17,7 @@ func (c *Client) GroupList() ([]string, error) {
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
//GroupUsers list the group's users
func (c *Client) GroupUsers(name string) ([]string, error) { func (c *Client) GroupUsers(name string) ([]string, error) {
res, err := c.baseRequest(routes.groups, name, "", nil, http.MethodGet) res, err := c.baseRequest(routes.groups, name, "", nil, http.MethodGet)
if err != nil { if err != nil {
@ -26,6 +28,7 @@ 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
func (c *Client) GroupSearch(search string) ([]string, error) { func (c *Client) GroupSearch(search string) ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"search": search}, Params: map[string]string{"search": search},
@ -39,6 +42,7 @@ func (c *Client) GroupSearch(search string) ([]string, error) {
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
//GroupCreate creates a group
func (c *Client) GroupCreate(name string) error { func (c *Client) GroupCreate(name string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
@ -51,6 +55,7 @@ func (c *Client) GroupCreate(name string) error {
return nil return nil
} }
//GroupDelete deletes the group
func (c *Client) GroupDelete(name string) error { func (c *Client) GroupDelete(name string) error {
if err := c.groupBaseRequest(name, "", nil, http.MethodDelete); err != nil { if err := c.groupBaseRequest(name, "", nil, http.MethodDelete); err != nil {
return err return err
@ -58,6 +63,7 @@ func (c *Client) GroupDelete(name string) error {
return nil return nil
} }
//GroupSubAdminList lists the group's subadmins
func (c *Client) GroupSubAdminList(name string) ([]string, error) { func (c *Client) GroupSubAdminList(name string) ([]string, error) {
res, err := c.baseRequest(routes.groups, name, "subadmins", nil, http.MethodGet) res, err := c.baseRequest(routes.groups, name, "subadmins", nil, http.MethodGet)
if err != nil { if err != nil {

View File

@ -2,6 +2,7 @@ package gonextcloud
import "net/url" import "net/url"
// Route references the available routes
type Routes struct { type Routes struct {
capabilities *url.URL capabilities *url.URL
users *url.URL users *url.URL

View File

@ -12,6 +12,7 @@ import (
"sync" "sync"
) )
// UserList return the Nextcloud'user list
func (c *Client) UserList() ([]string, error) { func (c *Client) UserList() ([]string, error) {
res, err := c.baseRequest(routes.users, "", "", nil, http.MethodGet) res, err := c.baseRequest(routes.users, "", "", nil, http.MethodGet)
//res, err := c.session.Get(u.String(), nil) //res, err := c.session.Get(u.String(), nil)
@ -23,6 +24,7 @@ func (c *Client) UserList() ([]string, error) {
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
// User return the details about the specified user
func (c *Client) User(name string) (*types.User, error) { func (c *Client) User(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"}
@ -41,6 +43,7 @@ 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
func (c *Client) UserSearch(search string) ([]string, error) { func (c *Client) UserSearch(search string) ([]string, error) {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"search": search}, Params: map[string]string{"search": search},
@ -54,6 +57,7 @@ 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
func (c *Client) UserCreate(username string, password string, user *types.User) error { func (c *Client) UserCreate(username string, password string, user *types.User) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
@ -70,10 +74,12 @@ func (c *Client) UserCreate(username string, password string, user *types.User)
return c.UserUpdate(user) return c.UserUpdate(user)
} }
//UserDelete delete the user
func (c *Client) UserDelete(name string) error { func (c *Client) UserDelete(name string) error {
return c.userBaseRequest(name, "", nil, http.MethodDelete) return c.userBaseRequest(name, "", nil, http.MethodDelete)
} }
//UserEnable enables the user
func (c *Client) UserEnable(name string) error { func (c *Client) UserEnable(name string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{}, Data: map[string]string{},
@ -81,6 +87,7 @@ func (c *Client) UserEnable(name string) error {
return c.userBaseRequest(name, "enable", ro, http.MethodPut) return c.userBaseRequest(name, "enable", ro, http.MethodPut)
} }
//UserDisable disables the user
func (c *Client) UserDisable(name string) error { func (c *Client) UserDisable(name string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{}, Data: map[string]string{},
@ -88,10 +95,12 @@ func (c *Client) UserDisable(name string) error {
return c.userBaseRequest(name, "disable", ro, http.MethodPut) return c.userBaseRequest(name, "disable", ro, http.MethodPut)
} }
//UserSendWelcomeEmail (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 (c *Client) UserSendWelcomeEmail(name string) error {
return c.userBaseRequest(name, "welcome", nil, http.MethodPost) return c.userBaseRequest(name, "welcome", nil, http.MethodPost)
} }
//UserUpdate takes a *types.User struct to update the user's information
func (c *Client) UserUpdate(user *types.User) error { func (c *Client) UserUpdate(user *types.User) error {
m := structs.Map(user) m := structs.Map(user)
errs := make(chan types.UpdateError) errs := make(chan types.UpdateError)
@ -117,38 +126,47 @@ func (c *Client) UserUpdate(user *types.User) error {
return types.NewUpdateError(errs) return types.NewUpdateError(errs)
} }
//UserUpdateEmail update the user's email
func (c *Client) UserUpdateEmail(name string, email string) error { func (c *Client) UserUpdateEmail(name string, email string) error {
return c.userUpdateAttribute(name, "email", email) return c.userUpdateAttribute(name, "email", email)
} }
//UserUpdateDisplayName update the user's display name
func (c *Client) UserUpdateDisplayName(name string, displayName string) error { func (c *Client) UserUpdateDisplayName(name string, displayName string) error {
return c.userUpdateAttribute(name, "displayname", displayName) return c.userUpdateAttribute(name, "displayname", displayName)
} }
//UserUpdatePhone update the user's phone
func (c *Client) UserUpdatePhone(name string, phone string) error { func (c *Client) UserUpdatePhone(name string, phone string) error {
return c.userUpdateAttribute(name, "phone", phone) return c.userUpdateAttribute(name, "phone", phone)
} }
//UserUpdateAddress update the user's address
func (c *Client) UserUpdateAddress(name string, address string) error { func (c *Client) UserUpdateAddress(name string, address string) error {
return c.userUpdateAttribute(name, "address", address) return c.userUpdateAttribute(name, "address", address)
} }
//UserUpdateWebSite update the user's website
func (c *Client) UserUpdateWebSite(name string, website string) error { func (c *Client) UserUpdateWebSite(name string, website string) error {
return c.userUpdateAttribute(name, "website", website) return c.userUpdateAttribute(name, "website", website)
} }
//UserUpdateTwitter update the user's twitter
func (c *Client) UserUpdateTwitter(name string, twitter string) error { func (c *Client) UserUpdateTwitter(name string, twitter string) error {
return c.userUpdateAttribute(name, "twitter", twitter) return c.userUpdateAttribute(name, "twitter", twitter)
} }
//UserUpdatePassword update the user's password
func (c *Client) UserUpdatePassword(name string, password string) error { func (c *Client) UserUpdatePassword(name string, password string) error {
return c.userUpdateAttribute(name, "password", password) return c.userUpdateAttribute(name, "password", password)
} }
//UserUpdateQuota update the user's quota (bytes)
func (c *Client) UserUpdateQuota(name string, quota int) error { func (c *Client) UserUpdateQuota(name string, quota int) error {
return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota)) return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota))
} }
//UserGroupList lists the user's groups
func (c *Client) UserGroupList(name string) ([]string, error) { func (c *Client) UserGroupList(name string) ([]string, error) {
res, err := c.baseRequest(routes.users, name, "groups", nil, http.MethodGet) res, err := c.baseRequest(routes.users, name, "groups", nil, http.MethodGet)
if err != nil { if err != nil {
@ -159,6 +177,7 @@ 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
func (c *Client) UserGroupAdd(name string, group string) error { func (c *Client) UserGroupAdd(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
@ -168,6 +187,7 @@ func (c *Client) UserGroupAdd(name string, group string) error {
return c.userBaseRequest(name, "groups", ro, http.MethodPost) return c.userBaseRequest(name, "groups", ro, http.MethodPost)
} }
//UserGroupRemove removes the user from the group
func (c *Client) UserGroupRemove(name string, group string) error { func (c *Client) UserGroupRemove(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
@ -177,6 +197,7 @@ func (c *Client) UserGroupRemove(name string, group string) error {
return c.userBaseRequest(name, "groups", ro, http.MethodDelete) return c.userBaseRequest(name, "groups", ro, http.MethodDelete)
} }
//UserGroupPromote promotes the user as group admin
func (c *Client) UserGroupPromote(name string, group string) error { func (c *Client) UserGroupPromote(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
@ -186,6 +207,7 @@ func (c *Client) UserGroupPromote(name string, group string) error {
return c.userBaseRequest(name, "subadmins", ro, http.MethodPost) return c.userBaseRequest(name, "subadmins", ro, http.MethodPost)
} }
//UserGroupDemote demotes the user
func (c *Client) UserGroupDemote(name string, group string) error { func (c *Client) UserGroupDemote(name string, group string) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
@ -195,6 +217,7 @@ func (c *Client) UserGroupDemote(name string, group string) error {
return c.userBaseRequest(name, "subadmins", ro, http.MethodDelete) return c.userBaseRequest(name, "subadmins", ro, http.MethodDelete)
} }
//UserGroupSubAdminList lists the groups where he is subadmin
func (c *Client) UserGroupSubAdminList(name string) ([]string, error) { func (c *Client) UserGroupSubAdminList(name string) ([]string, error) {
if !c.loggedIn() { if !c.loggedIn() {
return nil, unauthorized return nil, unauthorized