diff --git a/README.md b/README.md index 104cef9..f852154 100644 --- a/README.md +++ b/README.md @@ -62,155 +62,168 @@ $ go test -v . #### type Client ```go -type Client struct { - Apps *Apps - AppsConfig *AppsConfig - GroupFolders *GroupFolders - Notifications *Notifications - Shares *Shares - Users *Users - Groups *Groups +type Client interface { + Apps() Apps + AppsConfig() AppsConfig + GroupFolders() GroupFolders + Notifications() Notifications + Shares() Shares + Users() Users + Groups() Groups + WebDav() WebDav + Monitoring() (*Monitoring, error) + Login(username string, password string) error + Logout() error } ``` -Client is the API client that performs all operations against a Nextcloud -server. +Client is the main client interface #### func NewClient ```go -func NewClient(hostname string) (*Client, error) +func NewClient(hostname string) (Client, error) ``` -NewClient create a new Client from the Nextcloud Instance URL +NewClient create a new client -#### func (*Client) Login +#### ShareType and SharePermission ```go -func (c *Client) Login(username string, password string) error -``` -Login perform login and create a session with the Nextcloud API. +const ( + UserShare ShareType = 0 + GroupShare ShareType = 1 + PublicLinkShare ShareType = 3 + FederatedCloudShare ShareType = 6 -#### func (*Client) Logout + ReadPermission SharePermission = 1 + UpdatePermission SharePermission = 2 + CreatePermission SharePermission = 4 + DeletePermission SharePermission = 8 + ReSharePermission SharePermission = 16 + AllPermissions SharePermission = 31 +) +``` + +#### type APIError ```go -func (c *Client) Logout() error +type APIError struct { + Code int + Message string +} ``` -Logout logs out from the Nextcloud API, close the session and delete session's -cookie + +APIError contains the returned error code and message from the Nextcloud's API + +#### func (*APIError) Error + +```go +func (e *APIError) Error() string +``` +Error return the types.APIError string + +#### type ActiveUsers + +```go +type ActiveUsers struct { + Last5Minutes int `json:"last5minutes"` + Last1Hour int `json:"last1hour"` + Last24Hours int `json:"last24hours"` +} +``` + + +#### type App + +```go +type App struct { + ID string `json:"id"` + Ocsid string `json:"ocsid"` + Name string `json:"name"` + Summary string `json:"summary"` + Description string `json:"description"` + Licence string `json:"licence"` + Author string `json:"author"` + Version string `json:"version"` + Namespace string `json:"namespace"` + Types []string `json:"types"` + Documentation struct { + Admin string `json:"admin"` + Developer string `json:"developer"` + User string `json:"user"` + } `json:"documentation"` + Category []string `json:"category"` + Website string `json:"website"` + Bugs string `json:"bugs"` + Repository struct { + Attributes struct { + Type string `json:"type"` + } `json:"@attributes"` + Value string `json:"@value"` + } `json:"repository"` + Screenshot []interface{} `json:"screenshot"` + Dependencies struct { + Owncloud struct { + Attributes struct { + MinVersion string `json:"min-version"` + MaxVersion string `json:"max-version"` + } `json:"@attributes"` + } `json:"owncloud"` + Nextcloud struct { + Attributes struct { + MinVersion string `json:"min-version"` + MaxVersion string `json:"max-version"` + } `json:"@attributes"` + } `json:"nextcloud"` + } `json:"dependencies"` + Settings struct { + Admin []string `json:"admin"` + AdminSection []string `json:"admin-section"` + Personal []interface{} `json:"personal"` + PersonalSection []interface{} `json:"personal-section"` + } `json:"settings"` + Info []interface{} `json:"info"` + Remote []interface{} `json:"remote"` + Public []interface{} `json:"public"` + RepairSteps struct { + Install []interface{} `json:"install"` + PreMigration []interface{} `json:"pre-migration"` + PostMigration []interface{} `json:"post-migration"` + LiveMigration []interface{} `json:"live-migration"` + Uninstall []interface{} `json:"uninstall"` + } `json:"repair-steps"` + BackgroundJobs []interface{} `json:"background-jobs"` + TwoFactorProviders []interface{} `json:"two-factor-providers"` + Commands []interface{} `json:"commands"` + Activity struct { + Filters []interface{} `json:"filters"` + Settings []interface{} `json:"settings"` + Providers []interface{} `json:"providers"` + } `json:"activity"` +} +``` + +App #### type Apps ```go -type Apps struct { +type Apps interface { + List() ([]string, error) + ListEnabled() ([]string, error) + ListDisabled() ([]string, error) + Infos(name string) (App, error) + Enable(name string) error + Disable(name string) error } ``` -Apps contains all Apps available actions - -#### func (*Apps) Disable - -```go -func (a *Apps) Disable(name string) error -``` -Disable disables an app - -#### func (*Apps) Enable - -```go -func (a *Apps) Enable(name string) error -``` -Enable enables an app - -#### func (*Apps) Infos - -```go -func (a *Apps) Infos(name string) (types.App, error) -``` -Infos return the app's details - -#### func (*Apps) List - -```go -func (a *Apps) List() ([]string, error) -``` -List return the list of the Nextcloud Apps - -#### func (*Apps) ListDisabled - -```go -func (a *Apps) ListDisabled() ([]string, error) -``` -ListDisabled lists the disabled apps - -#### func (*Apps) ListEnabled - -```go -func (a *Apps) ListEnabled() ([]string, error) -``` -ListEnabled lists the enabled apps +Apps available methods #### type AppsConfig ```go -type AppsConfig struct { -} -``` - -AppsConfig contains all Apps Configuration available actions - -#### func (*AppsConfig) DeleteValue - -```go -func (a *AppsConfig) DeleteValue(id, key, value string) error -``` -DeleteValue delete the config value and (!! be careful !!) the key - -#### func (*AppsConfig) Details - -```go -func (a *AppsConfig) Details(appID string) (map[string]string, error) -``` -Details returns all the config's key, values pair of the app - -#### func (*AppsConfig) Get - -```go -func (a *AppsConfig) Get() (map[string]map[string]string, error) -``` -Get returns all apps AppConfigDetails - -#### func (*AppsConfig) Keys - -```go -func (a *AppsConfig) Keys(id string) (keys []string, err error) -``` -Keys returns the app's config keys - -#### func (*AppsConfig) List - -```go -func (a *AppsConfig) List() (apps []string, err error) -``` -List lists all the available apps - -#### func (*AppsConfig) SetValue - -```go -func (a *AppsConfig) SetValue(id, key, value string) error -``` -SetValue set the config value for the given app's key - -#### func (*AppsConfig) Value - -```go -func (a *AppsConfig) Value(id, key string) (string, error) -``` -Value get the config value for the given app's key - -#### type AppsConfigI - -```go -type AppsConfigI interface { +type AppsConfig interface { List() (apps []string, err error) Keys(id string) (keys []string, err error) Value(id, key string) (string, error) @@ -221,175 +234,188 @@ type AppsConfigI interface { } ``` -AppsConfigI available methods +AppsConfig available methods -#### type AppsI +#### type Auth ```go -type AppsI interface { - List() ([]string, error) - ListEnabled() ([]string, error) - ListDisabled() ([]string, error) - Infos(name string) (types.App, error) - Enable(name string) error - Disable(name string) error +type Auth interface { + Login(username string, password string) error + Logout() error } ``` -AppsI available methods -#### func (*Client) Monitoring + +#### type Capabilities ```go -func (c *Client) Monitoring() (*types.Monitoring, error) +type Capabilities struct { + Core struct { + Pollinterval int `json:"pollinterval"` + WebdavRoot string `json:"webdav-root"` + } `json:"core"` + Bruteforce struct { + Delay int `json:"delay"` + } `json:"bruteforce"` + Activity struct { + Apiv2 []string `json:"apiv2"` + } `json:"activity"` + Ocm struct { + Enabled bool `json:"enabled"` + APIVersion string `json:"apiVersion"` + EndPoint string `json:"endPoint"` + ShareTypes []struct { + Name string `json:"name"` + Protocols struct { + Webdav string `json:"webdav"` + } `json:"protocols"` + } `json:"shareTypes"` + } `json:"ocm"` + Dav struct { + Chunking string `json:"chunking"` + } `json:"dav"` + FilesSharing struct { + APIEnabled bool `json:"api_enabled"` + Public struct { + Enabled bool `json:"enabled"` + Password struct { + Enforced bool `json:"enforced"` + } `json:"password"` + ExpireDate struct { + Enabled bool `json:"enabled"` + } `json:"expire_date"` + SendMail bool `json:"send_mail"` + Upload bool `json:"upload"` + UploadFilesDrop bool `json:"upload_files_drop"` + } `json:"public"` + Resharing bool `json:"resharing"` + User struct { + SendMail bool `json:"send_mail"` + ExpireDate struct { + Enabled bool `json:"enabled"` + } `json:"expire_date"` + } `json:"user"` + GroupSharing bool `json:"group_sharing"` + Group struct { + Enabled bool `json:"enabled"` + ExpireDate struct { + Enabled bool `json:"enabled"` + } `json:"expire_date"` + } `json:"group"` + DefaultPermissions int `json:"default_permissions"` + Federation struct { + Outgoing bool `json:"outgoing"` + Incoming bool `json:"incoming"` + ExpireDate struct { + Enabled bool `json:"enabled"` + } `json:"expire_date"` + } `json:"federation"` + Sharebymail struct { + Enabled bool `json:"enabled"` + UploadFilesDrop struct { + Enabled bool `json:"enabled"` + } `json:"upload_files_drop"` + Password struct { + Enabled bool `json:"enabled"` + } `json:"password"` + ExpireDate struct { + Enabled bool `json:"enabled"` + } `json:"expire_date"` + } `json:"sharebymail"` + } `json:"files_sharing"` + Notifications struct { + OcsEndpoints []string `json:"ocs-endpoints"` + Push []string `json:"push"` + AdminNotifications []string `json:"admin-notifications"` + } `json:"notifications"` + PasswordPolicy struct { + MinLength int `json:"minLength"` + EnforceNonCommonPassword bool `json:"enforceNonCommonPassword"` + EnforceNumericCharacters bool `json:"enforceNumericCharacters"` + EnforceSpecialCharacters bool `json:"enforceSpecialCharacters"` + EnforceUpperLowerCase bool `json:"enforceUpperLowerCase"` + } `json:"password_policy"` + Theming struct { + Name string `json:"name"` + URL string `json:"url"` + Slogan string `json:"slogan"` + Color string `json:"color"` + ColorText string `json:"color-text"` + ColorElement string `json:"color-element"` + Logo string `json:"logo"` + Background string `json:"background"` + BackgroundPlain bool `json:"background-plain"` + BackgroundDefault bool `json:"background-default"` + } `json:"theming"` + Files struct { + Bigfilechunking bool `json:"bigfilechunking"` + BlacklistedFiles []string `json:"blacklisted_files"` + Undelete bool `json:"undelete"` + Versioning bool `json:"versioning"` + } `json:"files"` + Registration struct { + Enabled bool `json:"enabled"` + APIRoot string `json:"apiRoot"` + APILevel string `json:"apiLevel"` + } `json:"registration"` +} ``` -Monitoring return nextcloud monitoring statistics + +Capabilities + + + +#### type Group + +```go +type Group struct { + ID string `json:"id"` + Displayname string `json:"displayname"` + UserCount int `json:"usercount"` + Disabled int `json:"disabled"` + CanAdd bool `json:"canAdd"` + CanRemove bool `json:"canRemove"` +} +``` + +Group + +#### type GroupFolder + +```go +type GroupFolder struct { + ID int `json:"id"` + MountPoint string `json:"mount_point"` + Groups map[string]SharePermission `json:"groups"` + Quota int `json:"quota"` + Size int `json:"size"` +} +``` + #### type GroupFolders ```go -type GroupFolders struct { -} -``` - -GroupFolders contains all Groups Folders available actions - -#### func (*GroupFolders) AddGroup - -```go -func (g *GroupFolders) AddGroup(folderID int, groupName string) error -``` -AddGroup adds group to folder - -#### func (*GroupFolders) Create - -```go -func (g *GroupFolders) Create(name string) (id int, err error) -``` -Create creates a group folder - -#### func (*GroupFolders) Get - -```go -func (g *GroupFolders) Get(id int) (types.GroupFolder, error) -``` -Get returns the group folder details - -#### func (*GroupFolders) List - -```go -func (g *GroupFolders) List() (map[int]types.GroupFolder, error) -``` -List returns the groups folders - -#### func (*GroupFolders) RemoveGroup - -```go -func (g *GroupFolders) RemoveGroup(folderID int, groupName string) error -``` -RemoveGroup remove a group from the group folder - -#### func (*GroupFolders) Rename - -```go -func (g *GroupFolders) Rename(groupID int, name string) error -``` -Rename renames the group folder - -#### func (*GroupFolders) SetGroupPermissions - -```go -func (g *GroupFolders) SetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error -``` -SetGroupPermissions set groups permissions - -#### func (*GroupFolders) SetQuota - -```go -func (g *GroupFolders) SetQuota(folderID int, quota int) error -``` -SetQuota set quota on the group folder. quota in bytes, use -3 for unlimited - -#### type GroupFoldersI - -```go -type GroupFoldersI interface { - List() (map[int]types.GroupFolder, error) - Get(id int) (types.GroupFolder, error) +type GroupFolders interface { + List() (map[int]GroupFolder, error) + Get(id int) (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 + SetGroupPermissions(folderID int, groupName string, permission SharePermission) error SetQuota(folderID int, quota int) error } ``` -GroupFoldersI available methods +GroupFolders available methods #### type Groups ```go -type Groups struct { -} -``` - -Groups contains all Groups available actions - -#### func (*Groups) Create - -```go -func (g *Groups) Create(name string) error -``` -Create creates a group - -#### func (*Groups) Delete - -```go -func (g *Groups) Delete(name string) error -``` -Delete deletes the group - -#### func (*Groups) List - -```go -func (g *Groups) List() ([]string, error) -``` -List lists the Nextcloud groups - -#### func (*Groups) ListDetails - -```go -func (g *Groups) ListDetails() ([]types.Group, error) -``` -ListDetails lists the Nextcloud groups - -#### func (*Groups) Search - -```go -func (g *Groups) Search(search string) ([]string, error) -``` -Search return the list of groups matching the search string - -#### func (*Groups) SubAdminList - -```go -func (g *Groups) SubAdminList(name string) ([]string, error) -``` -SubAdminList lists the group's subadmins - -#### func (*Groups) Users - -```go -func (g *Groups) Users(name string) ([]string, error) -``` -Users list the group's users - -#### type GroupsI - -```go -type GroupsI interface { +type Groups interface { List() ([]string, error) - ListDetails() ([]types.Group, error) + ListDetails(search string) ([]Group, error) Users(name string) ([]string, error) Search(search string) ([]string, error) Create(name string) error @@ -398,72 +424,73 @@ type GroupsI interface { } ``` -GroupsI available methods +Groups available methods + +#### type Monitoring + +```go +type Monitoring struct { + Nextcloud struct { + System System `json:"system"` + Storage Storage `json:"storage"` + Shares struct { + NumShares int `json:"num_shares"` + NumSharesUser int `json:"num_shares_user"` + NumSharesGroups int `json:"num_shares_groups"` + NumSharesLink int `json:"num_shares_link"` + NumSharesLinkNoPassword int `json:"num_shares_link_no_password"` + NumFedSharesSent int `json:"num_fed_shares_sent"` + NumFedSharesReceived int `json:"num_fed_shares_received"` + } `json:"shares"` + } `json:"nextcloud"` + Server struct { + Webserver string `json:"webserver"` + Php struct { + Version string `json:"version"` + MemoryLimit int `json:"memory_limit"` + MaxExecutionTime int `json:"max_execution_time"` + UploadMaxFilesize int `json:"upload_max_filesize"` + } `json:"php"` + Database struct { + Type string `json:"type"` + Version string `json:"version"` + Size int `json:"size"` + } `json:"database"` + } `json:"server"` + ActiveUsers ActiveUsers `json:"activeUsers"` +} +``` + + +#### type Notification + +```go +type Notification struct { + NotificationID int `json:"notification_id"` + App string `json:"app"` + User string `json:"user"` + Datetime time.Time `json:"datetime"` + ObjectType string `json:"object_type"` + ObjectID string `json:"object_id"` + Subject string `json:"subject"` + Message string `json:"message"` + Link string `json:"link"` + SubjectRich string `json:"subjectRich"` + SubjectRichParameters []interface{} `json:"subjectRichParameters"` + MessageRich string `json:"messageRich"` + MessageRichParameters []interface{} `json:"messageRichParameters"` + Icon string `json:"icon"` + Actions []interface{} `json:"actions"` +} +``` + #### type Notifications ```go -type Notifications struct { -} -``` - -Notifications contains all Notifications available actions - -#### func (*Notifications) AdminAvailable - -```go -func (n *Notifications) AdminAvailable() error -``` -AdminAvailable returns an error if the admin-notifications app is not installed - -#### func (*Notifications) Available - -```go -func (n *Notifications) Available() error -``` -Available returns an error if the notifications app is not installed - -#### func (*Notifications) Create - -```go -func (n *Notifications) Create(userID, title, message string) error -``` -Create creates a notification (if the user is an admin) - -#### func (*Notifications) Delete - -```go -func (n *Notifications) Delete(id int) error -``` -Delete deletes the notification corresponding to the id - -#### func (*Notifications) DeleteAll - -```go -func (n *Notifications) DeleteAll() error -``` -DeleteAll deletes all notifications - -#### func (*Notifications) Get - -```go -func (n *Notifications) Get(id int) (types.Notification, error) -``` -Get returns the notification corresponding to the id - -#### func (*Notifications) List - -```go -func (n *Notifications) List() ([]types.Notification, error) -``` -List returns all the notifications - -#### type NotificationsI - -```go -type NotificationsI interface { - List() ([]types.Notification, error) - Get(id int) (types.Notification, error) +type Notifications interface { + List() ([]Notification, error) + Get(id int) (Notification, error) Delete(id int) error DeleteAll() error Create(userID, title, message string) error @@ -472,332 +499,128 @@ type NotificationsI interface { } ``` -NotificationsI available methods +Notifications available methods -#### type Routes +#### type Quota ```go -type Routes struct { +type Quota struct { + Free int64 `json:"free"` + Used int64 `json:"used"` + Total int64 `json:"total"` + Relative float64 `json:"relative"` + Quota int64 `json:"quota"` +} +``` + + +#### func (*Quota) String + +```go +func (q *Quota) String() string +``` + +#### type Share + +```go +type Share struct { + ID string `json:"id"` + ShareType int `json:"share_type"` + UIDOwner string `json:"uid_owner"` + DisplaynameOwner string `json:"displayname_owner"` + Permissions int `json:"permissions"` + Stime int `json:"stime"` + Parent interface{} `json:"parent"` + Expiration string `json:"expiration"` + Token string `json:"token"` + UIDFileOwner string `json:"uid_file_owner"` + DisplaynameFileOwner string `json:"displayname_file_owner"` + Path string `json:"path"` + ItemType string `json:"item_type"` + Mimetype string `json:"mimetype"` + StorageID string `json:"storage_id"` + Storage int `json:"storage"` + ItemSource int `json:"item_source"` + FileSource int `json:"file_source"` + FileParent int `json:"file_parent"` + FileTarget string `json:"file_target"` + ShareWith string `json:"share_with"` + ShareWithDisplayname string `json:"share_with_displayname"` + MailSend int `json:"mail_send"` + Tags []string `json:"tags"` +} +``` + + +#### type SharePermission + +```go +type SharePermission int +``` + + +#### type ShareType + +```go +type ShareType int +``` + + +#### type ShareUpdate + +```go +type ShareUpdate struct { + ShareID int + Permissions SharePermission + Password string + PublicUpload bool + ExpireDate string } ``` -Routes references the available routes #### type Shares ```go -type Shares struct { -} -``` - -Shares contains all Shares available actions - -#### func (*Shares) Create - -```go -func (s *Shares) Create( - path string, - shareType types.ShareType, - permission types.SharePermission, - shareWith string, - publicUpload bool, - password string, -) (types.Share, error) -``` -Create create a share - -#### func (*Shares) Delete - -```go -func (s *Shares) Delete(shareID int) error -``` -Delete Remove the given share. - -#### func (*Shares) Get - -```go -func (s *Shares) Get(shareID string) (types.Share, error) -``` -Get information about a known Share - -#### func (*Shares) GetFromPath - -```go -func (s *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error) -``` -GetFromPath return shares from a specific file or folder - -#### func (*Shares) List - -```go -func (s *Shares) List() ([]types.Share, error) -``` -List list all shares of the logged in user - -#### func (*Shares) Update - -```go -func (s *Shares) Update(shareUpdate types.ShareUpdate) error -``` -Update update share details expireDate expireDate expects a well formatted date -string, e.g. ‘YYYY-MM-DD’ - -#### func (*Shares) UpdateExpireDate - -```go -func (s *Shares) UpdateExpireDate(shareID int, expireDate string) error -``` -UpdateExpireDate updates the share's expire date expireDate expects a well -formatted date string, e.g. ‘YYYY-MM-DD’ - -#### func (*Shares) UpdatePassword - -```go -func (s *Shares) UpdatePassword(shareID int, password string) error -``` -UpdatePassword updates share password - -#### func (*Shares) UpdatePermissions - -```go -func (s *Shares) UpdatePermissions(shareID int, permissions types.SharePermission) error -``` -UpdatePermissions update permissions - -#### func (*Shares) UpdatePublicUpload - -```go -func (s *Shares) UpdatePublicUpload(shareID int, public bool) error -``` -UpdatePublicUpload enable or disable public upload - -#### type SharesI - -```go -type SharesI interface { - List() ([]types.Share, error) - GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error) - Get(shareID string) (types.Share, error) +type Shares interface { + List() ([]Share, error) + GetFromPath(path string, reshares bool, subfiles bool) ([]Share, error) + Get(shareID string) (Share, error) Create( path string, - shareType types.ShareType, - permission types.SharePermission, + shareType ShareType, + permission SharePermission, shareWith string, publicUpload bool, password string, - ) (types.Share, error) + ) (Share, error) Delete(shareID int) error - Update(shareUpdate types.ShareUpdate) error + Update(shareUpdate 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 + UpdatePermissions(shareID int, permissions SharePermission) error } ``` -SharesI available methods +``` #### type Users ```go -type Users struct { -} -``` - -Users contains all Users available actions - -#### func (*Users) Create - -```go -func (u *Users) Create(username string, password string, user *types.User) error -``` -Create create a new user - -#### func (*Users) CreateWithoutPassword - -```go -func (u *Users) CreateWithoutPassword(username, email, displayName string) error -``` -CreateWithoutPassword create a user without provisioning a password, the email -address must be provided to send an init password email - -#### func (*Users) Delete - -```go -func (u *Users) Delete(name string) error -``` -Delete delete the user - -#### func (*Users) Disable - -```go -func (u *Users) Disable(name string) error -``` -Disable disables the user - -#### func (*Users) Enable - -```go -func (u *Users) Enable(name string) error -``` -Enable enables the user - -#### func (*Users) Get - -```go -func (u *Users) Get(name string) (*types.User, error) -``` -Get return the details about the specified user - -#### func (*Users) GroupAdd - -```go -func (u *Users) GroupAdd(name string, group string) error -``` -GroupAdd adds a the user to the group - -#### func (*Users) GroupDemote - -```go -func (u *Users) GroupDemote(name string, group string) error -``` -GroupDemote demotes the user - -#### func (*Users) GroupList - -```go -func (u *Users) GroupList(name string) ([]string, error) -``` -GroupList lists the user's groups - -#### func (*Users) GroupPromote - -```go -func (u *Users) GroupPromote(name string, group string) error -``` -GroupPromote promotes the user as group admin - -#### func (*Users) GroupRemove - -```go -func (u *Users) GroupRemove(name string, group string) error -``` -GroupRemove removes the user from the group - -#### func (*Users) GroupSubAdminList - -```go -func (u *Users) GroupSubAdminList(name string) ([]string, error) -``` -GroupSubAdminList lists the groups where he is subadmin - -#### func (*Users) List - -```go -func (u *Users) List() ([]string, error) -``` -List return the Nextcloud'user list - -#### func (*Users) ListDetails - -```go -func (u *Users) ListDetails() (map[string]types.User, error) -``` -ListDetails return a map of user with details - -#### func (*Users) Search - -```go -func (u *Users) Search(search string) ([]string, error) -``` -Search returns the users whose name match the search string - -#### func (*Users) SendWelcomeEmail - -```go -func (u *Users) SendWelcomeEmail(name string) error -``` -SendWelcomeEmail (re)send the welcome mail to the user (return an error if the -user has not configured his email) - -#### func (*Users) Update - -```go -func (u *Users) Update(user *types.User) error -``` -Update takes a *types.Users struct to update the user's information - -#### func (*Users) UpdateAddress - -```go -func (u *Users) UpdateAddress(name string, address string) error -``` -UpdateAddress update the user's address - -#### func (*Users) UpdateDisplayName - -```go -func (u *Users) UpdateDisplayName(name string, displayName string) error -``` -UpdateDisplayName update the user's display name - -#### func (*Users) UpdateEmail - -```go -func (u *Users) UpdateEmail(name string, email string) error -``` -UpdateEmail update the user's email - -#### func (*Users) UpdatePassword - -```go -func (u *Users) UpdatePassword(name string, password string) error -``` -UpdatePassword update the user's password - -#### func (*Users) UpdatePhone - -```go -func (u *Users) UpdatePhone(name string, phone string) error -``` -UpdatePhone update the user's phone - -#### func (*Users) UpdateQuota - -```go -func (u *Users) UpdateQuota(name string, quota int) error -``` -UpdateQuota update the user's quota (bytes) - -#### func (*Users) UpdateTwitter - -```go -func (u *Users) UpdateTwitter(name string, twitter string) error -``` -UpdateTwitter update the user's twitter - -#### func (*Users) UpdateWebSite - -```go -func (u *Users) UpdateWebSite(name string, website string) error -``` -UpdateWebSite update the user's website - -#### type UsersI - -```go -type UsersI interface { +type Users interface { List() ([]string, error) - ListDetails() (map[string]types.User, error) - Get(name string) (*types.User, error) + ListDetails() (map[string]UserDetails, error) + Get(name string) (*UserDetails, error) Search(search string) ([]string, error) - Create(username string, password string, user *types.User) error - CreateWithoutPassword(username, email, displayName string) error + Create(username string, password string, user *UserDetails) error + CreateWithoutPassword(username, email, displayName, quota, language string, groups ...string) error + CreateBatchWithoutPassword(users []User) error Delete(name string) error Enable(name string) error Disable(name string) error SendWelcomeEmail(name string) error - Update(user *types.User) error + Update(user *UserDetails) error UpdateEmail(name string, email string) error UpdateDisplayName(name string, displayName string) error UpdatePhone(name string, phone string) error @@ -805,7 +628,7 @@ type UsersI interface { UpdateWebSite(name string, website string) error UpdateTwitter(name string, twitter string) error UpdatePassword(name string, password string) error - UpdateQuota(name string, quota int) error + UpdateQuota(name string, quota int64) error GroupList(name string) ([]string, error) GroupAdd(name string, group string) error GroupRemove(name string, group string) error @@ -815,5 +638,160 @@ type UsersI interface { } ``` -UsersI available methods +Users available methods +#### type Version + +```go +type Version struct { + Major int `json:"major"` + Minor int `json:"minor"` + Micro int `json:"micro"` + String string `json:"string"` + Edition string `json:"edition"` +} +``` + + +#### type WebDav + +```go +type WebDav interface { + // ReadDir reads the contents of a remote directory + ReadDir(path string) ([]os.FileInfo, error) + // Stat returns the file stats for a specified path + Stat(path string) (os.FileInfo, error) + // Remove removes a remote file + Remove(path string) error + // RemoveAll removes remote files + RemoveAll(path string) error + // Mkdir makes a directory + Mkdir(path string, _ os.FileMode) error + // MkdirAll like mkdir -p, but for webdav + MkdirAll(path string, _ os.FileMode) error + // Rename moves a file from A to B + Rename(oldpath, newpath string, overwrite bool) error + // Copy copies a file from A to B + Copy(oldpath, newpath string, overwrite bool) error + // Read reads the contents of a remote file + Read(path string) ([]byte, error) + // ReadStream reads the stream for a given path + ReadStream(path string) (io.ReadCloser, error) + // Write writes data to a given path + Write(path string, data []byte, _ os.FileMode) error + // WriteStream writes a stream + WriteStream(path string, stream io.Reader, _ os.FileMode) error + + // Walk walks the file tree rooted at root, calling walkFn for each file or + // directory in the tree, including root. + Walk(path string, walkFunc filepath.WalkFunc) error +} +``` + +WebDav available methods + + +#### type Storage + +```go +type Storage struct { + NumUsers int `json:"num_users"` + NumFiles int `json:"num_files"` + NumStorages int `json:"num_storages"` + NumStoragesLocal int `json:"num_storages_local"` + NumStoragesHome int `json:"num_storages_home"` + NumStoragesOther int `json:"num_storages_other"` +} +``` + + +#### type System + +```go +type System struct { + Version string `json:"version"` + Theme string `json:"theme"` + EnableAvatars string `json:"enable_avatars"` + EnablePreviews string `json:"enable_previews"` + MemcacheLocal string `json:"memcache.local"` + MemcacheDistributed string `json:"memcache.distributed"` + FilelockingEnabled string `json:"filelocking.enabled"` + MemcacheLocking string `json:"memcache.locking"` + Debug string `json:"debug"` + Freespace int64 `json:"freespace"` + Cpuload []float32 `json:"cpuload"` + MemTotal int `json:"mem_total"` + MemFree int `json:"mem_free"` + SwapTotal int `json:"swap_total"` + SwapFree int `json:"swap_free"` +} +``` + + +#### type UpdateError + +```go +type UpdateError struct { + Field string + Error error +} +``` + +UpdateError contains the user's field and corresponding error + +#### type User + +```go +type User struct { + Username string + Email string + DisplayName string + Quota string + Language string + Groups []string +} +``` + +User encapsulate the data needed to create a new Nextcloud's User + +#### type UserDetails + +```go +type UserDetails struct { + Enabled bool `json:"enabled"` + ID string `json:"id"` + Quota Quota `json:"quota"` + Email string `json:"email"` + Displayname string `json:"displayname"` + Phone string `json:"phone"` + Address string `json:"address"` + Website string `json:"website"` + Twitter string `json:"twitter"` + Groups []string `json:"groups"` + Language string `json:"language,omitempty"` + + StorageLocation string `json:"storageLocation,omitempty"` + LastLogin int64 `json:"lastLogin,omitempty"` + Backend string `json:"backend,omitempty"` + Subadmin []interface{} `json:"subadmin,omitempty"` + Locale string `json:"locale,omitempty"` +} +``` + +UserDetails is the raw Nextcloud User response + +#### type UserUpdateError + +```go +type UserUpdateError struct { + Errors map[string]error +} +``` + +UpdateError contains the errors resulting from a UserUpdate or a UserCreateFull +call + +#### func (*UserUpdateError) Error + +```go +func (e *UserUpdateError) Error() string diff --git a/types/app.go b/app.go similarity index 99% rename from types/app.go rename to app.go index 514288f..880d8bd 100644 --- a/types/app.go +++ b/app.go @@ -1,4 +1,4 @@ -package types +package gonextcloud //App type App struct { diff --git a/apps.go b/apps_impl.go similarity index 65% rename from apps.go rename to apps_impl.go index 0fd7fca..a2b9e87 100644 --- a/apps.go +++ b/apps_impl.go @@ -1,29 +1,29 @@ package gonextcloud import ( - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "net/http" + + req "github.com/levigross/grequests" ) -//Apps contains all Apps available actions -type Apps struct { - c *Client +//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) { +//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 { return nil, err } - var r types.AppListResponse + var r appListResponse res.JSON(&r) return r.Ocs.Data.Apps, nil } //ListEnabled lists the enabled apps -func (a *Apps) ListEnabled() ([]string, error) { +func (a *apps) ListEnabled() ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"filter": "enabled"}, } @@ -31,13 +31,13 @@ func (a *Apps) ListEnabled() ([]string, error) { if err != nil { return nil, err } - var r types.AppListResponse + var r appListResponse res.JSON(&r) return r.Ocs.Data.Apps, nil } //ListDisabled lists the disabled apps -func (a *Apps) ListDisabled() ([]string, error) { +func (a *apps) ListDisabled() ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"filter": "disabled"}, } @@ -45,30 +45,30 @@ func (a *Apps) ListDisabled() ([]string, error) { if err != nil { return nil, err } - var r types.AppListResponse + var r appListResponse res.JSON(&r) return r.Ocs.Data.Apps, nil } //Infos return the app's details -func (a *Apps) Infos(name string) (types.App, error) { +func (a *apps) Infos(name string) (App, error) { res, err := a.c.baseRequest(http.MethodGet, routes.apps, nil, name) if err != nil { - return types.App{}, err + return App{}, err } - var r types.AppResponse + var r appResponse res.JSON(&r) return r.Ocs.Data, nil } //Enable enables an app -func (a *Apps) Enable(name string) error { +func (a *apps) Enable(name string) error { _, err := a.c.baseRequest(http.MethodPost, routes.apps, nil, name) return err } //Disable disables an app -func (a *Apps) Disable(name string) error { +func (a *apps) Disable(name string) error { _, err := a.c.baseRequest(http.MethodDelete, routes.apps, nil, name) return err } diff --git a/apps_test.go b/apps_impl_test.go similarity index 100% rename from apps_test.go rename to apps_impl_test.go diff --git a/appsconfig.go b/appsconfig_impl.go similarity index 73% rename from appsconfig.go rename to appsconfig_impl.go index e4d8f7e..0adb385 100644 --- a/appsconfig.go +++ b/appsconfig_impl.go @@ -1,52 +1,52 @@ package gonextcloud import ( - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "net/http" "sync" + + req "github.com/levigross/grequests" ) -//AppsConfig contains all Apps Configuration available actions -type AppsConfig struct { - c *Client +//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) { +func (a *appsConfig) List() (apps []string, err error) { res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil) if err != nil { return nil, err } - var r types.AppConfigResponse + var r appConfigResponse res.JSON(&r) return r.Ocs.Data.Data, nil } //Keys returns the app's config keys -func (a *AppsConfig) Keys(id string) (keys []string, err error) { +func (a *appsConfig) Keys(id string) (keys []string, err error) { res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil, id) if err != nil { return nil, err } - var r types.AppConfigResponse + var r appConfigResponse res.JSON(&r) return r.Ocs.Data.Data, nil } //Value get the config value for the given app's key -func (a *AppsConfig) Value(id, key string) (string, error) { +func (a *appsConfig) Value(id, key string) (string, error) { res, err := a.c.baseRequest(http.MethodGet, routes.appsConfig, nil, id, key) if err != nil { return "", err } - var r types.AppcConfigValueResponse + var r appcConfigValueResponse res.JSON(&r) return r.Ocs.Data.Data, nil } //SetValue set the config value for the given app's key -func (a *AppsConfig) SetValue(id, key, value string) error { +func (a *appsConfig) SetValue(id, key, value string) error { ro := &req.RequestOptions{ Data: map[string]string{ "value": value, @@ -57,13 +57,13 @@ func (a *AppsConfig) SetValue(id, key, value string) error { } //DeleteValue delete the config value and (!! be careful !!) the key -func (a *AppsConfig) DeleteValue(id, key, value string) error { +func (a *appsConfig) DeleteValue(id, key, value string) error { _, err := a.c.baseRequest(http.MethodDelete, routes.appsConfig, nil, id, key) return err } //Get returns all apps AppConfigDetails -func (a *AppsConfig) Get() (map[string]map[string]string, error) { +func (a *appsConfig) Get() (map[string]map[string]string, error) { config := map[string]map[string]string{} m := sync.Mutex{} appsIDs, err := a.List() @@ -88,7 +88,7 @@ func (a *AppsConfig) Get() (map[string]map[string]string, error) { } //Details returns all the config's key, values pair of the app -func (a *AppsConfig) Details(appID string) (map[string]string, error) { +func (a *appsConfig) Details(appID string) (map[string]string, error) { config := map[string]string{} m := sync.Mutex{} var err error diff --git a/appsconfig_test.go b/appsconfig_impl_test.go similarity index 100% rename from appsconfig_test.go rename to appsconfig_impl_test.go diff --git a/auth.go b/auth_impl.go similarity index 78% rename from auth.go rename to auth_impl.go index cb074dc..92dc2b1 100644 --- a/auth.go +++ b/auth_impl.go @@ -4,14 +4,12 @@ import ( "fmt" req "github.com/levigross/grequests" - - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" ) var errUnauthorized = 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.password = password options := req.RequestOptions{ @@ -25,14 +23,14 @@ func (c *Client) Login(username string, password string) error { if err != nil { return err } - var r types.CapabilitiesResponse + var r capabilitiesResponse res.JSON(&r) - // No need to check for Ocs.Meta.StatusCode as capabilities are always returned + // No need to check for Ocs.meta.StatusCode as capabilities are always returned c.capabilities = &r.Ocs.Data.Capabilities c.version = &r.Ocs.Data.Version // Check if authentication failed if !c.loggedIn() { - e := types.APIError{Message: "authentication failed"} + e := APIError{Message: "authentication failed"} return &e } // Create webdav client @@ -41,7 +39,7 @@ func (c *Client) Login(username string, password string) error { } // 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.HTTPClient.Jar = nil // Clear capabilities as it is used to check for valid authentication @@ -49,7 +47,7 @@ func (c *Client) Logout() error { return nil } -func (c *Client) loggedIn() bool { +func (c *client) loggedIn() bool { // When authentication failed, capabilities doesn't contains core information if c.capabilities == nil { return false diff --git a/types/capabilities.go b/capabilities.go similarity index 99% rename from types/capabilities.go rename to capabilities.go index 32dd44e..c416ba4 100644 --- a/types/capabilities.go +++ b/capabilities.go @@ -1,4 +1,4 @@ -package types +package gonextcloud //Capabilities type Capabilities struct { diff --git a/client.go b/client.go deleted file mode 100644 index ec9a70e..0000000 --- a/client.go +++ /dev/null @@ -1,101 +0,0 @@ -package gonextcloud - -import ( - "net/url" - - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/adphi/gowebdav" - - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" -) - -// Client is the API client that performs all operations against a Nextcloud server. -type Client struct { - baseURL *url.URL - username string - password string - session *req.Session - headers map[string]string - capabilities *types.Capabilities - version *types.Version - - apps *Apps - appsConfig *AppsConfig - groupFolders *GroupFolders - notifications *Notifications - shares *Shares - users *Users - groups *Groups - webdav *webDav -} - -// NewClient create a new Client from the Nextcloud Instance URL -func NewClient(hostname string) (*Client, error) { - baseURL, err := url.ParseRequestURI(hostname) - if err != nil { - baseURL, err = url.ParseRequestURI("https://" + hostname) - if err != nil { - return nil, err - } - } - - c := &Client{ - baseURL: baseURL, - headers: map[string]string{ - "OCS-APIREQUEST": "true", - "Accept": "application/json", - }, - } - - 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} - // Create empty webdav client - // It will be replaced after login - c.webdav = &webDav{Client: &gowebdav.Client{}} - return c, nil -} - -// Apps return the Apps client Interface -func (c *Client) Apps() types.Apps { - return c.apps -} - -// AppsConfig return the AppsConfig client Interface -func (c *Client) AppsConfig() types.AppsConfig { - return c.appsConfig -} - -// GroupFolders return the GroupFolders client Interface -func (c *Client) GroupFolders() types.GroupFolders { - return c.groupFolders -} - -// Notifications return the Notifications client Interface -func (c *Client) Notifications() types.Notifications { - return c.notifications -} - -// Shares return the Shares client Interface -func (c *Client) Shares() types.Shares { - return c.shares -} - -// Users return the Users client Interface -func (c *Client) Users() types.Users { - return c.users -} - -// Groups return the Groups client Interface -func (c *Client) Groups() types.Groups { - return c.groups -} - -// WebDav return the WebDav client Interface -func (c *Client) WebDav() types.WebDav { - return c.webdav -} diff --git a/client_impl.go b/client_impl.go new file mode 100644 index 0000000..abf3ffa --- /dev/null +++ b/client_impl.go @@ -0,0 +1,99 @@ +package gonextcloud + +import ( + "net/url" + + req "github.com/levigross/grequests" + "gitlab.bertha.cloud/adphi/gowebdav" +) + +// client is the API client that performs all operations against a Nextcloud server. +type client struct { + baseURL *url.URL + username string + password string + session *req.Session + headers map[string]string + capabilities *Capabilities + version *Version + + apps *apps + appsConfig *appsConfig + groupFolders *groupFolders + notifications *notifications + shares *shares + users *users + groups *groups + webdav *webDav +} + +// newClient create a new client from the Nextcloud Instance URL +func newClient(hostname string) (*client, error) { + baseURL, err := url.ParseRequestURI(hostname) + if err != nil { + baseURL, err = url.ParseRequestURI("https://" + hostname) + if err != nil { + return nil, err + } + } + + c := &client{ + baseURL: baseURL, + headers: map[string]string{ + "OCS-APIREQUEST": "true", + "Accept": "application/json", + }, + } + + c.apps = &apps{c} + c.appsConfig = &appsConfig{c} + c.groupFolders = &groupFolders{c} + c.notifications = ¬ifications{c} + c.shares = &shares{c} + c.users = &users{c} + c.groups = &groups{c} + // Create empty webdav client + // It will be replaced after login + c.webdav = &webDav{Client: &gowebdav.Client{}} + return c, nil +} + +// apps return the apps client Interface +func (c *client) Apps() Apps { + return c.apps +} + +// appsConfig return the appsConfig client Interface +func (c *client) AppsConfig() AppsConfig { + return c.appsConfig +} + +// groupFolders return the groupFolders client Interface +func (c *client) GroupFolders() GroupFolders { + return c.groupFolders +} + +// notifications return the notifications client Interface +func (c *client) Notifications() Notifications { + return c.notifications +} + +// shares return the shares client Interface +func (c *client) Shares() Shares { + return c.shares +} + +// users return the users client Interface +func (c *client) Users() Users { + return c.users +} + +// groups return the groups client Interface +func (c *client) Groups() Groups { + return c.groups +} + +// WebDav return the WebDav client Interface +func (c *client) WebDav() WebDav { + return c.webdav +} diff --git a/doc.go b/doc.go index df2a23c..3722379 100644 --- a/doc.go +++ b/doc.go @@ -1,5 +1,5 @@ /* -Package gonextcloud is a Go client for the Nextcloud Provisioning API. +A simple Go Client for Nextcloud's API. For more information about the Provisioning API, see the documentation: https://docs.nextcloud.com/server/13/admin_manual/configuration_user/user_provisioning_api.html @@ -30,11 +30,11 @@ For example, to list all the Nextcloud's instance users: } defer c.Logout() - users, err := c.Users().List() + users, err := c.users().List() if err != nil { panic(err) } - fmt.Println("Users :", users) + fmt.Println("users :", users) } */ package gonextcloud diff --git a/docs/README.md b/docs/README.md index 5fed4bc..ac1f199 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,8 +1,8 @@ # gonextcloud -- - import "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" + import "." -Package gonextcloud is a Go client for the Nextcloud Provisioning API. +A simple Go Client for Nextcloud's API. For more information about the Provisioning API, see the documentation: https://docs.nextcloud.com/server/13/admin_manual/configuration_user/user_provisioning_api.html @@ -34,1570 +34,12 @@ For example, to list all the Nextcloud's instance users: } defer c.Logout() - users, err := c.Users().List() + users, err := c.users().List() if err != nil { panic(err) } - fmt.Println("Users :", users) + fmt.Println("users :", users) } ## Usage -#### type Apps - - type Apps struct { - } - - -Apps contains all Apps available actions - -#### func (*Apps) Disable - - func (a *Apps) Disable(name string) error - -Disable disables an app - -#### func (*Apps) Enable - - func (a *Apps) Enable(name string) error - -Enable enables an app - -#### func (*Apps) Infos - - func (a *Apps) Infos(name string) (types.App, error) - -Infos return the app's details - -#### func (*Apps) List - - func (a *Apps) List() ([]string, error) - -List return the list of the Nextcloud Apps - -#### func (*Apps) ListDisabled - - func (a *Apps) ListDisabled() ([]string, error) - -ListDisabled lists the disabled apps - -#### func (*Apps) ListEnabled - - func (a *Apps) ListEnabled() ([]string, error) - -ListEnabled lists the enabled apps - -#### type AppsConfig - - type AppsConfig struct { - } - - -AppsConfig contains all Apps Configuration available actions - -#### func (*AppsConfig) DeleteValue - - func (a *AppsConfig) DeleteValue(id, key, value string) error - -DeleteValue delete the config value and (!! be careful !!) the key - -#### func (*AppsConfig) Details - - func (a *AppsConfig) Details(appID string) (map[string]string, error) - -Details returns all the config's key, values pair of the app - -#### func (*AppsConfig) Get - - func (a *AppsConfig) Get() (map[string]map[string]string, error) - -Get returns all apps AppConfigDetails - -#### func (*AppsConfig) Keys - - func (a *AppsConfig) Keys(id string) (keys []string, err error) - -Keys returns the app's config keys - -#### func (*AppsConfig) List - - func (a *AppsConfig) List() (apps []string, err error) - -List lists all the available apps - -#### func (*AppsConfig) SetValue - - func (a *AppsConfig) SetValue(id, key, value string) error - -SetValue set the config value for the given app's key - -#### func (*AppsConfig) Value - - func (a *AppsConfig) Value(id, key string) (string, error) - -Value get the config value for the given app's key - -#### type Client - - type Client struct { - } - - -Client is the API client that performs all operations against a Nextcloud -server. - -#### func NewClient - - func NewClient(hostname string) (*Client, error) - -NewClient create a new Client from the Nextcloud Instance URL - -#### func (*Client) Apps - - func (c *Client) Apps() types.Apps - - -#### func (*Client) AppsConfig - - func (c *Client) AppsConfig() types.AppsConfig - - -#### func (*Client) GroupFolders - - func (c *Client) GroupFolders() types.GroupFolders - - -#### func (*Client) Groups - - func (c *Client) Groups() types.Groups - - -#### func (*Client) Login - - func (c *Client) Login(username string, password string) error - -Login perform login and create a session with the Nextcloud API. - -#### func (*Client) Logout - - func (c *Client) Logout() error - -Logout logs out from the Nextcloud API, close the session and delete session's -cookie - -#### func (*Client) Monitoring - - func (c *Client) Monitoring() (*types.Monitoring, error) - -Monitoring return nextcloud monitoring statistics - -#### func (*Client) Notifications - - func (c *Client) Notifications() types.Notifications - - -#### func (*Client) Shares - - func (c *Client) Shares() types.Shares - - -#### func (*Client) Users - - func (c *Client) Users() types.Users - - -#### type GroupFolders - - type GroupFolders struct { - } - - -GroupFolders contains all Groups Folders available actions - -#### func (*GroupFolders) AddGroup - - func (g *GroupFolders) AddGroup(folderID int, groupName string) error - -AddGroup adds group to folder - -#### func (*GroupFolders) Create - - func (g *GroupFolders) Create(name string) (id int, err error) - -Create creates a group folder - -#### func (*GroupFolders) Get - - func (g *GroupFolders) Get(id int) (types.GroupFolder, error) - -Get returns the group folder details - -#### func (*GroupFolders) List - - func (g *GroupFolders) List() (map[int]types.GroupFolder, error) - -List returns the groups folders - -#### func (*GroupFolders) RemoveGroup - - func (g *GroupFolders) RemoveGroup(folderID int, groupName string) error - -RemoveGroup remove a group from the group folder - -#### func (*GroupFolders) Rename - - func (g *GroupFolders) Rename(groupID int, name string) error - -Rename renames the group folder - -#### func (*GroupFolders) SetGroupPermissions - - func (g *GroupFolders) SetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error - -SetGroupPermissions set groups permissions - -#### func (*GroupFolders) SetQuota - - func (g *GroupFolders) SetQuota(folderID int, quota int) error - -SetQuota set quota on the group folder. quota in bytes, use -3 for unlimited - -#### type Groups - - type Groups struct { - } - - -Groups contains all Groups available actions - -#### func (*Groups) Create - - func (g *Groups) Create(name string) error - -Create creates a group - -#### func (*Groups) Delete - - func (g *Groups) Delete(name string) error - -Delete deletes the group - -#### func (*Groups) List - - func (g *Groups) List() ([]string, error) - -List lists the Nextcloud groups - -#### func (*Groups) ListDetails - - func (g *Groups) ListDetails() ([]types.Group, error) - -ListDetails lists the Nextcloud groups - -#### func (*Groups) Search - - func (g *Groups) Search(search string) ([]string, error) - -Search return the list of groups matching the search string - -#### func (*Groups) SubAdminList - - func (g *Groups) SubAdminList(name string) ([]string, error) - -SubAdminList lists the group's subadmins - -#### func (*Groups) Users - - func (g *Groups) Users(name string) ([]string, error) - -Users list the group's users - -#### type Notifications - - type Notifications struct { - } - - -Notifications contains all Notifications available actions - -#### func (*Notifications) AdminAvailable - - func (n *Notifications) AdminAvailable() error - -AdminAvailable returns an error if the admin-notifications app is not installed - -#### func (*Notifications) Available - - func (n *Notifications) Available() error - -Available returns an error if the notifications app is not installed - -#### func (*Notifications) Create - - func (n *Notifications) Create(userID, title, message string) error - -Create creates a notification (if the user is an admin) - -#### func (*Notifications) Delete - - func (n *Notifications) Delete(id int) error - -Delete deletes the notification corresponding to the id - -#### func (*Notifications) DeleteAll - - func (n *Notifications) DeleteAll() error - -DeleteAll deletes all notifications - -#### func (*Notifications) Get - - func (n *Notifications) Get(id int) (types.Notification, error) - -Get returns the notification corresponding to the id - -#### func (*Notifications) List - - func (n *Notifications) List() ([]types.Notification, error) - -List returns all the notifications - -#### type Routes - - type Routes struct { - } - - -Routes references the available routes - -#### type Shares - - type Shares struct { - } - - -Shares contains all Shares available actions - -#### func (*Shares) Create - - func (s *Shares) Create( - path string, - shareType types.ShareType, - permission types.SharePermission, - shareWith string, - publicUpload bool, - password string, - ) (types.Share, error) - -Create create a share - -#### func (*Shares) Delete - - func (s *Shares) Delete(shareID int) error - -Delete Remove the given share. - -#### func (*Shares) Get - - func (s *Shares) Get(shareID string) (types.Share, error) - -Get information about a known Share - -#### func (*Shares) GetFromPath - - func (s *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error) - -GetFromPath return shares from a specific file or folder - -#### func (*Shares) List - - func (s *Shares) List() ([]types.Share, error) - -List list all shares of the logged in user - -#### func (*Shares) Update - - func (s *Shares) Update(shareUpdate types.ShareUpdate) error - -Update update share details expireDate expireDate expects a well formatted date -string, e.g. ‘YYYY-MM-DD’ - -#### func (*Shares) UpdateExpireDate - - func (s *Shares) UpdateExpireDate(shareID int, expireDate string) error - -UpdateExpireDate updates the share's expire date expireDate expects a well -formatted date string, e.g. ‘YYYY-MM-DD’ - -#### func (*Shares) UpdatePassword - - func (s *Shares) UpdatePassword(shareID int, password string) error - -UpdatePassword updates share password - -#### func (*Shares) UpdatePermissions - - func (s *Shares) UpdatePermissions(shareID int, permissions types.SharePermission) error - -UpdatePermissions update permissions - -#### func (*Shares) UpdatePublicUpload - - func (s *Shares) UpdatePublicUpload(shareID int, public bool) error - -UpdatePublicUpload enable or disable public upload - -#### type Users - - type Users struct { - } - - -Users contains all Users available actions - -#### func (*Users) Create - - func (u *Users) Create(username string, password string, user *types.User) error - -Create create a new user - -#### func (*Users) CreateWithoutPassword - - func (u *Users) CreateWithoutPassword(username, email, displayName string) error - -CreateWithoutPassword create a user without provisioning a password, the email -address must be provided to send an init password email - -#### func (*Users) Delete - - func (u *Users) Delete(name string) error - -Delete delete the user - -#### func (*Users) Disable - - func (u *Users) Disable(name string) error - -Disable disables the user - -#### func (*Users) Enable - - func (u *Users) Enable(name string) error - -Enable enables the user - -#### func (*Users) Get - - func (u *Users) Get(name string) (*types.User, error) - -Get return the details about the specified user - -#### func (*Users) GroupAdd - - func (u *Users) GroupAdd(name string, group string) error - -GroupAdd adds a the user to the group - -#### func (*Users) GroupDemote - - func (u *Users) GroupDemote(name string, group string) error - -GroupDemote demotes the user - -#### func (*Users) GroupList - - func (u *Users) GroupList(name string) ([]string, error) - -GroupList lists the user's groups - -#### func (*Users) GroupPromote - - func (u *Users) GroupPromote(name string, group string) error - -GroupPromote promotes the user as group admin - -#### func (*Users) GroupRemove - - func (u *Users) GroupRemove(name string, group string) error - -GroupRemove removes the user from the group - -#### func (*Users) GroupSubAdminList - - func (u *Users) GroupSubAdminList(name string) ([]string, error) - -GroupSubAdminList lists the groups where he is subadmin - -#### func (*Users) List - - func (u *Users) List() ([]string, error) - -List return the Nextcloud'user list - -#### func (*Users) ListDetails - - func (u *Users) ListDetails() (map[string]types.User, error) - -ListDetails return a map of user with details - -#### func (*Users) Search - - func (u *Users) Search(search string) ([]string, error) - -Search returns the users whose name match the search string - -#### func (*Users) SendWelcomeEmail - - func (u *Users) SendWelcomeEmail(name string) error - -SendWelcomeEmail (re)send the welcome mail to the user (return an error if the -user has not configured his email) - -#### func (*Users) Update - - func (u *Users) Update(user *types.User) error - -Update takes a *types.Users struct to update the user's information - -#### func (*Users) UpdateAddress - - func (u *Users) UpdateAddress(name string, address string) error - -UpdateAddress update the user's address - -#### func (*Users) UpdateDisplayName - - func (u *Users) UpdateDisplayName(name string, displayName string) error - -UpdateDisplayName update the user's display name - -#### func (*Users) UpdateEmail - - func (u *Users) UpdateEmail(name string, email string) error - -UpdateEmail update the user's email - -#### func (*Users) UpdatePassword - - func (u *Users) UpdatePassword(name string, password string) error - -UpdatePassword update the user's password - -#### func (*Users) UpdatePhone - - func (u *Users) UpdatePhone(name string, phone string) error - -UpdatePhone update the user's phone - -#### func (*Users) UpdateQuota - - func (u *Users) UpdateQuota(name string, quota int) error - -UpdateQuota update the user's quota (bytes) - -#### func (*Users) UpdateTwitter - - func (u *Users) UpdateTwitter(name string, twitter string) error - -UpdateTwitter update the user's twitter - -#### func (*Users) UpdateWebSite - - func (u *Users) UpdateWebSite(name string, website string) error - -UpdateWebSite update the user's website - -# types --- - import "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" - - -## Usage - -```go -const ( - UserShare ShareType = 0 - GroupShare ShareType = 1 - PublicLinkShare ShareType = 3 - FederatedCloudShare ShareType = 6 - - ReadPermission SharePermission = 1 - UpdatePermission SharePermission = 2 - CreatePermission SharePermission = 4 - DeletePermission SharePermission = 8 - ReSharePermission SharePermission = 16 - AllPermissions SharePermission = 31 -) -``` - -#### type APIError - -```go -type APIError struct { - Code int - Message string -} -``` - -APIError contains the returned error code and message from the Nextcloud's API - -#### func ErrorFromMeta - -```go -func ErrorFromMeta(meta Meta) *APIError -``` -ErrorFromMeta return a types.APIError from the Response's types.Meta - -#### func (*APIError) Error - -```go -func (e *APIError) Error() string -``` -Error return the types.APIError string - -#### type ActiveUsers - -```go -type ActiveUsers struct { - Last5Minutes int `json:"last5minutes"` - Last1Hour int `json:"last1hour"` - Last24Hours int `json:"last24hours"` -} -``` - - -#### type App - -```go -type App struct { - ID string `json:"id"` - Ocsid string `json:"ocsid"` - Name string `json:"name"` - Summary string `json:"summary"` - Description string `json:"description"` - Licence string `json:"licence"` - Author string `json:"author"` - Version string `json:"version"` - Namespace string `json:"namespace"` - Types []string `json:"types"` - Documentation struct { - Admin string `json:"admin"` - Developer string `json:"developer"` - User string `json:"user"` - } `json:"documentation"` - Category []string `json:"category"` - Website string `json:"website"` - Bugs string `json:"bugs"` - Repository struct { - Attributes struct { - Type string `json:"type"` - } `json:"@attributes"` - Value string `json:"@value"` - } `json:"repository"` - Screenshot []interface{} `json:"screenshot"` - Dependencies struct { - Owncloud struct { - Attributes struct { - MinVersion string `json:"min-version"` - MaxVersion string `json:"max-version"` - } `json:"@attributes"` - } `json:"owncloud"` - Nextcloud struct { - Attributes struct { - MinVersion string `json:"min-version"` - MaxVersion string `json:"max-version"` - } `json:"@attributes"` - } `json:"nextcloud"` - } `json:"dependencies"` - Settings struct { - Admin []string `json:"admin"` - AdminSection []string `json:"admin-section"` - Personal []interface{} `json:"personal"` - PersonalSection []interface{} `json:"personal-section"` - } `json:"settings"` - Info []interface{} `json:"info"` - Remote []interface{} `json:"remote"` - Public []interface{} `json:"public"` - RepairSteps struct { - Install []interface{} `json:"install"` - PreMigration []interface{} `json:"pre-migration"` - PostMigration []interface{} `json:"post-migration"` - LiveMigration []interface{} `json:"live-migration"` - Uninstall []interface{} `json:"uninstall"` - } `json:"repair-steps"` - BackgroundJobs []interface{} `json:"background-jobs"` - TwoFactorProviders []interface{} `json:"two-factor-providers"` - Commands []interface{} `json:"commands"` - Activity struct { - Filters []interface{} `json:"filters"` - Settings []interface{} `json:"settings"` - Providers []interface{} `json:"providers"` - } `json:"activity"` -} -``` - -App - -#### type AppConfigResponse - -```go -type AppConfigResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Data []string `json:"data"` - } `json:"data"` - } `json:"ocs"` -} -``` - - -#### type AppListResponse - -```go -type AppListResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Apps []string `json:"apps"` - } `json:"data"` - } `json:"ocs"` -} -``` - -AppListResponse - -#### type AppResponse - -```go -type AppResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data App `json:"data"` - } `json:"ocs"` -} -``` - -AppResponse - -#### type AppcConfigValueResponse - -```go -type AppcConfigValueResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Data string `json:"data"` - } `json:"data"` - } `json:"ocs"` -} -``` - - -#### type Apps - -```go -type Apps interface { - List() ([]string, error) - ListEnabled() ([]string, error) - ListDisabled() ([]string, error) - Infos(name string) (App, error) - Enable(name string) error - Disable(name string) error -} -``` - -Apps available methods - -#### type AppsConfig - -```go -type AppsConfig interface { - 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 available methods - -#### type Auth - -```go -type Auth interface { - Login(username string, password string) error - Logout() error -} -``` - - -#### type BaseResponse - -```go -type BaseResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data []string `json:"data"` - } `json:"ocs"` -} -``` - -BaseResponse - -#### type Capabilities - -```go -type Capabilities struct { - Core struct { - Pollinterval int `json:"pollinterval"` - WebdavRoot string `json:"webdav-root"` - } `json:"core"` - Bruteforce struct { - Delay int `json:"delay"` - } `json:"bruteforce"` - Activity struct { - Apiv2 []string `json:"apiv2"` - } `json:"activity"` - Ocm struct { - Enabled bool `json:"enabled"` - APIVersion string `json:"apiVersion"` - EndPoint string `json:"endPoint"` - ShareTypes []struct { - Name string `json:"name"` - Protocols struct { - Webdav string `json:"webdav"` - } `json:"protocols"` - } `json:"shareTypes"` - } `json:"ocm"` - Dav struct { - Chunking string `json:"chunking"` - } `json:"dav"` - FilesSharing struct { - APIEnabled bool `json:"api_enabled"` - Public struct { - Enabled bool `json:"enabled"` - Password struct { - Enforced bool `json:"enforced"` - } `json:"password"` - ExpireDate struct { - Enabled bool `json:"enabled"` - } `json:"expire_date"` - SendMail bool `json:"send_mail"` - Upload bool `json:"upload"` - UploadFilesDrop bool `json:"upload_files_drop"` - } `json:"public"` - Resharing bool `json:"resharing"` - User struct { - SendMail bool `json:"send_mail"` - ExpireDate struct { - Enabled bool `json:"enabled"` - } `json:"expire_date"` - } `json:"user"` - GroupSharing bool `json:"group_sharing"` - Group struct { - Enabled bool `json:"enabled"` - ExpireDate struct { - Enabled bool `json:"enabled"` - } `json:"expire_date"` - } `json:"group"` - DefaultPermissions int `json:"default_permissions"` - Federation struct { - Outgoing bool `json:"outgoing"` - Incoming bool `json:"incoming"` - ExpireDate struct { - Enabled bool `json:"enabled"` - } `json:"expire_date"` - } `json:"federation"` - Sharebymail struct { - Enabled bool `json:"enabled"` - UploadFilesDrop struct { - Enabled bool `json:"enabled"` - } `json:"upload_files_drop"` - Password struct { - Enabled bool `json:"enabled"` - } `json:"password"` - ExpireDate struct { - Enabled bool `json:"enabled"` - } `json:"expire_date"` - } `json:"sharebymail"` - } `json:"files_sharing"` - Notifications struct { - OcsEndpoints []string `json:"ocs-endpoints"` - Push []string `json:"push"` - AdminNotifications []string `json:"admin-notifications"` - } `json:"notifications"` - PasswordPolicy struct { - MinLength int `json:"minLength"` - EnforceNonCommonPassword bool `json:"enforceNonCommonPassword"` - EnforceNumericCharacters bool `json:"enforceNumericCharacters"` - EnforceSpecialCharacters bool `json:"enforceSpecialCharacters"` - EnforceUpperLowerCase bool `json:"enforceUpperLowerCase"` - } `json:"password_policy"` - Theming struct { - Name string `json:"name"` - URL string `json:"url"` - Slogan string `json:"slogan"` - Color string `json:"color"` - ColorText string `json:"color-text"` - ColorElement string `json:"color-element"` - Logo string `json:"logo"` - Background string `json:"background"` - BackgroundPlain bool `json:"background-plain"` - BackgroundDefault bool `json:"background-default"` - } `json:"theming"` - Files struct { - Bigfilechunking bool `json:"bigfilechunking"` - BlacklistedFiles []string `json:"blacklisted_files"` - Undelete bool `json:"undelete"` - Versioning bool `json:"versioning"` - } `json:"files"` - Registration struct { - Enabled bool `json:"enabled"` - APIRoot string `json:"apiRoot"` - APILevel string `json:"apiLevel"` - } `json:"registration"` -} -``` - -Capabilities - -#### type CapabilitiesResponse - -```go -type CapabilitiesResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Version Version `json:"version"` - Capabilities Capabilities `json:"capabilities"` - } `json:"data"` - } `json:"ocs"` -} -``` - -CapabilitiesResponse - -#### type Client - -```go -type Client interface { - Apps() Apps - AppsConfig() AppsConfig - GroupFolders() GroupFolders - Notifications() Notifications - Shares() Shares - Users() Users - Groups() Groups -} -``` - -Client is the main client interface - -#### type ErrorResponse - -```go -type ErrorResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data []interface{} `json:"data"` - } `json:"ocs"` -} -``` - -ErrorResponse - -#### type Group - -```go -type Group struct { - ID string `json:"id"` - Displayname string `json:"displayname"` - UserCount int `json:"usercount"` - Disabled int `json:"disabled"` - CanAdd bool `json:"canAdd"` - CanRemove bool `json:"canRemove"` -} -``` - -Group - -#### type GroupFolder - -```go -type GroupFolder struct { - ID int `json:"id"` - MountPoint string `json:"mount_point"` - Groups map[string]SharePermission `json:"groups"` - Quota int `json:"quota"` - Size int `json:"size"` -} -``` - - -#### type GroupFolderBadFormatGroups - -```go -type GroupFolderBadFormatGroups struct { - ID int `json:"id"` - MountPoint string `json:"mount_point"` - Groups map[string]string `json:"groups"` - Quota string `json:"quota"` - Size int `json:"size"` -} -``` - - -#### func (*GroupFolderBadFormatGroups) FormatGroupFolder - -```go -func (gf *GroupFolderBadFormatGroups) FormatGroupFolder() GroupFolder -``` - -#### type GroupFolderBadFormatIDAndGroups - -```go -type GroupFolderBadFormatIDAndGroups struct { - ID string `json:"id"` - MountPoint string `json:"mount_point"` - Groups map[string]string `json:"groups"` - Quota string `json:"quota"` - Size int `json:"size"` -} -``` - - -#### func (*GroupFolderBadFormatIDAndGroups) FormatGroupFolder - -```go -func (gf *GroupFolderBadFormatIDAndGroups) FormatGroupFolder() GroupFolder -``` - -#### type GroupFolders - -```go -type GroupFolders interface { - List() (map[int]GroupFolder, error) - Get(id int) (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 SharePermission) error - SetQuota(folderID int, quota int) error -} -``` - -GroupFolders available methods - -#### type GroupFoldersCreateResponse - -```go -type GroupFoldersCreateResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data GroupFolderBadFormatIDAndGroups `json:"data"` - } `json:"ocs"` -} -``` - - -#### type GroupFoldersListResponse - -```go -type GroupFoldersListResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data map[string]GroupFolderBadFormatIDAndGroups `json:"data"` - } `json:"ocs"` -} -``` - - -#### type GroupFoldersResponse - -```go -type GroupFoldersResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data GroupFolderBadFormatGroups `json:"data"` - } `json:"ocs"` -} -``` - - -#### type GroupListDetailsResponse - -```go -type GroupListDetailsResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Groups []Group `json:"groups"` - } `json:"data"` - } `json:"ocs"` -} -``` - -GroupListDetailsResponse - -#### type GroupListResponse - -```go -type GroupListResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Groups []string `json:"groups"` - } `json:"data"` - } `json:"ocs"` -} -``` - -GroupListResponse - -#### type Groups - -```go -type Groups interface { - List() ([]string, error) - ListDetails() ([]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 available methods - -#### type Meta - -```go -type Meta struct { - Status string `json:"status"` - Statuscode int `json:"statuscode"` - Message string `json:"message"` - Totalitems string `json:"totalitems"` - Itemsperpage string `json:"itemsperpage"` -} -``` - -Meta - -#### type Monitoring - -```go -type Monitoring struct { - Nextcloud struct { - System System `json:"system"` - Storage Storage `json:"storage"` - Shares struct { - NumShares int `json:"num_shares"` - NumSharesUser int `json:"num_shares_user"` - NumSharesGroups int `json:"num_shares_groups"` - NumSharesLink int `json:"num_shares_link"` - NumSharesLinkNoPassword int `json:"num_shares_link_no_password"` - NumFedSharesSent int `json:"num_fed_shares_sent"` - NumFedSharesReceived int `json:"num_fed_shares_received"` - } `json:"shares"` - } `json:"nextcloud"` - Server struct { - Webserver string `json:"webserver"` - Php struct { - Version string `json:"version"` - MemoryLimit int `json:"memory_limit"` - MaxExecutionTime int `json:"max_execution_time"` - UploadMaxFilesize int `json:"upload_max_filesize"` - } `json:"php"` - Database struct { - Type string `json:"type"` - Version string `json:"version"` - Size int `json:"size"` - } `json:"database"` - } `json:"server"` - ActiveUsers ActiveUsers `json:"activeUsers"` -} -``` - - -#### type MonitoringResponse - -```go -type MonitoringResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data Monitoring `json:"data"` - } `json:"ocs"` -} -``` - - -#### type Notification - -```go -type Notification struct { - NotificationID int `json:"notification_id"` - App string `json:"app"` - User string `json:"user"` - Datetime time.Time `json:"datetime"` - ObjectType string `json:"object_type"` - ObjectID string `json:"object_id"` - Subject string `json:"subject"` - Message string `json:"message"` - Link string `json:"link"` - SubjectRich string `json:"subjectRich"` - SubjectRichParameters []interface{} `json:"subjectRichParameters"` - MessageRich string `json:"messageRich"` - MessageRichParameters []interface{} `json:"messageRichParameters"` - Icon string `json:"icon"` - Actions []interface{} `json:"actions"` -} -``` - - -#### type NotificationResponse - -```go -type NotificationResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data Notification `json:"data"` - } `json:"ocs"` -} -``` - - -#### type Notifications - -```go -type Notifications interface { - List() ([]Notification, error) - Get(id int) (Notification, error) - Delete(id int) error - DeleteAll() error - Create(userID, title, message string) error - AdminAvailable() error - Available() error -} -``` - -Notifications available methods - -#### type NotificationsListResponse - -```go -type NotificationsListResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data []Notification `json:"data"` - } `json:"ocs"` -} -``` - - -#### type Share - -```go -type Share struct { - ID string `json:"id"` - ShareType int `json:"share_type"` - UIDOwner string `json:"uid_owner"` - DisplaynameOwner string `json:"displayname_owner"` - Permissions int `json:"permissions"` - Stime int `json:"stime"` - Parent interface{} `json:"parent"` - Expiration string `json:"expiration"` - Token string `json:"token"` - UIDFileOwner string `json:"uid_file_owner"` - DisplaynameFileOwner string `json:"displayname_file_owner"` - Path string `json:"path"` - ItemType string `json:"item_type"` - Mimetype string `json:"mimetype"` - StorageID string `json:"storage_id"` - Storage int `json:"storage"` - ItemSource int `json:"item_source"` - FileSource int `json:"file_source"` - FileParent int `json:"file_parent"` - FileTarget string `json:"file_target"` - ShareWith string `json:"share_with"` - ShareWithDisplayname string `json:"share_with_displayname"` - MailSend int `json:"mail_send"` - Tags []string `json:"tags"` -} -``` - - -#### type SharePermission - -```go -type SharePermission int -``` - - -#### type ShareType - -```go -type ShareType int -``` - - -#### type ShareUpdate - -```go -type ShareUpdate struct { - ShareID int - Permissions SharePermission - Password string - PublicUpload bool - ExpireDate string -} -``` - - -#### type Shares - -```go -type Shares interface { - List() ([]Share, error) - GetFromPath(path string, reshares bool, subfiles bool) ([]Share, error) - Get(shareID string) (Share, error) - Create( - path string, - shareType ShareType, - permission SharePermission, - shareWith string, - publicUpload bool, - password string, - ) (Share, error) - Delete(shareID int) error - Update(shareUpdate ShareUpdate) error - UpdateExpireDate(shareID int, expireDate string) error - UpdatePublicUpload(shareID int, public bool) error - UpdatePassword(shareID int, password string) error - UpdatePermissions(shareID int, permissions SharePermission) error -} -``` - -Shares available methods - -#### type SharesListResponse - -```go -type SharesListResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data []Share `json:"data"` - } `json:"ocs"` -} -``` - - -#### type SharesResponse - -```go -type SharesResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data Share `json:"data"` - } `json:"ocs"` -} -``` - - -#### type Storage - -```go -type Storage struct { - NumUsers int `json:"num_users"` - NumFiles int `json:"num_files"` - NumStorages int `json:"num_storages"` - NumStoragesLocal int `json:"num_storages_local"` - NumStoragesHome int `json:"num_storages_home"` - NumStoragesOther int `json:"num_storages_other"` -} -``` - - -#### type System - -```go -type System struct { - Version string `json:"version"` - Theme string `json:"theme"` - EnableAvatars string `json:"enable_avatars"` - EnablePreviews string `json:"enable_previews"` - MemcacheLocal string `json:"memcache.local"` - MemcacheDistributed string `json:"memcache.distributed"` - FilelockingEnabled string `json:"filelocking.enabled"` - MemcacheLocking string `json:"memcache.locking"` - Debug string `json:"debug"` - Freespace int64 `json:"freespace"` - Cpuload []float32 `json:"cpuload"` - MemTotal int `json:"mem_total"` - MemFree int `json:"mem_free"` - SwapTotal int `json:"swap_total"` - SwapFree int `json:"swap_free"` -} -``` - - -#### type UpdateError - -```go -type UpdateError struct { - Field string - Error error -} -``` - -UpdateError contains the user's field and corresponding error - -#### type User - -```go -type User struct { - Enabled bool `json:"enabled"` - ID string `json:"id"` - Quota struct { - Free int64 `json:"free"` - Used int `json:"used"` - Total int64 `json:"total"` - Relative float64 `json:"relative"` - Quota int `json:"quota"` - } `json:"quota"` - Email string `json:"email"` - Displayname string `json:"displayname"` - Phone string `json:"phone"` - Address string `json:"address"` - Website string `json:"website"` - Twitter string `json:"twitter"` - Groups []string `json:"groups"` - Language string `json:"language,omitempty"` - - StorageLocation string `json:"storageLocation,omitempty"` - LastLogin int64 `json:"lastLogin,omitempty"` - Backend string `json:"backend,omitempty"` - Subadmin []interface{} `json:"subadmin,omitempty"` - Locale string `json:"locale,omitempty"` -} -``` - -Users - -#### type UserListDetailsResponse - -```go -type UserListDetailsResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Users map[string]User `json:"users"` - } `json:"data"` - } `json:"ocs"` -} -``` - - -#### type UserListResponse - -```go -type UserListResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data struct { - Users []string `json:"users"` - } `json:"data"` - } `json:"ocs"` -} -``` - -UserListResponse - -#### type UserResponse - -```go -type UserResponse struct { - Ocs struct { - Meta Meta `json:"meta"` - Data User `json:"data"` - } `json:"ocs"` -} -``` - -UserResponse - -#### type UserUpdateError - -```go -type UserUpdateError struct { - Errors map[string]error -} -``` - -UpdateError contains the errors resulting from a UserUpdate or a UserCreateFull -call - -#### func NewUpdateError - -```go -func NewUpdateError(errors chan UpdateError) *UserUpdateError -``` -NewUpdateError returns an UpdateError based on an UpdateError channel - -#### func (*UserUpdateError) Error - -```go -func (e *UserUpdateError) Error() string -``` - -#### type Users - -```go -type Users interface { - List() ([]string, error) - ListDetails() (map[string]User, error) - Get(name string) (*User, error) - Search(search string) ([]string, error) - Create(username string, password string, user *User) error - CreateWithoutPassword(username, email, displayName, quota, language string, groups ...string) error - Delete(name string) error - Enable(name string) error - Disable(name string) error - SendWelcomeEmail(name string) error - Update(user *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 available methods - -#### type Version - -```go -type Version struct { - Major int `json:"major"` - Minor int `json:"minor"` - Micro int `json:"micro"` - String string `json:"string"` - Edition string `json:"edition"` -} -``` - diff --git a/types/errors.go b/errors.go similarity index 79% rename from types/errors.go rename to errors.go index 5f73d57..c109580 100644 --- a/types/errors.go +++ b/errors.go @@ -1,4 +1,4 @@ -package types +package gonextcloud import ( "fmt" @@ -11,8 +11,8 @@ type APIError struct { Message string } -//ErrorFromMeta return a types.APIError from the Response's types.Meta -func ErrorFromMeta(meta Meta) *APIError { +//errorFromMeta return a types.APIError from the Response's types.meta +func errorFromMeta(meta meta) *APIError { return &APIError{ meta.Statuscode, meta.Message, @@ -43,8 +43,8 @@ func (e *UserUpdateError) Error() string { return strings.Join(errors, ", ") } -//NewUpdateError returns an UpdateError based on an UpdateError channel -func NewUpdateError(errors chan *UpdateError) *UserUpdateError { +//newUpdateError returns an UpdateError based on an UpdateError channel +func newUpdateError(errors chan *UpdateError) *UserUpdateError { ue := UserUpdateError{map[string]error{}} for e := range errors { if e != nil { diff --git a/types/errors_test.go b/errors_test.go similarity index 89% rename from types/errors_test.go rename to errors_test.go index 08bfa0c..56449e2 100644 --- a/types/errors_test.go +++ b/errors_test.go @@ -1,11 +1,12 @@ -package types +package gonextcloud import ( "errors" - "github.com/stretchr/testify/assert" "strconv" "sync" "testing" + + "github.com/stretchr/testify/assert" ) func TestUserUpdateErrors(t *testing.T) { @@ -24,7 +25,7 @@ func TestUserUpdateErrors(t *testing.T) { } close(errs) }() - uerrs := NewUpdateError(errs) + uerrs := newUpdateError(errs) assert.Equal(t, exp, uerrs.Errors) assert.NotEmpty(t, uerrs.Error()) } @@ -41,6 +42,6 @@ func TestUserUpdateErrorsNil(t *testing.T) { wg.Wait() close(errs) }() - uerrs := NewUpdateError(errs) + uerrs := newUpdateError(errs) assert.Nil(t, uerrs) } diff --git a/go.mod b/go.mod index 720b9b2..e4cc512 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/pkg/errors v0.0.0-20181023235946-059132a15dd0 github.com/sirupsen/logrus v1.4.2 github.com/stretchr/testify v1.2.2 - github.com/studio-b12/gowebdav v0.0.0-20190103184047-38f79aeaf1ac gitlab.bertha.cloud/adphi/gowebdav v0.0.0-20190720232020-771eec6e76d0 golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect diff --git a/go.sum b/go.sum index e62c82d..bae990b 100644 --- a/go.sum +++ b/go.sum @@ -23,8 +23,6 @@ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/studio-b12/gowebdav v0.0.0-20190103184047-38f79aeaf1ac h1:xQ9gCVzqb939vjhxuES4IXYe4AlHB4Q71/K06aazQmQ= -github.com/studio-b12/gowebdav v0.0.0-20190103184047-38f79aeaf1ac/go.mod h1:gCcfDlA1Y7GqOaeEKw5l9dOGx1VLdc/HuQSlQAaZ30s= gitlab.bertha.cloud/adphi/gowebdav v0.0.0-20190720232020-771eec6e76d0 h1:kjJ5Xn+FgD+QvWP670A2hmdMqxWkOuffMukEA1JSGo4= gitlab.bertha.cloud/adphi/gowebdav v0.0.0-20190720232020-771eec6e76d0/go.mod h1:Nr6YgM/ZBLPOlAAjcER6HSAXF64AAlal6AJ2CEKg2Fc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/types/interfaces.go b/gonextcloud.go similarity index 67% rename from types/interfaces.go rename to gonextcloud.go index 2f70f1c..c572f7f 100644 --- a/types/interfaces.go +++ b/gonextcloud.go @@ -1,6 +1,17 @@ -package types +package gonextcloud -//Client is the main client interface +import ( + "io" + "os" + "path/filepath" +) + +// NewClient create a new client +func NewClient(hostname string) (Client, error) { + return newClient(hostname) +} + +// Client is the main client interface type Client interface { Apps() Apps AppsConfig() AppsConfig @@ -10,6 +21,7 @@ type Client interface { Users() Users Groups() Groups WebDav() WebDav + Monitoring() (*Monitoring, error) Login(username string, password string) error Logout() error } @@ -19,7 +31,7 @@ type Auth interface { Logout() error } -//Apps available methods +// Apps available methods type Apps interface { List() ([]string, error) ListEnabled() ([]string, error) @@ -29,7 +41,7 @@ type Apps interface { Disable(name string) error } -//AppsConfig available methods +// AppsConfig available methods type AppsConfig interface { List() (apps []string, err error) Keys(id string) (keys []string, err error) @@ -40,7 +52,7 @@ type AppsConfig interface { Details(appID string) (map[string]string, error) } -//Groups available methods +// Groups available methods type Groups interface { List() ([]string, error) ListDetails(search string) ([]Group, error) @@ -51,7 +63,7 @@ type Groups interface { SubAdminList(name string) ([]string, error) } -//GroupFolders available methods +// GroupFolders available methods type GroupFolders interface { List() (map[int]GroupFolder, error) Get(id int) (GroupFolder, error) @@ -63,7 +75,7 @@ type GroupFolders interface { SetQuota(folderID int, quota int) error } -//Notifications available methods +// Notifications available methods type Notifications interface { List() ([]Notification, error) Get(id int) (Notification, error) @@ -74,7 +86,7 @@ type Notifications interface { Available() error } -//Shares available methods +// Shares available methods type Shares interface { List() ([]Share, error) GetFromPath(path string, reshares bool, subfiles bool) ([]Share, error) @@ -95,7 +107,7 @@ type Shares interface { UpdatePermissions(shareID int, permissions SharePermission) error } -//Users available methods +// Users available methods type Users interface { List() ([]string, error) ListDetails() (map[string]UserDetails, error) @@ -124,3 +136,35 @@ type Users interface { GroupDemote(name string, group string) error GroupSubAdminList(name string) ([]string, error) } + +// WebDav available methods +type WebDav interface { + // ReadDir reads the contents of a remote directory + ReadDir(path string) ([]os.FileInfo, error) + // Stat returns the file stats for a specified path + Stat(path string) (os.FileInfo, error) + // Remove removes a remote file + Remove(path string) error + // RemoveAll removes remote files + RemoveAll(path string) error + // Mkdir makes a directory + Mkdir(path string, _ os.FileMode) error + // MkdirAll like mkdir -p, but for webdav + MkdirAll(path string, _ os.FileMode) error + // Rename moves a file from A to B + Rename(oldpath, newpath string, overwrite bool) error + // Copy copies a file from A to B + Copy(oldpath, newpath string, overwrite bool) error + // Read reads the contents of a remote file + Read(path string) ([]byte, error) + // ReadStream reads the stream for a given path + ReadStream(path string) (io.ReadCloser, error) + // Write writes data to a given path + Write(path string, data []byte, _ os.FileMode) error + // WriteStream writes a stream + WriteStream(path string, stream io.Reader, _ os.FileMode) error + + // Walk walks the file tree rooted at root, calling walkFn for each file or + // directory in the tree, including root. + Walk(path string, walkFunc filepath.WalkFunc) error +} diff --git a/gonextcloud_test.go b/gonextcloud_test.go index 7b1b1d7..690261f 100644 --- a/gonextcloud_test.go +++ b/gonextcloud_test.go @@ -2,9 +2,6 @@ package gonextcloud import ( "fmt" - "github.com/stretchr/testify/assert" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" - "gopkg.in/yaml.v2" "io/ioutil" "math/rand" "net/http" @@ -15,6 +12,9 @@ import ( "sync" "testing" "time" + + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v2" ) type Config struct { @@ -35,7 +35,7 @@ type test = func(t *testing.T) var ( config = Config{} - c *Client + c *client groupID = 37 provisionningTests = []struct { string @@ -52,7 +52,7 @@ var ( "create client", func(t *testing.T) { var err error - c, err = NewClient(config.URL) + c, err = newClient(config.URL) assert.NoError(t, err, "aie") }, }, @@ -131,7 +131,7 @@ var ( // return // } // username := fmt.Sprintf("%s-2", config.NotExistingUser) - // user := &types.Users{ + // user := &types.users{ // ID: username, // Displayname: strings.ToUpper(username), // Email: "some@address.com", @@ -140,9 +140,9 @@ var ( // Phone: "42 42 242 424", // Website: "my.site.com", // } - // err := c.Users().Create(username, password, user) + // err := c.users().Create(username, password, user) // assert.NoError(t, err) - // u, err := c.Users().Get(username) + // u, err := c.users().Get(username) // assert.NoError(t, err) // o := structs.Map(user) // r := structs.Map(u) @@ -153,7 +153,7 @@ var ( // assert.Equal(t, o[k], r[k]) // } // // Clean up - // err = c.Users().Delete(u.ID) + // err = c.users().Delete(u.ID) // assert.NoError(t, err) // }, //}, @@ -283,8 +283,8 @@ var ( quota := int64(1024 * 1024 * 1024) err := c.Users().UpdateQuota(config.NotExistingUser, quota) assert.NoError(t, err) - // TODO : Find better verification : A never connected Users does not have quota available - //u, err := c.Users(config.NotExistingUser) + // TODO : Find better verification : A never connected users does not have quota available + //u, err := c.users(config.NotExistingUser) //assert.NoError(t, err) //assert.Equal(t, quota, u.Quota.Quota) }, @@ -407,15 +407,15 @@ var ( { "TestLoggedIn", func(t *testing.T) { - c := &Client{} - c.capabilities = &types.Capabilities{} + c := &client{} + c.capabilities = &Capabilities{} assert.False(t, c.loggedIn()) }, }, { "TestLoginInvalidURL", func(t *testing.T) { - c, _ = NewClient("") + c, _ = newClient("") err := c.Login("", "") assert.Error(t, err) }, @@ -423,7 +423,7 @@ var ( { "TestBaseRequest", func(t *testing.T) { - c, _ = NewClient("") + c, _ = newClient("") _, err := c.baseRequest(http.MethodGet, routes.capabilities, nil, "admin", "invalid") assert.Error(t, err) }, @@ -463,10 +463,10 @@ func TestUserCreateBatchWithoutPassword(t *testing.T) { if c.version.Major < 14 { t.SkipNow() } - var us []types.User + var us []User for i := 0; i < 5; i++ { u := fmt.Sprintf(config.NotExistingUser+"_%d", i) - us = append(us, types.User{ + us = append(us, User{ Username: u, DisplayName: strings.Title(u), Groups: []string{"admin"}, @@ -547,7 +547,7 @@ func initClient() error { return err } var err error - c, err = NewClient(config.URL) + c, err = newClient(config.URL) if err != nil { return err } diff --git a/types/group.go b/group.go similarity index 92% rename from types/group.go rename to group.go index 7fb81b9..4c5c655 100644 --- a/types/group.go +++ b/group.go @@ -1,4 +1,4 @@ -package types +package gonextcloud //Group type Group struct { diff --git a/groupfolders.go b/groupfolders.go index 8ae966e..6c31967 100644 --- a/groupfolders.go +++ b/groupfolders.go @@ -1,140 +1,57 @@ package gonextcloud -import ( - "fmt" - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" - "net/http" - "strconv" -) +import "strconv" -//GroupFolders contains all Groups Folders available actions -type GroupFolders struct { - c *Client +type groupFolderBadFormatIDAndGroups struct { + ID string `json:"id"` + MountPoint string `json:"mount_point"` + Groups map[string]string `json:"groups"` + Quota string `json:"quota"` + Size int `json:"size"` } -//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 { - return nil, err - } - var r types.GroupFoldersListResponse - res.JSON(&r) - gfs := formatBadIDAndGroups(r.Ocs.Data) - return gfs, nil +type groupFolderBadFormatGroups struct { + ID int `json:"id"` + MountPoint string `json:"mount_point"` + Groups map[string]string `json:"groups"` + Quota string `json:"quota"` + Size int `json:"size"` } -//Get returns the group folder details -func (g *GroupFolders) Get(id int) (types.GroupFolder, error) { - res, err := g.c.baseRequest(http.MethodGet, routes.groupfolders, nil, strconv.Itoa(id)) - if err != nil { - return types.GroupFolder{}, err - } - var r types.GroupFoldersResponse - res.JSON(&r) - if r.Ocs.Data.ID == 0 { - return types.GroupFolder{}, fmt.Errorf("%d is not a valid groupfolder's id", id) - } - return r.Ocs.Data.FormatGroupFolder(), nil +type GroupFolder struct { + ID int `json:"id"` + MountPoint string `json:"mount_point"` + Groups map[string]SharePermission `json:"groups"` + Quota int `json:"quota"` + Size int `json:"size"` } -//Create creates a group folder -func (g *GroupFolders) Create(name string) (id int, err error) { - // TODO: Validate Folder name - ro := &req.RequestOptions{ - Data: map[string]string{ - "mountpoint": name, - }, +func (gf *groupFolderBadFormatGroups) FormatGroupFolder() GroupFolder { + g := GroupFolder{} + g.ID = gf.ID + g.MountPoint = gf.MountPoint + g.Groups = map[string]SharePermission{} + for k, v := range gf.Groups { + p, _ := strconv.Atoi(v) + g.Groups[k] = SharePermission(p) } - res, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro) - if err != nil { - return 0, err - } - var r types.GroupFoldersCreateResponse - res.JSON(&r) - id, _ = strconv.Atoi(r.Ocs.Data.ID) - return id, nil + q, _ := strconv.Atoi(gf.Quota) + g.Quota = q + g.Size = gf.Size + return g } -//Rename renames the group folder -func (g *GroupFolders) Rename(groupID int, name string) error { - ro := &req.RequestOptions{ - Data: map[string]string{ - "mountpoint": name, - }, +func (gf *groupFolderBadFormatIDAndGroups) FormatGroupFolder() GroupFolder { + g := GroupFolder{} + g.ID, _ = strconv.Atoi(gf.ID) + g.MountPoint = gf.MountPoint + g.Groups = map[string]SharePermission{} + for k, v := range gf.Groups { + p, _ := strconv.Atoi(v) + g.Groups[k] = SharePermission(p) } - // GroupFolders's response does not give any clues about success or failure - _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(groupID), "mountpoint") - if err != nil { - return err - } - return nil -} - -//TODO func (c *Client) GroupFoldersDelete(id int) error { - -//AddGroup adds group to folder -func (g *GroupFolders) AddGroup(folderID int, groupName string) error { - ro := &req.RequestOptions{ - Data: map[string]string{ - "group": groupName, - }, - } - // GroupFolders's response does not give any clues about success or failure - _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups") - if err != nil { - return err - } - return nil -} - -//RemoveGroup remove a group from the group folder -func (g *GroupFolders) RemoveGroup(folderID int, groupName string) error { - // GroupFolders's response does not give any clues about success or failure - _, err := g.c.baseRequest(http.MethodDelete, routes.groupfolders, nil, strconv.Itoa(folderID), "groups", groupName) - if err != nil { - return err - } - return nil -} - -//SetGroupPermissions set groups permissions -func (g *GroupFolders) SetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error { - ro := &req.RequestOptions{ - Data: map[string]string{ - "permissions": strconv.Itoa(int(permission)), - }, - } - // GroupFolders's response does not give any clues about success or failure - _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups", groupName) - if err != nil { - return err - } - return nil -} - -//SetQuota set quota on the group folder. quota in bytes, use -3 for unlimited -func (g *GroupFolders) SetQuota(folderID int, quota int) error { - ro := &req.RequestOptions{ - Data: map[string]string{ - "quota": strconv.Itoa(int(quota)), - }, - } - // GroupFolders's response does not give any clues about success or failure - _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "quota") - if err != nil { - return err - } - return nil -} - -func formatBadIDAndGroups(g map[string]types.GroupFolderBadFormatIDAndGroups) map[int]types.GroupFolder { - var gfs = map[int]types.GroupFolder{} - for k := range g { - i, _ := strconv.Atoi(k) - d := g[k] - gfs[i] = d.FormatGroupFolder() - } - return gfs + q, _ := strconv.Atoi(gf.Quota) + g.Quota = q + g.Size = gf.Size + return g } diff --git a/groupfolders_impl.go b/groupfolders_impl.go new file mode 100644 index 0000000..1f99723 --- /dev/null +++ b/groupfolders_impl.go @@ -0,0 +1,140 @@ +package gonextcloud + +import ( + "fmt" + "net/http" + "strconv" + + req "github.com/levigross/grequests" +) + +//groupFolders contains all groups Folders available actions +type groupFolders struct { + c *client +} + +//List returns the groups folders +func (g *groupFolders) List() (map[int]GroupFolder, error) { + res, err := g.c.baseRequest(http.MethodGet, routes.groupfolders, nil) + if err != nil { + return nil, err + } + var r groupFoldersListResponse + res.JSON(&r) + gfs := formatBadIDAndGroups(r.Ocs.Data) + return gfs, nil +} + +//Get returns the group folder details +func (g *groupFolders) Get(id int) (GroupFolder, error) { + res, err := g.c.baseRequest(http.MethodGet, routes.groupfolders, nil, strconv.Itoa(id)) + if err != nil { + return GroupFolder{}, err + } + var r groupFoldersResponse + res.JSON(&r) + if r.Ocs.Data.ID == 0 { + return GroupFolder{}, fmt.Errorf("%d is not a valid groupfolder's id", id) + } + return r.Ocs.Data.FormatGroupFolder(), nil +} + +//Create creates a group folder +func (g *groupFolders) Create(name string) (id int, err error) { + // TODO: Validate Folder name + ro := &req.RequestOptions{ + Data: map[string]string{ + "mountpoint": name, + }, + } + res, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro) + if err != nil { + return 0, err + } + var r groupFoldersCreateResponse + res.JSON(&r) + id, _ = strconv.Atoi(r.Ocs.Data.ID) + return id, nil +} + +//Rename renames the group folder +func (g *groupFolders) Rename(groupID int, name string) error { + ro := &req.RequestOptions{ + Data: map[string]string{ + "mountpoint": name, + }, + } + // groupFolders's response does not give any clues about success or failure + _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(groupID), "mountpoint") + if err != nil { + return err + } + return nil +} + +//TODO func (c *client) GroupFoldersDelete(id int) error { + +//AddGroup adds group to folder +func (g *groupFolders) AddGroup(folderID int, groupName string) error { + ro := &req.RequestOptions{ + Data: map[string]string{ + "group": groupName, + }, + } + // groupFolders's response does not give any clues about success or failure + _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups") + if err != nil { + return err + } + return nil +} + +//RemoveGroup remove a group from the group folder +func (g *groupFolders) RemoveGroup(folderID int, groupName string) error { + // groupFolders's response does not give any clues about success or failure + _, err := g.c.baseRequest(http.MethodDelete, routes.groupfolders, nil, strconv.Itoa(folderID), "groups", groupName) + if err != nil { + return err + } + return nil +} + +//SetGroupPermissions set groups permissions +func (g *groupFolders) SetGroupPermissions(folderID int, groupName string, permission SharePermission) error { + ro := &req.RequestOptions{ + Data: map[string]string{ + "permissions": strconv.Itoa(int(permission)), + }, + } + // groupFolders's response does not give any clues about success or failure + _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "groups", groupName) + if err != nil { + return err + } + return nil +} + +//SetQuota set quota on the group folder. quota in bytes, use -3 for unlimited +func (g *groupFolders) SetQuota(folderID int, quota int) error { + ro := &req.RequestOptions{ + Data: map[string]string{ + "quota": strconv.Itoa(int(quota)), + }, + } + // groupFolders's response does not give any clues about success or failure + _, err := g.c.baseRequest(http.MethodPost, routes.groupfolders, ro, strconv.Itoa(folderID), "quota") + if err != nil { + return err + } + return nil +} + +func formatBadIDAndGroups(g map[string]groupFolderBadFormatIDAndGroups) map[int]GroupFolder { + var gfs = map[int]GroupFolder{} + for k := range g { + i, _ := strconv.Atoi(k) + d := g[k] + gfs[i] = d.FormatGroupFolder() + } + return gfs +} diff --git a/groupfolders_test.go b/groupfolders_impl_test.go similarity index 94% rename from groupfolders_test.go rename to groupfolders_impl_test.go index 8d2cd13..9a7d0c7 100644 --- a/groupfolders_test.go +++ b/groupfolders_impl_test.go @@ -1,9 +1,9 @@ package gonextcloud import ( - "github.com/stretchr/testify/assert" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "testing" + + "github.com/stretchr/testify/assert" ) var ( @@ -53,7 +53,7 @@ var ( { "TestGroupFoldersSetGroupPermissions", func(t *testing.T) { - err := c.GroupFolders().SetGroupPermissions(groupID, "admin", types.ReadPermission) + err := c.GroupFolders().SetGroupPermissions(groupID, "admin", ReadPermission) assert.NoError(t, err) }, }, diff --git a/groups.go b/groups_impl.go similarity index 66% rename from groups.go rename to groups_impl.go index 1dec769..eb1d30e 100644 --- a/groups.go +++ b/groups_impl.go @@ -1,29 +1,29 @@ package gonextcloud import ( - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "net/http" + + req "github.com/levigross/grequests" ) -//Groups contains all Groups available actions -type Groups struct { - c *Client +//groups contains all groups available actions +type groups struct { + c *client } //List lists the Nextcloud groups -func (g *Groups) List() ([]string, error) { +func (g *groups) List() ([]string, error) { res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil) if err != nil { return nil, err } - var r types.GroupListResponse + var r groupListResponse res.JSON(&r) return r.Ocs.Data.Groups, nil } //ListDetails lists the Nextcloud groups -func (g *Groups) ListDetails(search string) ([]types.Group, error) { +func (g *groups) ListDetails(search string) ([]Group, error) { ro := &req.RequestOptions{ Params: map[string]string{ "search": search, @@ -33,24 +33,24 @@ func (g *Groups) ListDetails(search string) ([]types.Group, error) { if err != nil { return nil, err } - var r types.GroupListDetailsResponse + var r groupListDetailsResponse res.JSON(&r) return r.Ocs.Data.Groups, nil } -//Users list the group's users -func (g *Groups) Users(name string) ([]string, error) { +//users list the group's users +func (g *groups) Users(name string) ([]string, error) { res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil, name) if err != nil { return nil, err } - var r types.UserListResponse + var r userListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } //Search return the list of groups matching the search string -func (g *Groups) Search(search string) ([]string, error) { +func (g *groups) Search(search string) ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"search": search}, } @@ -58,13 +58,13 @@ func (g *Groups) Search(search string) ([]string, error) { if err != nil { return nil, err } - var r types.GroupListResponse + var r groupListResponse res.JSON(&r) return r.Ocs.Data.Groups, nil } //Create creates a group -func (g *Groups) Create(name string) error { +func (g *groups) Create(name string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": name, @@ -74,22 +74,22 @@ func (g *Groups) Create(name string) error { } //Delete deletes the group -func (g *Groups) Delete(name string) error { +func (g *groups) Delete(name string) error { return g.baseRequest(http.MethodDelete, nil, name) } //SubAdminList lists the group's subadmins -func (g *Groups) SubAdminList(name string) ([]string, error) { +func (g *groups) SubAdminList(name string) ([]string, error) { res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil, name, "subadmins") if err != nil { return nil, err } - var r types.UserListResponse + var r userListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } -func (g *Groups) baseRequest(method string, ro *req.RequestOptions, subRoute ...string) error { +func (g *groups) baseRequest(method string, ro *req.RequestOptions, subRoute ...string) error { _, err := g.c.baseRequest(method, routes.groups, ro, subRoute...) return err } diff --git a/types/mocks/Apps.go b/mocks/Apps.go similarity index 87% rename from types/mocks/Apps.go rename to mocks/Apps.go index 7118743..1983837 100644 --- a/types/mocks/Apps.go +++ b/mocks/Apps.go @@ -2,8 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import types "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" +import ( + "github.com/stretchr/testify/mock" + + "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" +) // Apps is an autogenerated mock type for the Apps type type Apps struct { @@ -39,14 +42,14 @@ func (_m *Apps) Enable(name string) error { } // Infos provides a mock function with given fields: name -func (_m *Apps) Infos(name string) (types.App, error) { +func (_m *Apps) Infos(name string) (gonextcloud.App, error) { ret := _m.Called(name) - var r0 types.App - if rf, ok := ret.Get(0).(func(string) types.App); ok { + var r0 gonextcloud.App + if rf, ok := ret.Get(0).(func(string) gonextcloud.App); ok { r0 = rf(name) } else { - r0 = ret.Get(0).(types.App) + r0 = ret.Get(0).(gonextcloud.App) } var r1 error diff --git a/types/mocks/AppsConfig.go b/mocks/AppsConfig.go similarity index 100% rename from types/mocks/AppsConfig.go rename to mocks/AppsConfig.go diff --git a/types/mocks/Auth.go b/mocks/Auth.go similarity index 100% rename from types/mocks/Auth.go rename to mocks/Auth.go diff --git a/types/mocks/Client.go b/mocks/Client.go similarity index 55% rename from types/mocks/Client.go rename to mocks/Client.go index 8535d3b..17743e5 100644 --- a/types/mocks/Client.go +++ b/mocks/Client.go @@ -2,8 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import types "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" +import ( + "github.com/stretchr/testify/mock" + + "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" +) // Client is an autogenerated mock type for the Client type type Client struct { @@ -11,15 +14,15 @@ type Client struct { } // Apps provides a mock function with given fields: -func (_m *Client) Apps() types.Apps { +func (_m *Client) Apps() gonextcloud.Apps { ret := _m.Called() - var r0 types.Apps - if rf, ok := ret.Get(0).(func() types.Apps); ok { + var r0 gonextcloud.Apps + if rf, ok := ret.Get(0).(func() gonextcloud.Apps); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(types.Apps) + r0 = ret.Get(0).(gonextcloud.Apps) } } @@ -27,15 +30,15 @@ func (_m *Client) Apps() types.Apps { } // AppsConfig provides a mock function with given fields: -func (_m *Client) AppsConfig() types.AppsConfig { +func (_m *Client) AppsConfig() gonextcloud.AppsConfig { ret := _m.Called() - var r0 types.AppsConfig - if rf, ok := ret.Get(0).(func() types.AppsConfig); ok { + var r0 gonextcloud.AppsConfig + if rf, ok := ret.Get(0).(func() gonextcloud.AppsConfig); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(types.AppsConfig) + r0 = ret.Get(0).(gonextcloud.AppsConfig) } } @@ -43,15 +46,15 @@ func (_m *Client) AppsConfig() types.AppsConfig { } // GroupFolders provides a mock function with given fields: -func (_m *Client) GroupFolders() types.GroupFolders { +func (_m *Client) GroupFolders() gonextcloud.GroupFolders { ret := _m.Called() - var r0 types.GroupFolders - if rf, ok := ret.Get(0).(func() types.GroupFolders); ok { + var r0 gonextcloud.GroupFolders + if rf, ok := ret.Get(0).(func() gonextcloud.GroupFolders); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(types.GroupFolders) + r0 = ret.Get(0).(gonextcloud.GroupFolders) } } @@ -59,15 +62,15 @@ func (_m *Client) GroupFolders() types.GroupFolders { } // Groups provides a mock function with given fields: -func (_m *Client) Groups() types.Groups { +func (_m *Client) Groups() gonextcloud.Groups { ret := _m.Called() - var r0 types.Groups - if rf, ok := ret.Get(0).(func() types.Groups); ok { + var r0 gonextcloud.Groups + if rf, ok := ret.Get(0).(func() gonextcloud.Groups); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(types.Groups) + r0 = ret.Get(0).(gonextcloud.Groups) } } @@ -103,15 +106,15 @@ func (_m *Client) Logout() error { } // Notifications provides a mock function with given fields: -func (_m *Client) Notifications() types.Notifications { +func (_m *Client) Notifications() gonextcloud.Notifications { ret := _m.Called() - var r0 types.Notifications - if rf, ok := ret.Get(0).(func() types.Notifications); ok { + var r0 gonextcloud.Notifications + if rf, ok := ret.Get(0).(func() gonextcloud.Notifications); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(types.Notifications) + r0 = ret.Get(0).(gonextcloud.Notifications) } } @@ -119,15 +122,15 @@ func (_m *Client) Notifications() types.Notifications { } // Shares provides a mock function with given fields: -func (_m *Client) Shares() types.Shares { +func (_m *Client) Shares() gonextcloud.Shares { ret := _m.Called() - var r0 types.Shares - if rf, ok := ret.Get(0).(func() types.Shares); ok { + var r0 gonextcloud.Shares + if rf, ok := ret.Get(0).(func() gonextcloud.Shares); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(types.Shares) + r0 = ret.Get(0).(gonextcloud.Shares) } } @@ -135,15 +138,15 @@ func (_m *Client) Shares() types.Shares { } // Users provides a mock function with given fields: -func (_m *Client) Users() types.Users { +func (_m *Client) Users() gonextcloud.Users { ret := _m.Called() - var r0 types.Users - if rf, ok := ret.Get(0).(func() types.Users); ok { + var r0 gonextcloud.Users + if rf, ok := ret.Get(0).(func() gonextcloud.Users); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(types.Users) + r0 = ret.Get(0).(gonextcloud.Users) } } diff --git a/types/mocks/GroupFolders.go b/mocks/GroupFolders.go similarity index 79% rename from types/mocks/GroupFolders.go rename to mocks/GroupFolders.go index 45241c9..0d6dfb7 100644 --- a/types/mocks/GroupFolders.go +++ b/mocks/GroupFolders.go @@ -2,8 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import types "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" +import ( + "github.com/stretchr/testify/mock" + + "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" +) // GroupFolders is an autogenerated mock type for the GroupFolders type type GroupFolders struct { @@ -46,14 +49,14 @@ func (_m *GroupFolders) Create(name string) (int, error) { } // Get provides a mock function with given fields: id -func (_m *GroupFolders) Get(id int) (types.GroupFolder, error) { +func (_m *GroupFolders) Get(id int) (gonextcloud.GroupFolder, error) { ret := _m.Called(id) - var r0 types.GroupFolder - if rf, ok := ret.Get(0).(func(int) types.GroupFolder); ok { + var r0 gonextcloud.GroupFolder + if rf, ok := ret.Get(0).(func(int) gonextcloud.GroupFolder); ok { r0 = rf(id) } else { - r0 = ret.Get(0).(types.GroupFolder) + r0 = ret.Get(0).(gonextcloud.GroupFolder) } var r1 error @@ -67,15 +70,15 @@ func (_m *GroupFolders) Get(id int) (types.GroupFolder, error) { } // List provides a mock function with given fields: -func (_m *GroupFolders) List() (map[int]types.GroupFolder, error) { +func (_m *GroupFolders) List() (map[int]gonextcloud.GroupFolder, error) { ret := _m.Called() - var r0 map[int]types.GroupFolder - if rf, ok := ret.Get(0).(func() map[int]types.GroupFolder); ok { + var r0 map[int]gonextcloud.GroupFolder + if rf, ok := ret.Get(0).(func() map[int]gonextcloud.GroupFolder); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(map[int]types.GroupFolder) + r0 = ret.Get(0).(map[int]gonextcloud.GroupFolder) } } @@ -118,11 +121,11 @@ func (_m *GroupFolders) Rename(groupID int, name string) error { } // SetGroupPermissions provides a mock function with given fields: folderID, groupName, permission -func (_m *GroupFolders) SetGroupPermissions(folderID int, groupName string, permission types.SharePermission) error { +func (_m *GroupFolders) SetGroupPermissions(folderID int, groupName string, permission gonextcloud.SharePermission) error { ret := _m.Called(folderID, groupName, permission) var r0 error - if rf, ok := ret.Get(0).(func(int, string, types.SharePermission) error); ok { + if rf, ok := ret.Get(0).(func(int, string, gonextcloud.SharePermission) error); ok { r0 = rf(folderID, groupName, permission) } else { r0 = ret.Error(0) diff --git a/types/mocks/Groups.go b/mocks/Groups.go similarity index 89% rename from types/mocks/Groups.go rename to mocks/Groups.go index 8b758da..22c7c1f 100644 --- a/types/mocks/Groups.go +++ b/mocks/Groups.go @@ -2,8 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import types "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" +import ( + "github.com/stretchr/testify/mock" + + "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" +) // Groups is an autogenerated mock type for the Groups type type Groups struct { @@ -62,15 +65,15 @@ func (_m *Groups) List() ([]string, error) { } // ListDetails provides a mock function with given fields: search -func (_m *Groups) ListDetails(search string) ([]types.Group, error) { +func (_m *Groups) ListDetails(search string) ([]gonextcloud.Group, error) { ret := _m.Called(search) - var r0 []types.Group - if rf, ok := ret.Get(0).(func(string) []types.Group); ok { + var r0 []gonextcloud.Group + if rf, ok := ret.Get(0).(func(string) []gonextcloud.Group); ok { r0 = rf(search) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).([]types.Group) + r0 = ret.Get(0).([]gonextcloud.Group) } } diff --git a/types/mocks/Notifications.go b/mocks/Notifications.go similarity index 78% rename from types/mocks/Notifications.go rename to mocks/Notifications.go index dca767d..ea0be0d 100644 --- a/types/mocks/Notifications.go +++ b/mocks/Notifications.go @@ -2,8 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import types "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" +import ( + "github.com/stretchr/testify/mock" + + "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" +) // Notifications is an autogenerated mock type for the Notifications type type Notifications struct { @@ -81,14 +84,14 @@ func (_m *Notifications) DeleteAll() error { } // Get provides a mock function with given fields: id -func (_m *Notifications) Get(id int) (types.Notification, error) { +func (_m *Notifications) Get(id int) (gonextcloud.Notification, error) { ret := _m.Called(id) - var r0 types.Notification - if rf, ok := ret.Get(0).(func(int) types.Notification); ok { + var r0 gonextcloud.Notification + if rf, ok := ret.Get(0).(func(int) gonextcloud.Notification); ok { r0 = rf(id) } else { - r0 = ret.Get(0).(types.Notification) + r0 = ret.Get(0).(gonextcloud.Notification) } var r1 error @@ -102,15 +105,15 @@ func (_m *Notifications) Get(id int) (types.Notification, error) { } // List provides a mock function with given fields: -func (_m *Notifications) List() ([]types.Notification, error) { +func (_m *Notifications) List() ([]gonextcloud.Notification, error) { ret := _m.Called() - var r0 []types.Notification - if rf, ok := ret.Get(0).(func() []types.Notification); ok { + var r0 []gonextcloud.Notification + if rf, ok := ret.Get(0).(func() []gonextcloud.Notification); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).([]types.Notification) + r0 = ret.Get(0).([]gonextcloud.Notification) } } diff --git a/types/mocks/Shares.go b/mocks/Shares.go similarity index 68% rename from types/mocks/Shares.go rename to mocks/Shares.go index c74ef1e..95c597f 100644 --- a/types/mocks/Shares.go +++ b/mocks/Shares.go @@ -2,8 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import types "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" +import ( + "github.com/stretchr/testify/mock" + + "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" +) // Shares is an autogenerated mock type for the Shares type type Shares struct { @@ -11,18 +14,18 @@ type Shares struct { } // Create provides a mock function with given fields: path, shareType, permission, shareWith, publicUpload, password -func (_m *Shares) Create(path string, shareType types.ShareType, permission types.SharePermission, shareWith string, publicUpload bool, password string) (types.Share, error) { +func (_m *Shares) Create(path string, shareType gonextcloud.ShareType, permission gonextcloud.SharePermission, shareWith string, publicUpload bool, password string) (gonextcloud.Share, error) { ret := _m.Called(path, shareType, permission, shareWith, publicUpload, password) - var r0 types.Share - if rf, ok := ret.Get(0).(func(string, types.ShareType, types.SharePermission, string, bool, string) types.Share); ok { + var r0 gonextcloud.Share + if rf, ok := ret.Get(0).(func(string, gonextcloud.ShareType, gonextcloud.SharePermission, string, bool, string) gonextcloud.Share); ok { r0 = rf(path, shareType, permission, shareWith, publicUpload, password) } else { - r0 = ret.Get(0).(types.Share) + r0 = ret.Get(0).(gonextcloud.Share) } var r1 error - if rf, ok := ret.Get(1).(func(string, types.ShareType, types.SharePermission, string, bool, string) error); ok { + if rf, ok := ret.Get(1).(func(string, gonextcloud.ShareType, gonextcloud.SharePermission, string, bool, string) error); ok { r1 = rf(path, shareType, permission, shareWith, publicUpload, password) } else { r1 = ret.Error(1) @@ -46,14 +49,14 @@ func (_m *Shares) Delete(shareID int) error { } // Get provides a mock function with given fields: shareID -func (_m *Shares) Get(shareID string) (types.Share, error) { +func (_m *Shares) Get(shareID string) (gonextcloud.Share, error) { ret := _m.Called(shareID) - var r0 types.Share - if rf, ok := ret.Get(0).(func(string) types.Share); ok { + var r0 gonextcloud.Share + if rf, ok := ret.Get(0).(func(string) gonextcloud.Share); ok { r0 = rf(shareID) } else { - r0 = ret.Get(0).(types.Share) + r0 = ret.Get(0).(gonextcloud.Share) } var r1 error @@ -67,15 +70,15 @@ func (_m *Shares) Get(shareID string) (types.Share, error) { } // GetFromPath provides a mock function with given fields: path, reshares, subfiles -func (_m *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error) { +func (_m *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]gonextcloud.Share, error) { ret := _m.Called(path, reshares, subfiles) - var r0 []types.Share - if rf, ok := ret.Get(0).(func(string, bool, bool) []types.Share); ok { + var r0 []gonextcloud.Share + if rf, ok := ret.Get(0).(func(string, bool, bool) []gonextcloud.Share); ok { r0 = rf(path, reshares, subfiles) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).([]types.Share) + r0 = ret.Get(0).([]gonextcloud.Share) } } @@ -90,15 +93,15 @@ func (_m *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]type } // List provides a mock function with given fields: -func (_m *Shares) List() ([]types.Share, error) { +func (_m *Shares) List() ([]gonextcloud.Share, error) { ret := _m.Called() - var r0 []types.Share - if rf, ok := ret.Get(0).(func() []types.Share); ok { + var r0 []gonextcloud.Share + if rf, ok := ret.Get(0).(func() []gonextcloud.Share); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).([]types.Share) + r0 = ret.Get(0).([]gonextcloud.Share) } } @@ -113,11 +116,11 @@ func (_m *Shares) List() ([]types.Share, error) { } // Update provides a mock function with given fields: shareUpdate -func (_m *Shares) Update(shareUpdate types.ShareUpdate) error { +func (_m *Shares) Update(shareUpdate gonextcloud.ShareUpdate) error { ret := _m.Called(shareUpdate) var r0 error - if rf, ok := ret.Get(0).(func(types.ShareUpdate) error); ok { + if rf, ok := ret.Get(0).(func(gonextcloud.ShareUpdate) error); ok { r0 = rf(shareUpdate) } else { r0 = ret.Error(0) @@ -155,11 +158,11 @@ func (_m *Shares) UpdatePassword(shareID int, password string) error { } // UpdatePermissions provides a mock function with given fields: shareID, permissions -func (_m *Shares) UpdatePermissions(shareID int, permissions types.SharePermission) error { +func (_m *Shares) UpdatePermissions(shareID int, permissions gonextcloud.SharePermission) error { ret := _m.Called(shareID, permissions) var r0 error - if rf, ok := ret.Get(0).(func(int, types.SharePermission) error); ok { + if rf, ok := ret.Get(0).(func(int, gonextcloud.SharePermission) error); ok { r0 = rf(shareID, permissions) } else { r0 = ret.Error(0) diff --git a/types/mocks/Users.go b/mocks/Users.go similarity index 89% rename from types/mocks/Users.go rename to mocks/Users.go index ae975a5..b2e5309 100644 --- a/types/mocks/Users.go +++ b/mocks/Users.go @@ -2,8 +2,11 @@ package mocks -import mock "github.com/stretchr/testify/mock" -import types "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" +import ( + "github.com/stretchr/testify/mock" + + "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud" +) // Users is an autogenerated mock type for the Users type type Users struct { @@ -11,11 +14,11 @@ type Users struct { } // Create provides a mock function with given fields: username, password, user -func (_m *Users) Create(username string, password string, user *types.UserDetails) error { +func (_m *Users) Create(username string, password string, user *gonextcloud.UserDetails) error { ret := _m.Called(username, password, user) var r0 error - if rf, ok := ret.Get(0).(func(string, string, *types.UserDetails) error); ok { + if rf, ok := ret.Get(0).(func(string, string, *gonextcloud.UserDetails) error); ok { r0 = rf(username, password, user) } else { r0 = ret.Error(0) @@ -25,11 +28,11 @@ func (_m *Users) Create(username string, password string, user *types.UserDetail } // CreateBatchWithoutPassword provides a mock function with given fields: users -func (_m *Users) CreateBatchWithoutPassword(users []types.User) error { +func (_m *Users) CreateBatchWithoutPassword(users []gonextcloud.User) error { ret := _m.Called(users) var r0 error - if rf, ok := ret.Get(0).(func([]types.User) error); ok { + if rf, ok := ret.Get(0).(func([]gonextcloud.User) error); ok { r0 = rf(users) } else { r0 = ret.Error(0) @@ -102,15 +105,15 @@ func (_m *Users) Enable(name string) error { } // Get provides a mock function with given fields: name -func (_m *Users) Get(name string) (*types.UserDetails, error) { +func (_m *Users) Get(name string) (*gonextcloud.UserDetails, error) { ret := _m.Called(name) - var r0 *types.UserDetails - if rf, ok := ret.Get(0).(func(string) *types.UserDetails); ok { + var r0 *gonextcloud.UserDetails + if rf, ok := ret.Get(0).(func(string) *gonextcloud.UserDetails); ok { r0 = rf(name) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*types.UserDetails) + r0 = ret.Get(0).(*gonextcloud.UserDetails) } } @@ -250,15 +253,15 @@ func (_m *Users) List() ([]string, error) { } // ListDetails provides a mock function with given fields: -func (_m *Users) ListDetails() (map[string]types.UserDetails, error) { +func (_m *Users) ListDetails() (map[string]gonextcloud.UserDetails, error) { ret := _m.Called() - var r0 map[string]types.UserDetails - if rf, ok := ret.Get(0).(func() map[string]types.UserDetails); ok { + var r0 map[string]gonextcloud.UserDetails + if rf, ok := ret.Get(0).(func() map[string]gonextcloud.UserDetails); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(map[string]types.UserDetails) + r0 = ret.Get(0).(map[string]gonextcloud.UserDetails) } } @@ -310,11 +313,11 @@ func (_m *Users) SendWelcomeEmail(name string) error { } // Update provides a mock function with given fields: user -func (_m *Users) Update(user *types.UserDetails) error { +func (_m *Users) Update(user *gonextcloud.UserDetails) error { ret := _m.Called(user) var r0 error - if rf, ok := ret.Get(0).(func(*types.UserDetails) error); ok { + if rf, ok := ret.Get(0).(func(*gonextcloud.UserDetails) error); ok { r0 = rf(user) } else { r0 = ret.Error(0) diff --git a/monitoring.go b/monitoring.go index a88dfc2..29dccc4 100644 --- a/monitoring.go +++ b/monitoring.go @@ -1,17 +1,65 @@ package gonextcloud -import ( - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" - "net/http" -) - -//Monitoring return nextcloud monitoring statistics -func (c *Client) Monitoring() (*types.Monitoring, error) { - res, err := c.baseRequest(http.MethodGet, routes.monitor, nil) - if err != nil { - return nil, err - } - var m types.MonitoringResponse - res.JSON(&m) - return &m.Ocs.Data, nil +type System struct { + Version string `json:"version"` + Theme string `json:"theme"` + EnableAvatars string `json:"enable_avatars"` + EnablePreviews string `json:"enable_previews"` + MemcacheLocal string `json:"memcache.local"` + MemcacheDistributed string `json:"memcache.distributed"` + FilelockingEnabled string `json:"filelocking.enabled"` + MemcacheLocking string `json:"memcache.locking"` + Debug string `json:"debug"` + Freespace int64 `json:"freespace"` + Cpuload []float32 `json:"cpuload"` + MemTotal int `json:"mem_total"` + MemFree int `json:"mem_free"` + SwapTotal int `json:"swap_total"` + SwapFree int `json:"swap_free"` +} + +type Monitoring struct { + Nextcloud struct { + System System `json:"system"` + Storage Storage `json:"storage"` + Shares struct { + NumShares int `json:"num_shares"` + NumSharesUser int `json:"num_shares_user"` + NumSharesGroups int `json:"num_shares_groups"` + NumSharesLink int `json:"num_shares_link"` + NumSharesLinkNoPassword int `json:"num_shares_link_no_password"` + NumFedSharesSent int `json:"num_fed_shares_sent"` + NumFedSharesReceived int `json:"num_fed_shares_received"` + } `json:"shares"` + } `json:"nextcloud"` + Server struct { + Webserver string `json:"webserver"` + Php struct { + Version string `json:"version"` + MemoryLimit int `json:"memory_limit"` + MaxExecutionTime int `json:"max_execution_time"` + UploadMaxFilesize int `json:"upload_max_filesize"` + } `json:"php"` + Database struct { + Type string `json:"type"` + Version string `json:"version"` + Size int `json:"size"` + } `json:"database"` + } `json:"server"` + ActiveUsers ActiveUsers `json:"activeUsers"` +} + +type ActiveUsers struct { + Last5Minutes int `json:"last5minutes"` + Last1Hour int `json:"last1hour"` + Last24Hours int `json:"last24hours"` +} + +type Storage struct { + NumUsers int `json:"num_users"` + NumFiles int `json:"num_files"` + NumStorages int `json:"num_storages"` + NumStoragesLocal int `json:"num_storages_local"` + NumStoragesHome int `json:"num_storages_home"` + NumStoragesOther int `json:"num_storages_other"` } diff --git a/monitoring_impl.go b/monitoring_impl.go new file mode 100644 index 0000000..6c70a6c --- /dev/null +++ b/monitoring_impl.go @@ -0,0 +1,16 @@ +package gonextcloud + +import ( + "net/http" +) + +//Monitoring return nextcloud monitoring statistics +func (c *client) Monitoring() (*Monitoring, error) { + res, err := c.baseRequest(http.MethodGet, routes.monitor, nil) + if err != nil { + return nil, err + } + var m monitoringResponse + res.JSON(&m) + return &m.Ocs.Data, nil +} diff --git a/types/notification.go b/notification.go similarity index 97% rename from types/notification.go rename to notification.go index 931b6a6..5690f63 100644 --- a/types/notification.go +++ b/notification.go @@ -1,4 +1,4 @@ -package types +package gonextcloud import "time" diff --git a/notifications.go b/notifications_impl.go similarity index 72% rename from notifications.go rename to notifications_impl.go index badf7f4..53ff7db 100644 --- a/notifications.go +++ b/notifications_impl.go @@ -2,19 +2,19 @@ package gonextcloud import ( "errors" - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "net/http" "strconv" + + req "github.com/levigross/grequests" ) -//Notifications contains all Notifications available actions -type Notifications struct { - c *Client +//notifications contains all notifications available actions +type notifications struct { + c *client } //List returns all the notifications -func (n *Notifications) List() ([]types.Notification, error) { +func (n *notifications) List() ([]Notification, error) { if err := n.Available(); err != nil { return nil, err } @@ -22,27 +22,27 @@ func (n *Notifications) List() ([]types.Notification, error) { if err != nil { return nil, err } - var r types.NotificationsListResponse + var r notificationsListResponse res.JSON(&r) return r.Ocs.Data, nil } //Get returns the notification corresponding to the id -func (n *Notifications) Get(id int) (types.Notification, error) { +func (n *notifications) Get(id int) (Notification, error) { if err := n.Available(); err != nil { - return types.Notification{}, err + return Notification{}, err } res, err := n.c.baseRequest(http.MethodGet, routes.notifications, nil, strconv.Itoa(id)) if err != nil { - return types.Notification{}, err + return Notification{}, err } - var r types.NotificationResponse + var r notificationResponse res.JSON(&r) return r.Ocs.Data, nil } //Delete deletes the notification corresponding to the id -func (n *Notifications) Delete(id int) error { +func (n *notifications) Delete(id int) error { if err := n.Available(); err != nil { return err } @@ -51,7 +51,7 @@ func (n *Notifications) Delete(id int) error { } //DeleteAll deletes all notifications -func (n *Notifications) DeleteAll() error { +func (n *notifications) DeleteAll() error { if err := n.Available(); err != nil { return err } @@ -60,7 +60,7 @@ func (n *Notifications) DeleteAll() error { } //Create creates a notification (if the user is an admin) -func (n *Notifications) Create(userID, title, message string) error { +func (n *notifications) Create(userID, title, message string) error { if err := n.AdminAvailable(); err != nil { return err } @@ -75,7 +75,7 @@ func (n *Notifications) Create(userID, title, message string) error { } //AdminAvailable returns an error if the admin-notifications app is not installed -func (n *Notifications) AdminAvailable() error { +func (n *notifications) AdminAvailable() error { if len(n.c.capabilities.Notifications.AdminNotifications) == 0 { return errors.New("'admin notifications' not available on this instance") } @@ -83,7 +83,7 @@ func (n *Notifications) AdminAvailable() error { } //Available returns an error if the notifications app is not installed -func (n *Notifications) Available() error { +func (n *notifications) Available() error { if len(n.c.capabilities.Notifications.OcsEndpoints) == 0 { return errors.New("notifications not available on this instance") } diff --git a/notifications_test.go b/notifications_impl_test.go similarity index 100% rename from notifications_test.go rename to notifications_impl_test.go diff --git a/types/responses.go b/responses.go similarity index 53% rename from types/responses.go rename to responses.go index e7487e5..05215a3 100644 --- a/types/responses.go +++ b/responses.go @@ -1,7 +1,7 @@ -package types +package gonextcloud -//Meta -type Meta struct { +//meta +type meta struct { Status string `json:"status"` Statuscode int `json:"statuscode"` Message string `json:"message"` @@ -9,109 +9,109 @@ type Meta struct { Itemsperpage string `json:"itemsperpage"` } -//ErrorResponse -type ErrorResponse struct { +//errorResponse +type errorResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data []interface{} `json:"data"` } `json:"ocs"` } -//UserListResponse -type UserListResponse struct { +//userListResponse +type userListResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Users []string `json:"users"` } `json:"data"` } `json:"ocs"` } -type UserListDetailsResponse struct { +type userListDetailsResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Users map[string]UserDetails `json:"users"` } `json:"data"` } `json:"ocs"` } -//UserResponse -type UserResponse struct { +//userResponse +type userResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data UserDetails `json:"data"` } `json:"ocs"` } -//BaseResponse -type BaseResponse struct { +//baseResponse +type baseResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data []string `json:"data"` } `json:"ocs"` } -//GroupListResponse -type GroupListResponse struct { +//groupListResponse +type groupListResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Groups []string `json:"groups"` } `json:"data"` } `json:"ocs"` } -//GroupListDetailsResponse -type GroupListDetailsResponse struct { +//groupListDetailsResponse +type groupListDetailsResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Groups []Group `json:"groups"` } `json:"data"` } `json:"ocs"` } -//AppListResponse -type AppListResponse struct { +//appListResponse +type appListResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Apps []string `json:"apps"` } `json:"data"` } `json:"ocs"` } -//AppResponse -type AppResponse struct { +//appResponse +type appResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data App `json:"data"` } `json:"ocs"` } -type AppConfigResponse struct { +type appConfigResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Data []string `json:"data"` } `json:"data"` } `json:"ocs"` } -type AppcConfigValueResponse struct { +type appcConfigValueResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Data string `json:"data"` } `json:"data"` } `json:"ocs"` } -//CapabilitiesResponse -type CapabilitiesResponse struct { +//capabilitiesResponse +type capabilitiesResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data struct { Version Version `json:"version"` Capabilities Capabilities `json:"capabilities"` @@ -127,58 +127,58 @@ type Version struct { Edition string `json:"edition"` } -type MonitoringResponse struct { +type monitoringResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data Monitoring `json:"data"` } `json:"ocs"` } -type SharesListResponse struct { +type sharesListResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data []Share `json:"data"` } `json:"ocs"` } -type SharesResponse struct { +type sharesResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data Share `json:"data"` } `json:"ocs"` } -type GroupFoldersListResponse struct { +type groupFoldersListResponse struct { Ocs struct { - Meta Meta `json:"meta"` - Data map[string]GroupFolderBadFormatIDAndGroups `json:"data"` + Meta meta `json:"meta"` + Data map[string]groupFolderBadFormatIDAndGroups `json:"data"` } `json:"ocs"` } -type GroupFoldersCreateResponse struct { +type groupFoldersCreateResponse struct { Ocs struct { - Meta Meta `json:"meta"` - Data GroupFolderBadFormatIDAndGroups `json:"data"` + Meta meta `json:"meta"` + Data groupFolderBadFormatIDAndGroups `json:"data"` } `json:"ocs"` } -type GroupFoldersResponse struct { +type groupFoldersResponse struct { Ocs struct { - Meta Meta `json:"meta"` - Data GroupFolderBadFormatGroups `json:"data"` + Meta meta `json:"meta"` + Data groupFolderBadFormatGroups `json:"data"` } `json:"ocs"` } -type NotificationsListResponse struct { +type notificationsListResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data []Notification `json:"data"` } `json:"ocs"` } -type NotificationResponse struct { +type notificationResponse struct { Ocs struct { - Meta Meta `json:"meta"` + Meta meta `json:"meta"` Data Notification `json:"data"` } `json:"ocs"` } diff --git a/routes.go b/routes.go index 84cc31b..2359869 100644 --- a/routes.go +++ b/routes.go @@ -2,8 +2,8 @@ package gonextcloud import "net/url" -// Routes references the available routes -type Routes struct { +// apiRoutes references the available routes +type apiRoutes struct { capabilities *url.URL users *url.URL groups *url.URL @@ -20,7 +20,7 @@ const badRequest = 998 var ( apiPath = &url.URL{Path: "/ocs/v2.php"} - routes = Routes{ + routes = apiRoutes{ capabilities: &url.URL{Path: apiPath.Path + "/cloud/capabilities"}, users: &url.URL{Path: apiPath.Path + "/cloud/users"}, groups: &url.URL{Path: apiPath.Path + "/cloud/groups"}, diff --git a/shares.go b/shares.go index c53d889..bdc149f 100644 --- a/shares.go +++ b/shares.go @@ -1,174 +1,53 @@ package gonextcloud -import ( - "fmt" - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" - "net/http" - "strconv" - "sync" +type ShareType int +type SharePermission int + +const ( + UserShare ShareType = 0 + GroupShare ShareType = 1 + PublicLinkShare ShareType = 3 + FederatedCloudShare ShareType = 6 + + ReadPermission SharePermission = 1 + UpdatePermission SharePermission = 2 + CreatePermission SharePermission = 4 + DeletePermission SharePermission = 8 + ReSharePermission SharePermission = 16 + AllPermissions SharePermission = 31 ) -//Shares contains all Shares available actions -type Shares struct { - c *Client +type ShareUpdate struct { + ShareID int + Permissions SharePermission + Password string + PublicUpload bool + ExpireDate string } -//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 { - return nil, err - } - var r types.SharesListResponse - res.JSON(&r) - return r.Ocs.Data, nil -} - -//GetFromPath return shares from a specific file or folder -func (s *Shares) GetFromPath(path string, reshares bool, subfiles bool) ([]types.Share, error) { - ro := &req.RequestOptions{ - Params: map[string]string{ - "path": path, - "reshares": strconv.FormatBool(reshares), - "subfiles": strconv.FormatBool(subfiles), - }, - } - res, err := s.c.baseRequest(http.MethodGet, routes.shares, ro) - if err != nil { - return nil, err - } - var r types.SharesListResponse - res.JSON(&r) - return r.Ocs.Data, nil -} - -//Get information about a known Share -func (s *Shares) Get(shareID string) (types.Share, error) { - res, err := s.c.baseRequest(http.MethodGet, routes.shares, nil, shareID) - if err != nil { - return types.Share{}, err - } - var r types.SharesListResponse - res.JSON(&r) - return r.Ocs.Data[0], nil -} - -//Create create a share -func (s *Shares) Create( - path string, - shareType types.ShareType, - permission types.SharePermission, - shareWith string, - publicUpload bool, - password string, -) (types.Share, error) { - - if (shareType == types.UserShare || shareType == types.GroupShare) && shareWith == "" { - return types.Share{}, fmt.Errorf("shareWith cannot be empty for ShareType UserShare or GroupShare") - } - ro := &req.RequestOptions{ - Data: map[string]string{ - "path": path, - "shareType": strconv.Itoa(int(shareType)), - "shareWith": shareWith, - "publicUpload": strconv.FormatBool(publicUpload), - "password": password, - "permissions": strconv.Itoa(int(permission)), - }, - } - res, err := s.c.baseRequest(http.MethodPost, routes.shares, ro) - if err != nil { - return types.Share{}, err - } - var r types.SharesResponse - res.JSON(&r) - return r.Ocs.Data, nil -} - -//Delete Remove the given share. -func (s *Shares) Delete(shareID int) error { - _, err := s.c.baseRequest(http.MethodDelete, routes.shares, nil, strconv.Itoa(shareID)) - return err -} - -// Update update share details -// expireDate expireDate expects a well formatted date string, e.g. ‘YYYY-MM-DD’ -func (s *Shares) Update(shareUpdate types.ShareUpdate) error { - errs := make(chan *types.UpdateError) - var wg sync.WaitGroup - wg.Add(4) - go func() { - defer wg.Done() - if err := s.UpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil { - errs <- &types.UpdateError{ - Field: "password", - Error: err, - } - } - }() - go func() { - defer wg.Done() - if err := s.UpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil { - errs <- &types.UpdateError{ - Field: "expireDate", - Error: err, - } - } - }() - go func() { - defer wg.Done() - if err := s.UpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil { - errs <- &types.UpdateError{ - Field: "permissions", - Error: err, - } - } - }() - go func() { - defer wg.Done() - if err := s.UpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil { - errs <- &types.UpdateError{ - Field: "publicUpload", - Error: err, - } - } - }() - go func() { - wg.Wait() - close(errs) - }() - if err := types.NewUpdateError(errs); err != nil { - return err - } - return nil -} - -//UpdateExpireDate updates the share's expire date -// expireDate expects a well formatted date string, e.g. ‘YYYY-MM-DD’ -func (s *Shares) UpdateExpireDate(shareID int, expireDate string) error { - return s.baseShareUpdate(strconv.Itoa(shareID), "expireDate", expireDate) -} - -//UpdatePublicUpload enable or disable public upload -func (s *Shares) UpdatePublicUpload(shareID int, public bool) error { - return s.baseShareUpdate(strconv.Itoa(shareID), "publicUpload", strconv.FormatBool(public)) -} - -//UpdatePassword updates share password -func (s *Shares) UpdatePassword(shareID int, password string) error { - return s.baseShareUpdate(strconv.Itoa(shareID), "password", password) -} - -//UpdatePermissions update permissions -func (s *Shares) UpdatePermissions(shareID int, permissions types.SharePermission) error { - return s.baseShareUpdate(strconv.Itoa(shareID), "permissions", strconv.Itoa(int(permissions))) -} - -func (s *Shares) baseShareUpdate(shareID string, key string, value string) error { - ro := &req.RequestOptions{ - Data: map[string]string{key: value}, - } - _, err := s.c.baseRequest(http.MethodPut, routes.shares, ro, shareID) - return err +type Share struct { + ID string `json:"id"` + ShareType int `json:"share_type"` + UIDOwner string `json:"uid_owner"` + DisplaynameOwner string `json:"displayname_owner"` + Permissions int `json:"permissions"` + Stime int `json:"stime"` + Parent interface{} `json:"parent"` + Expiration string `json:"expiration"` + Token string `json:"token"` + UIDFileOwner string `json:"uid_file_owner"` + DisplaynameFileOwner string `json:"displayname_file_owner"` + Path string `json:"path"` + ItemType string `json:"item_type"` + Mimetype string `json:"mimetype"` + StorageID string `json:"storage_id"` + Storage int `json:"storage"` + ItemSource int `json:"item_source"` + FileSource int `json:"file_source"` + FileParent int `json:"file_parent"` + FileTarget string `json:"file_target"` + ShareWith string `json:"share_with"` + ShareWithDisplayname string `json:"share_with_displayname"` + MailSend int `json:"mail_send"` + Tags []string `json:"tags"` } diff --git a/shares_impl.go b/shares_impl.go new file mode 100644 index 0000000..34184f0 --- /dev/null +++ b/shares_impl.go @@ -0,0 +1,174 @@ +package gonextcloud + +import ( + "fmt" + "net/http" + "strconv" + "sync" + + req "github.com/levigross/grequests" +) + +//shares contains all shares available actions +type shares struct { + c *client +} + +//List list all shares of the logged in user +func (s *shares) List() ([]Share, error) { + res, err := s.c.baseRequest(http.MethodGet, routes.shares, nil) + if err != nil { + return nil, err + } + var r sharesListResponse + res.JSON(&r) + return r.Ocs.Data, nil +} + +//GetFromPath return shares from a specific file or folder +func (s *shares) GetFromPath(path string, reshares bool, subfiles bool) ([]Share, error) { + ro := &req.RequestOptions{ + Params: map[string]string{ + "path": path, + "reshares": strconv.FormatBool(reshares), + "subfiles": strconv.FormatBool(subfiles), + }, + } + res, err := s.c.baseRequest(http.MethodGet, routes.shares, ro) + if err != nil { + return nil, err + } + var r sharesListResponse + res.JSON(&r) + return r.Ocs.Data, nil +} + +//Get information about a known Share +func (s *shares) Get(shareID string) (Share, error) { + res, err := s.c.baseRequest(http.MethodGet, routes.shares, nil, shareID) + if err != nil { + return Share{}, err + } + var r sharesListResponse + res.JSON(&r) + return r.Ocs.Data[0], nil +} + +//Create create a share +func (s *shares) Create( + path string, + shareType ShareType, + permission SharePermission, + shareWith string, + publicUpload bool, + password string, +) (Share, error) { + + if (shareType == UserShare || shareType == GroupShare) && shareWith == "" { + return Share{}, fmt.Errorf("shareWith cannot be empty for ShareType UserShare or GroupShare") + } + ro := &req.RequestOptions{ + Data: map[string]string{ + "path": path, + "shareType": strconv.Itoa(int(shareType)), + "shareWith": shareWith, + "publicUpload": strconv.FormatBool(publicUpload), + "password": password, + "permissions": strconv.Itoa(int(permission)), + }, + } + res, err := s.c.baseRequest(http.MethodPost, routes.shares, ro) + if err != nil { + return Share{}, err + } + var r sharesResponse + res.JSON(&r) + return r.Ocs.Data, nil +} + +//Delete Remove the given share. +func (s *shares) Delete(shareID int) error { + _, err := s.c.baseRequest(http.MethodDelete, routes.shares, nil, strconv.Itoa(shareID)) + return err +} + +// Update update share details +// expireDate expireDate expects a well formatted date string, e.g. ‘YYYY-MM-DD’ +func (s *shares) Update(shareUpdate ShareUpdate) error { + errs := make(chan *UpdateError) + var wg sync.WaitGroup + wg.Add(4) + go func() { + defer wg.Done() + if err := s.UpdatePassword(shareUpdate.ShareID, shareUpdate.Password); err != nil { + errs <- &UpdateError{ + Field: "password", + Error: err, + } + } + }() + go func() { + defer wg.Done() + if err := s.UpdateExpireDate(shareUpdate.ShareID, shareUpdate.ExpireDate); err != nil { + errs <- &UpdateError{ + Field: "expireDate", + Error: err, + } + } + }() + go func() { + defer wg.Done() + if err := s.UpdatePermissions(shareUpdate.ShareID, shareUpdate.Permissions); err != nil { + errs <- &UpdateError{ + Field: "permissions", + Error: err, + } + } + }() + go func() { + defer wg.Done() + if err := s.UpdatePublicUpload(shareUpdate.ShareID, shareUpdate.PublicUpload); err != nil { + errs <- &UpdateError{ + Field: "publicUpload", + Error: err, + } + } + }() + go func() { + wg.Wait() + close(errs) + }() + if err := newUpdateError(errs); err != nil { + return err + } + return nil +} + +//UpdateExpireDate updates the share's expire date +// expireDate expects a well formatted date string, e.g. ‘YYYY-MM-DD’ +func (s *shares) UpdateExpireDate(shareID int, expireDate string) error { + return s.baseShareUpdate(strconv.Itoa(shareID), "expireDate", expireDate) +} + +//UpdatePublicUpload enable or disable public upload +func (s *shares) UpdatePublicUpload(shareID int, public bool) error { + return s.baseShareUpdate(strconv.Itoa(shareID), "publicUpload", strconv.FormatBool(public)) +} + +//UpdatePassword updates share password +func (s *shares) UpdatePassword(shareID int, password string) error { + return s.baseShareUpdate(strconv.Itoa(shareID), "password", password) +} + +//UpdatePermissions update permissions +func (s *shares) UpdatePermissions(shareID int, permissions SharePermission) error { + return s.baseShareUpdate(strconv.Itoa(shareID), "permissions", strconv.Itoa(int(permissions))) +} + +func (s *shares) baseShareUpdate(shareID string, key string, value string) error { + ro := &req.RequestOptions{ + Data: map[string]string{key: value}, + } + _, err := s.c.baseRequest(http.MethodPut, routes.shares, ro, shareID) + return err +} diff --git a/types/groupfolders.go b/types/groupfolders.go deleted file mode 100644 index f43bab5..0000000 --- a/types/groupfolders.go +++ /dev/null @@ -1,57 +0,0 @@ -package types - -import "strconv" - -type GroupFolderBadFormatIDAndGroups struct { - ID string `json:"id"` - MountPoint string `json:"mount_point"` - Groups map[string]string `json:"groups"` - Quota string `json:"quota"` - Size int `json:"size"` -} - -type GroupFolderBadFormatGroups struct { - ID int `json:"id"` - MountPoint string `json:"mount_point"` - Groups map[string]string `json:"groups"` - Quota string `json:"quota"` - Size int `json:"size"` -} - -type GroupFolder struct { - ID int `json:"id"` - MountPoint string `json:"mount_point"` - Groups map[string]SharePermission `json:"groups"` - Quota int `json:"quota"` - Size int `json:"size"` -} - -func (gf *GroupFolderBadFormatGroups) FormatGroupFolder() GroupFolder { - g := GroupFolder{} - g.ID = gf.ID - g.MountPoint = gf.MountPoint - g.Groups = map[string]SharePermission{} - for k, v := range gf.Groups { - p, _ := strconv.Atoi(v) - g.Groups[k] = SharePermission(p) - } - q, _ := strconv.Atoi(gf.Quota) - g.Quota = q - g.Size = gf.Size - return g -} - -func (gf *GroupFolderBadFormatIDAndGroups) FormatGroupFolder() GroupFolder { - g := GroupFolder{} - g.ID, _ = strconv.Atoi(gf.ID) - g.MountPoint = gf.MountPoint - g.Groups = map[string]SharePermission{} - for k, v := range gf.Groups { - p, _ := strconv.Atoi(v) - g.Groups[k] = SharePermission(p) - } - q, _ := strconv.Atoi(gf.Quota) - g.Quota = q - g.Size = gf.Size - return g -} diff --git a/types/monitoring.go b/types/monitoring.go deleted file mode 100644 index 51cf346..0000000 --- a/types/monitoring.go +++ /dev/null @@ -1,65 +0,0 @@ -package types - -type System struct { - Version string `json:"version"` - Theme string `json:"theme"` - EnableAvatars string `json:"enable_avatars"` - EnablePreviews string `json:"enable_previews"` - MemcacheLocal string `json:"memcache.local"` - MemcacheDistributed string `json:"memcache.distributed"` - FilelockingEnabled string `json:"filelocking.enabled"` - MemcacheLocking string `json:"memcache.locking"` - Debug string `json:"debug"` - Freespace int64 `json:"freespace"` - Cpuload []float32 `json:"cpuload"` - MemTotal int `json:"mem_total"` - MemFree int `json:"mem_free"` - SwapTotal int `json:"swap_total"` - SwapFree int `json:"swap_free"` -} - -type Monitoring struct { - Nextcloud struct { - System System `json:"system"` - Storage Storage `json:"storage"` - Shares struct { - NumShares int `json:"num_shares"` - NumSharesUser int `json:"num_shares_user"` - NumSharesGroups int `json:"num_shares_groups"` - NumSharesLink int `json:"num_shares_link"` - NumSharesLinkNoPassword int `json:"num_shares_link_no_password"` - NumFedSharesSent int `json:"num_fed_shares_sent"` - NumFedSharesReceived int `json:"num_fed_shares_received"` - } `json:"shares"` - } `json:"nextcloud"` - Server struct { - Webserver string `json:"webserver"` - Php struct { - Version string `json:"version"` - MemoryLimit int `json:"memory_limit"` - MaxExecutionTime int `json:"max_execution_time"` - UploadMaxFilesize int `json:"upload_max_filesize"` - } `json:"php"` - Database struct { - Type string `json:"type"` - Version string `json:"version"` - Size int `json:"size"` - } `json:"database"` - } `json:"server"` - ActiveUsers ActiveUsers `json:"activeUsers"` -} - -type ActiveUsers struct { - Last5Minutes int `json:"last5minutes"` - Last1Hour int `json:"last1hour"` - Last24Hours int `json:"last24hours"` -} - -type Storage struct { - NumUsers int `json:"num_users"` - NumFiles int `json:"num_files"` - NumStorages int `json:"num_storages"` - NumStoragesLocal int `json:"num_storages_local"` - NumStoragesHome int `json:"num_storages_home"` - NumStoragesOther int `json:"num_storages_other"` -} diff --git a/types/shares.go b/types/shares.go deleted file mode 100644 index 7073e94..0000000 --- a/types/shares.go +++ /dev/null @@ -1,53 +0,0 @@ -package types - -type ShareType int -type SharePermission int - -const ( - UserShare ShareType = 0 - GroupShare ShareType = 1 - PublicLinkShare ShareType = 3 - FederatedCloudShare ShareType = 6 - - ReadPermission SharePermission = 1 - UpdatePermission SharePermission = 2 - CreatePermission SharePermission = 4 - DeletePermission SharePermission = 8 - ReSharePermission SharePermission = 16 - AllPermissions SharePermission = 31 -) - -type ShareUpdate struct { - ShareID int - Permissions SharePermission - Password string - PublicUpload bool - ExpireDate string -} - -type Share struct { - ID string `json:"id"` - ShareType int `json:"share_type"` - UIDOwner string `json:"uid_owner"` - DisplaynameOwner string `json:"displayname_owner"` - Permissions int `json:"permissions"` - Stime int `json:"stime"` - Parent interface{} `json:"parent"` - Expiration string `json:"expiration"` - Token string `json:"token"` - UIDFileOwner string `json:"uid_file_owner"` - DisplaynameFileOwner string `json:"displayname_file_owner"` - Path string `json:"path"` - ItemType string `json:"item_type"` - Mimetype string `json:"mimetype"` - StorageID string `json:"storage_id"` - Storage int `json:"storage"` - ItemSource int `json:"item_source"` - FileSource int `json:"file_source"` - FileParent int `json:"file_parent"` - FileTarget string `json:"file_target"` - ShareWith string `json:"share_with"` - ShareWithDisplayname string `json:"share_with_displayname"` - MailSend int `json:"mail_send"` - Tags []string `json:"tags"` -} diff --git a/types/webdav.go b/types/webdav.go deleted file mode 100644 index 4f17573..0000000 --- a/types/webdav.go +++ /dev/null @@ -1,39 +0,0 @@ -package types - -import ( - "io" - "os" - "path/filepath" -) - -// WebDav available methods -type WebDav interface { - // ReadDir reads the contents of a remote directory - ReadDir(path string) ([]os.FileInfo, error) - // Stat returns the file stats for a specified path - Stat(path string) (os.FileInfo, error) - // Remove removes a remote file - Remove(path string) error - // RemoveAll removes remote files - RemoveAll(path string) error - // Mkdir makes a directory - Mkdir(path string, _ os.FileMode) error - // MkdirAll like mkdir -p, but for webdav - MkdirAll(path string, _ os.FileMode) error - // Rename moves a file from A to B - Rename(oldpath, newpath string, overwrite bool) error - // Copy copies a file from A to B - Copy(oldpath, newpath string, overwrite bool) error - // Read reads the contents of a remote file - Read(path string) ([]byte, error) - // ReadStream reads the stream for a given path - ReadStream(path string) (io.ReadCloser, error) - // Write writes data to a given path - Write(path string, data []byte, _ os.FileMode) error - // WriteStream writes a stream - WriteStream(path string, stream io.Reader, _ os.FileMode) error - - // Walk walks the file tree rooted at root, calling walkFn for each file or - // directory in the tree, including root. - Walk(path string, walkFunc filepath.WalkFunc) error -} diff --git a/update_test.go b/update_test.go index b6ac6da..feef13a 100644 --- a/update_test.go +++ b/update_test.go @@ -2,12 +2,12 @@ package gonextcloud import ( "fmt" - "github.com/fatih/structs" - "github.com/stretchr/testify/assert" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "strings" "testing" "time" + + "github.com/fatih/structs" + "github.com/stretchr/testify/assert" ) func TestUserUpdate(t *testing.T) { @@ -23,11 +23,11 @@ func TestUserUpdate(t *testing.T) { if err != nil { t.FailNow() } - user := &types.UserDetails{ + user := &UserDetails{ ID: username, Displayname: strings.ToUpper(username), Email: "some@mail.com", - Quota: types.Quota{ + Quota: Quota{ // Unlimited Quota: -3, }, diff --git a/types/user.go b/user.go similarity index 98% rename from types/user.go rename to user.go index 7e8ebfa..954e3df 100644 --- a/types/user.go +++ b/user.go @@ -1,4 +1,4 @@ -package types +package gonextcloud import "strconv" diff --git a/users.go b/users_impl.go similarity index 76% rename from users.go rename to users_impl.go index 8eca631..87531fe 100644 --- a/users.go +++ b/users_impl.go @@ -2,56 +2,56 @@ package gonextcloud import ( "encoding/json" - req "github.com/levigross/grequests" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "net/http" "net/url" "path" "strings" "sync" + + req "github.com/levigross/grequests" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) -//Users contains all Users available actions -type Users struct { - c *Client +//users contains all users available actions +type users struct { + c *client } // List return the Nextcloud'user list -func (u *Users) List() ([]string, error) { +func (u *users) List() ([]string, error) { res, err := u.c.baseRequest(http.MethodGet, routes.users, nil) //res, err := c.session.Get(u.String(), nil) if err != nil { return nil, err } - var r types.UserListResponse + var r userListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } //ListDetails return a map of user with details -func (u *Users) ListDetails() (map[string]types.UserDetails, error) { +func (u *users) ListDetails() (map[string]UserDetails, error) { res, err := u.c.baseRequest(http.MethodGet, routes.users, nil, "details") //res, err := c.session.Get(u.String(), nil) if err != nil { return nil, err } - var r types.UserListDetailsResponse + var r userListDetailsResponse res.JSON(&r) return r.Ocs.Data.Users, nil } // Get return the details about the specified user -func (u *Users) Get(name string) (*types.UserDetails, error) { +func (u *users) Get(name string) (*UserDetails, error) { if name == "" { - return nil, &types.APIError{Message: "name cannot be empty"} + return nil, &APIError{Message: "name cannot be empty"} } res, err := u.c.baseRequest(http.MethodGet, routes.users, nil, name) if err != nil { return nil, err } - var r types.UserResponse + var r userResponse js := res.String() // Nextcloud does not encode JSON properly js = reformatJSON(js) @@ -62,7 +62,7 @@ func (u *Users) Get(name string) (*types.UserDetails, error) { } // Search returns the users whose name match the search string -func (u *Users) Search(search string) ([]string, error) { +func (u *users) Search(search string) ([]string, error) { ro := &req.RequestOptions{ Params: map[string]string{"search": search}, } @@ -70,14 +70,14 @@ func (u *Users) Search(search string) ([]string, error) { if err != nil { return nil, err } - var r types.UserListResponse + var r userListResponse res.JSON(&r) return r.Ocs.Data.Users, nil } // Create create a new user -func (u *Users) Create(username string, password string, user *types.UserDetails) error { - // Create base Users +func (u *users) Create(username string, password string, user *UserDetails) error { + // Create base users ro := &req.RequestOptions{ Data: map[string]string{ "userid": username, @@ -97,7 +97,7 @@ func (u *Users) Create(username string, password string, user *types.UserDetails // CreateWithoutPassword create a user without provisioning a password, the email address must be provided to send // an init password email -func (u *Users) CreateWithoutPassword(username, email, displayName, quota, language string, groups ...string) error { +func (u *users) CreateWithoutPassword(username, email, displayName, quota, language string, groups ...string) error { if u.c.version.Major < 14 { return errors.New("unsupported method: requires Nextcloud 14+") } @@ -132,12 +132,12 @@ func (u *Users) CreateWithoutPassword(username, email, displayName, quota, langu } //CreateBatchWithoutPassword create multiple users and send them the init password email -func (u *Users) CreateBatchWithoutPassword(users []types.User) error { +func (u *users) CreateBatchWithoutPassword(users []User) error { var wg sync.WaitGroup errs := make(chan error) for _, us := range users { wg.Add(1) - go func(user types.User) { + go func(user User) { logrus.Debugf("creating user %s", user.Username) defer wg.Done() if err := u.CreateWithoutPassword( @@ -162,12 +162,12 @@ func (u *Users) CreateBatchWithoutPassword(users []types.User) error { } //Delete delete the user -func (u *Users) Delete(name string) error { +func (u *users) Delete(name string) error { return u.baseRequest(http.MethodDelete, nil, name) } //Enable enables the user -func (u *Users) Enable(name string) error { +func (u *users) Enable(name string) error { ro := &req.RequestOptions{ Data: map[string]string{}, } @@ -175,7 +175,7 @@ func (u *Users) Enable(name string) error { } //Disable disables the user -func (u *Users) Disable(name string) error { +func (u *users) Disable(name string) error { ro := &req.RequestOptions{ Data: map[string]string{}, } @@ -183,25 +183,25 @@ func (u *Users) Disable(name string) error { } //SendWelcomeEmail (re)send the welcome mail to the user (return an error if the user has not configured his email) -func (u *Users) SendWelcomeEmail(name string) error { +func (u *users) SendWelcomeEmail(name string) error { return u.baseRequest(http.MethodPost, nil, name, "welcome") } -//Update takes a *types.Users struct to update the user's information -// Updatable fields: Email, Displayname, Phone, Address, Website, Twitter, Quota, Groups -func (u *Users) Update(user *types.UserDetails) error { +//Update takes a *types.users struct to update the user's information +// Updatable fields: Email, Displayname, Phone, Address, Website, Twitter, Quota, groups +func (u *users) Update(user *UserDetails) error { // Get user to update only modified fields original, err := u.Get(user.ID) if err != nil { return err } - errs := make(chan *types.UpdateError) + errs := make(chan *UpdateError) var wg sync.WaitGroup update := func(key string, value string) { defer wg.Done() if err := u.updateAttribute(user.ID, strings.ToLower(key), value); err != nil { - errs <- &types.UpdateError{ + errs <- &UpdateError{ Field: key, Error: err, } @@ -242,7 +242,7 @@ func (u *Users) Update(user *types.UserDetails) error { if user.Quota.Quota != original.Quota.Quota { var value string // If empty - if user.Quota == (types.Quota{}) { + if user.Quota == (Quota{}) { value = "default" } else { value = user.Quota.String() @@ -250,7 +250,7 @@ func (u *Users) Update(user *types.UserDetails) error { wg.Add(1) go update("Quota", value) } - // Groups + // groups // Group removed for _, g := range original.Groups { if !contains(user.Groups, g) { @@ -258,8 +258,8 @@ func (u *Users) Update(user *types.UserDetails) error { go func(gr string) { defer wg.Done() if err := u.GroupRemove(user.ID, gr); err != nil { - errs <- &types.UpdateError{ - Field: "Groups/" + gr, + errs <- &UpdateError{ + Field: "groups/" + gr, Error: err, } } @@ -275,8 +275,8 @@ func (u *Users) Update(user *types.UserDetails) error { go func(gr string) { defer wg.Done() if err := u.GroupAdd(user.ID, gr); err != nil { - errs <- &types.UpdateError{ - Field: "Groups/" + gr, + errs <- &UpdateError{ + Field: "groups/" + gr, Error: err, } } @@ -290,66 +290,66 @@ func (u *Users) Update(user *types.UserDetails) error { close(errs) }() // Warning : we actually need to check the *err - if err := types.NewUpdateError(errs); err != nil { + if err := newUpdateError(errs); err != nil { return err } return nil } //UpdateEmail update the user's email -func (u *Users) UpdateEmail(name string, email string) error { +func (u *users) UpdateEmail(name string, email string) error { return u.updateAttribute(name, "email", email) } //UpdateDisplayName update the user's display name -func (u *Users) UpdateDisplayName(name string, displayName string) error { +func (u *users) UpdateDisplayName(name string, displayName string) error { return u.updateAttribute(name, "displayname", displayName) } //UpdatePhone update the user's phone -func (u *Users) UpdatePhone(name string, phone string) error { +func (u *users) UpdatePhone(name string, phone string) error { return u.updateAttribute(name, "phone", phone) } //UpdateAddress update the user's address -func (u *Users) UpdateAddress(name string, address string) error { +func (u *users) UpdateAddress(name string, address string) error { return u.updateAttribute(name, "address", address) } //UpdateWebSite update the user's website -func (u *Users) UpdateWebSite(name string, website string) error { +func (u *users) UpdateWebSite(name string, website string) error { return u.updateAttribute(name, "website", website) } //UpdateTwitter update the user's twitter -func (u *Users) UpdateTwitter(name string, twitter string) error { +func (u *users) UpdateTwitter(name string, twitter string) error { return u.updateAttribute(name, "twitter", twitter) } //UpdatePassword update the user's password -func (u *Users) UpdatePassword(name string, password string) error { +func (u *users) UpdatePassword(name string, password string) error { return u.updateAttribute(name, "password", password) } //UpdateQuota update the user's quota (bytes). Set negative quota for unlimited -func (u *Users) UpdateQuota(name string, quota int64) error { - q := types.Quota{Quota: quota} +func (u *users) UpdateQuota(name string, quota int64) error { + q := Quota{Quota: quota} return u.updateAttribute(name, "quota", q.String()) } //GroupList lists the user's groups -func (u *Users) GroupList(name string) ([]string, error) { +func (u *users) GroupList(name string) ([]string, error) { res, err := u.c.baseRequest(http.MethodGet, routes.users, nil, name, "groups") if err != nil { return nil, err } - var r types.GroupListResponse + var r groupListResponse res.JSON(&r) return r.Ocs.Data.Groups, nil } //GroupAdd adds a the user to the group -func (u *Users) GroupAdd(name string, group string) error { +func (u *users) GroupAdd(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, @@ -359,7 +359,7 @@ func (u *Users) GroupAdd(name string, group string) error { } //GroupRemove removes the user from the group -func (u *Users) GroupRemove(name string, group string) error { +func (u *users) GroupRemove(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, @@ -369,7 +369,7 @@ func (u *Users) GroupRemove(name string, group string) error { } //GroupPromote promotes the user as group admin -func (u *Users) GroupPromote(name string, group string) error { +func (u *users) GroupPromote(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, @@ -379,7 +379,7 @@ func (u *Users) GroupPromote(name string, group string) error { } //GroupDemote demotes the user -func (u *Users) GroupDemote(name string, group string) error { +func (u *users) GroupDemote(name string, group string) error { ro := &req.RequestOptions{ Data: map[string]string{ "groupid": group, @@ -389,7 +389,7 @@ func (u *Users) GroupDemote(name string, group string) error { } //GroupSubAdminList lists the groups where he is subadmin -func (u *Users) GroupSubAdminList(name string) ([]string, error) { +func (u *users) GroupSubAdminList(name string) ([]string, error) { if !u.c.loggedIn() { return nil, errUnauthorized } @@ -399,12 +399,12 @@ func (u *Users) GroupSubAdminList(name string) ([]string, error) { if err != nil { return nil, err } - var r types.BaseResponse + var r baseResponse res.JSON(&r) return r.Ocs.Data, nil } -func (u *Users) updateAttribute(name string, key string, value string) error { +func (u *users) updateAttribute(name string, key string, value string) error { ro := &req.RequestOptions{ Data: map[string]string{ "key": key, @@ -414,13 +414,13 @@ func (u *Users) updateAttribute(name string, key string, value string) error { return u.baseRequest(http.MethodPut, ro, name) } -func (u *Users) baseRequest(method string, ro *req.RequestOptions, subRoutes ...string) error { +func (u *users) baseRequest(method string, ro *req.RequestOptions, subRoutes ...string) error { _, err := u.c.baseRequest(method, routes.users, ro, subRoutes...) return err } func ignoredUserField(key string) bool { - keys := []string{"Email", "Displayname", "Phone", "Address", "Website", "Twitter", "Quota", "Groups"} + keys := []string{"Email", "Displayname", "Phone", "Address", "Website", "Twitter", "Quota", "groups"} for _, k := range keys { if key == k { return false diff --git a/utils.go b/utils.go index 7363e1a..dae14f9 100644 --- a/utils.go +++ b/utils.go @@ -2,15 +2,15 @@ package gonextcloud import ( "encoding/json" - req "github.com/levigross/grequests" - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" "net/http" "net/url" "path" "strings" + + req "github.com/levigross/grequests" ) -func (c *Client) baseRequest(method string, route *url.URL, ro *req.RequestOptions, subRoutes ...string) (*req.Response, error) { +func (c *client) baseRequest(method string, route *url.URL, ro *req.RequestOptions, subRoutes ...string) (*req.Response, error) { if !c.loggedIn() { return nil, errUnauthorized } @@ -40,12 +40,12 @@ func (c *Client) baseRequest(method string, route *url.URL, ro *req.RequestOptio } // As we cannot read the ReaderCloser twice, we use the string content js := res.String() - var r types.BaseResponse + var r baseResponse json.Unmarshal([]byte(js), &r) if r.Ocs.Meta.Statuscode == 200 || r.Ocs.Meta.Statuscode == 100 { return res, nil } - err = types.ErrorFromMeta(r.Ocs.Meta) + err = errorFromMeta(r.Ocs.Meta) return nil, err } diff --git a/webdav.go b/webdav_impl.go similarity index 100% rename from webdav.go rename to webdav_impl.go diff --git a/webdav_test.go b/webdav_impl_test.go similarity index 94% rename from webdav_test.go rename to webdav_impl_test.go index cbdc889..753200d 100644 --- a/webdav_test.go +++ b/webdav_impl_test.go @@ -9,14 +9,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types" ) var ( dir string - wd types.WebDav - folders = []string{ + wd WebDav + folders = []string{ "folder1", "folder1/sub1", "folder1/sub1/ssub1",