Added Apps support, added full UserUpdate and create

This commit is contained in:
Philippe-Adrien Nousse 2018-07-25 15:06:31 +02:00
parent 40718cb98c
commit dae9553da7
18 changed files with 1127 additions and 161 deletions

78
apps.go Normal file
View File

@ -0,0 +1,78 @@
package gonextcloud
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http"
)
func (c *Client) AppList() ([]string, error) {
res, err := c.baseRequest(routes.apps, "", "", nil, http.MethodGet)
if err != nil {
return nil, err
}
var r types.AppListResponse
res.JSON(&r)
return r.Ocs.Data.Apps, nil
}
func (c *Client) AppListEnabled() ([]string, error) {
ro := &req.RequestOptions{
Params: map[string]string{"filter": "enabled"},
}
res, err := c.baseRequest(routes.apps, "", "", ro, http.MethodGet)
if err != nil {
return nil, err
}
var r types.AppListResponse
res.JSON(&r)
return r.Ocs.Data.Apps, nil
}
func (c *Client) AppListDisabled() ([]string, error) {
ro := &req.RequestOptions{
Params: map[string]string{"filter": "disabled"},
}
res, err := c.baseRequest(routes.apps, "", "", ro, http.MethodGet)
if err != nil {
return nil, err
}
var r types.AppListResponse
res.JSON(&r)
return r.Ocs.Data.Apps, nil
}
func (c *Client) AppInfos(name string) (types.App, error) {
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodGet)
if err != nil {
return types.App{}, err
}
var r types.AppResponse
res.JSON(&r)
return r.Ocs.Data, nil
}
func (c *Client) AppEnable(name string) error {
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodPut)
if err != nil {
return err
}
var r types.BaseResponse
res.JSON(&r)
return nil
}
func (c *Client) AppDisable(name string) error {
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodDelete)
if err != nil {
return err
}
var r types.BaseResponse
res.JSON(&r)
return nil
}
func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error {
_, err := c.baseRequest(routes.apps, name, route, ro, method)
return err
}

View File

@ -1,9 +1,9 @@
package client package gonextcloud
import ( import (
"fmt" "fmt"
req "github.com/levigross/grequests" req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types" "github.com/partitio/gonextcloud/types"
) )
var unauthorized = fmt.Errorf("login first") var unauthorized = fmt.Errorf("login first")
@ -24,10 +24,7 @@ func (c *Client) Login(username string, password string) error {
} }
var r types.CapabilitiesResponse var r types.CapabilitiesResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 { // No need to check for Ocs.Meta.StatusCode as capabilities are always returned
e := types.ErrorFromMeta(r.Ocs.Meta)
return &e
}
c.capabilities = &r.Ocs.Data.Capabilities c.capabilities = &r.Ocs.Data.Capabilities
// Check if authentication failed // Check if authentication failed
if !c.loggedIn() { if !c.loggedIn() {
@ -39,10 +36,16 @@ func (c *Client) Login(username string, password string) error {
func (c *Client) Logout() error { func (c *Client) Logout() error {
c.session.CloseIdleConnections() c.session.CloseIdleConnections()
c.session.HTTPClient.Jar = nil
// Clear capabilities as it is used to check for valid authentication
c.capabilities = nil
return nil return nil
} }
func (c *Client) loggedIn() bool { func (c *Client) loggedIn() bool {
// When authentication failed, capabilities doesn't contains core information // When authentication failed, capabilities doesn't contains core information
if c.capabilities == nil {
return false
}
return c.capabilities.Core.WebdavRoot != "" return c.capabilities.Core.WebdavRoot != ""
} }

View File

@ -38,11 +38,11 @@ For example, to list all the Nextcloud's instance users:
} }
*/ */
package client package gonextcloud
import ( import (
req "github.com/levigross/grequests" req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types" "github.com/partitio/gonextcloud/types"
"net/url" "net/url"
) )

View File

@ -1,35 +0,0 @@
package client
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types"
)
func (c *Client) AppList() ([]string, error) {
if !c.loggedIn() {
return nil, unauthorized
}
u := c.baseURL.ResolveReference(routes.apps)
res, err := c.session.Get(u.String(), nil)
if err != nil {
return nil, err
}
var r types.AppListResponse
res.JSON(&r)
return r.Ocs.Data.Apps, nil
return nil, nil
}
func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error {
res, err := c.baseRequest(routes.apps, name, route, ro, method)
if err != nil {
return err
}
var r types.UserResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return &e
}
return nil
}

View File

@ -1,19 +0,0 @@
package types
import "fmt"
type APIError struct {
Code int
Message string
}
func ErrorFromMeta(meta Meta) APIError {
return APIError{
meta.Statuscode,
meta.Message,
}
}
func (e *APIError) Error() string {
return fmt.Sprintf("%d : %s", e.Code, e.Message)
}

125
cover/gonextcloud.cov Normal file
View File

@ -0,0 +1,125 @@
mode: count
github.com/partitio/gonextcloud/utils.go:13.138,14.19 1 21
github.com/partitio/gonextcloud/utils.go:17.2,18.16 2 20
github.com/partitio/gonextcloud/utils.go:21.2,21.20 1 20
github.com/partitio/gonextcloud/utils.go:24.2,28.30 2 20
github.com/partitio/gonextcloud/utils.go:37.2,37.16 1 20
github.com/partitio/gonextcloud/utils.go:41.2,44.34 4 19
github.com/partitio/gonextcloud/utils.go:48.2,48.17 1 18
github.com/partitio/gonextcloud/utils.go:14.19,16.3 1 1
github.com/partitio/gonextcloud/utils.go:18.16,20.3 1 16
github.com/partitio/gonextcloud/utils.go:21.20,23.3 1 6
github.com/partitio/gonextcloud/utils.go:28.30,30.3 1 2
github.com/partitio/gonextcloud/utils.go:30.8,30.38 1 18
github.com/partitio/gonextcloud/utils.go:30.38,32.3 1 5
github.com/partitio/gonextcloud/utils.go:32.8,32.37 1 13
github.com/partitio/gonextcloud/utils.go:32.37,34.3 1 10
github.com/partitio/gonextcloud/utils.go:34.8,34.40 1 3
github.com/partitio/gonextcloud/utils.go:34.40,36.3 1 3
github.com/partitio/gonextcloud/utils.go:37.16,39.3 1 1
github.com/partitio/gonextcloud/utils.go:44.34,47.3 2 1
github.com/partitio/gonextcloud/utils.go:51.39,58.2 4 10
github.com/partitio/gonextcloud/apps.go:8.46,9.19 1 0
github.com/partitio/gonextcloud/apps.go:12.2,14.16 3 0
github.com/partitio/gonextcloud/apps.go:17.2,20.17 4 0
github.com/partitio/gonextcloud/apps.go:9.19,11.3 1 0
github.com/partitio/gonextcloud/apps.go:14.16,16.3 1 0
github.com/partitio/gonextcloud/apps.go:23.106,25.16 2 0
github.com/partitio/gonextcloud/apps.go:28.2,30.34 3 0
github.com/partitio/gonextcloud/apps.go:34.2,34.12 1 0
github.com/partitio/gonextcloud/apps.go:25.16,27.3 1 0
github.com/partitio/gonextcloud/apps.go:30.34,33.3 2 0
github.com/partitio/gonextcloud/auth.go:11.64,22.16 7 3
github.com/partitio/gonextcloud/auth.go:25.2,30.19 4 2
github.com/partitio/gonextcloud/auth.go:34.2,34.12 1 1
github.com/partitio/gonextcloud/auth.go:22.16,24.3 1 1
github.com/partitio/gonextcloud/auth.go:30.19,33.3 2 1
github.com/partitio/gonextcloud/auth.go:37.33,43.2 4 1
github.com/partitio/gonextcloud/auth.go:45.34,47.27 1 39
github.com/partitio/gonextcloud/auth.go:50.2,50.45 1 38
github.com/partitio/gonextcloud/auth.go:47.27,49.3 1 1
github.com/partitio/gonextcloud/client.go:58.50,60.16 2 3
github.com/partitio/gonextcloud/client.go:67.2,74.16 2 3
github.com/partitio/gonextcloud/client.go:60.16,62.17 2 2
github.com/partitio/gonextcloud/client.go:62.17,64.4 1 0
github.com/partitio/gonextcloud/groups.go:9.48,11.16 2 1
github.com/partitio/gonextcloud/groups.go:14.2,16.31 3 1
github.com/partitio/gonextcloud/groups.go:11.16,13.3 1 0
github.com/partitio/gonextcloud/groups.go:19.60,21.16 2 0
github.com/partitio/gonextcloud/groups.go:24.2,26.30 3 0
github.com/partitio/gonextcloud/groups.go:21.16,23.3 1 0
github.com/partitio/gonextcloud/groups.go:29.63,34.16 3 0
github.com/partitio/gonextcloud/groups.go:37.2,39.31 3 0
github.com/partitio/gonextcloud/groups.go:34.16,36.3 1 0
github.com/partitio/gonextcloud/groups.go:42.49,48.72 2 1
github.com/partitio/gonextcloud/groups.go:51.2,51.12 1 1
github.com/partitio/gonextcloud/groups.go:48.72,50.3 1 0
github.com/partitio/gonextcloud/groups.go:54.49,55.77 1 1
github.com/partitio/gonextcloud/groups.go:58.2,58.12 1 1
github.com/partitio/gonextcloud/groups.go:55.77,57.3 1 0
github.com/partitio/gonextcloud/groups.go:61.67,63.16 2 0
github.com/partitio/gonextcloud/groups.go:66.2,68.30 3 0
github.com/partitio/gonextcloud/groups.go:63.16,65.3 1 0
github.com/partitio/gonextcloud/groups.go:71.107,73.16 2 2
github.com/partitio/gonextcloud/groups.go:76.2,78.12 3 2
github.com/partitio/gonextcloud/groups.go:73.16,75.3 1 0
github.com/partitio/gonextcloud/users.go:12.47,13.19 1 1
github.com/partitio/gonextcloud/users.go:16.2,18.16 3 1
github.com/partitio/gonextcloud/users.go:21.2,23.30 3 1
github.com/partitio/gonextcloud/users.go:13.19,15.3 1 0
github.com/partitio/gonextcloud/users.go:18.16,20.3 1 0
github.com/partitio/gonextcloud/users.go:26.57,27.16 1 11
github.com/partitio/gonextcloud/users.go:30.2,30.19 1 10
github.com/partitio/gonextcloud/users.go:33.2,36.16 4 10
github.com/partitio/gonextcloud/users.go:39.2,43.55 4 10
github.com/partitio/gonextcloud/users.go:46.2,46.34 1 9
github.com/partitio/gonextcloud/users.go:50.2,50.25 1 9
github.com/partitio/gonextcloud/users.go:27.16,29.3 1 1
github.com/partitio/gonextcloud/users.go:30.19,32.3 1 0
github.com/partitio/gonextcloud/users.go:36.16,38.3 1 0
github.com/partitio/gonextcloud/users.go:43.55,45.3 1 1
github.com/partitio/gonextcloud/users.go:46.34,49.3 2 0
github.com/partitio/gonextcloud/users.go:53.62,54.19 1 1
github.com/partitio/gonextcloud/users.go:57.2,62.16 4 1
github.com/partitio/gonextcloud/users.go:65.2,67.34 3 1
github.com/partitio/gonextcloud/users.go:71.2,71.30 1 1
github.com/partitio/gonextcloud/users.go:54.19,56.3 1 0
github.com/partitio/gonextcloud/users.go:62.16,64.3 1 0
github.com/partitio/gonextcloud/users.go:67.34,70.3 2 0
github.com/partitio/gonextcloud/users.go:74.69,82.2 2 2
github.com/partitio/gonextcloud/users.go:84.48,86.2 1 1
github.com/partitio/gonextcloud/users.go:88.48,93.2 2 1
github.com/partitio/gonextcloud/users.go:95.49,100.2 2 1
github.com/partitio/gonextcloud/users.go:102.58,104.2 1 0
github.com/partitio/gonextcloud/users.go:106.67,108.2 1 1
github.com/partitio/gonextcloud/users.go:110.79,112.2 1 1
github.com/partitio/gonextcloud/users.go:114.67,116.2 1 1
github.com/partitio/gonextcloud/users.go:118.71,120.2 1 1
github.com/partitio/gonextcloud/users.go:122.71,124.2 1 1
github.com/partitio/gonextcloud/users.go:126.71,128.2 1 1
github.com/partitio/gonextcloud/users.go:130.73,132.2 1 1
github.com/partitio/gonextcloud/users.go:134.64,136.2 1 1
github.com/partitio/gonextcloud/users.go:138.63,139.19 1 1
github.com/partitio/gonextcloud/users.go:142.2,145.16 4 1
github.com/partitio/gonextcloud/users.go:148.2,150.34 3 1
github.com/partitio/gonextcloud/users.go:154.2,154.31 1 1
github.com/partitio/gonextcloud/users.go:139.19,141.3 1 0
github.com/partitio/gonextcloud/users.go:145.16,147.3 1 0
github.com/partitio/gonextcloud/users.go:150.34,153.3 2 0
github.com/partitio/gonextcloud/users.go:157.64,164.2 2 1
github.com/partitio/gonextcloud/users.go:166.67,173.2 2 0
github.com/partitio/gonextcloud/users.go:175.68,182.2 2 1
github.com/partitio/gonextcloud/users.go:184.67,191.2 2 1
github.com/partitio/gonextcloud/users.go:193.71,194.19 1 2
github.com/partitio/gonextcloud/users.go:197.2,200.16 4 2
github.com/partitio/gonextcloud/users.go:203.2,205.34 3 2
github.com/partitio/gonextcloud/users.go:209.2,209.24 1 1
github.com/partitio/gonextcloud/users.go:194.19,196.3 1 0
github.com/partitio/gonextcloud/users.go:200.16,202.3 1 0
github.com/partitio/gonextcloud/users.go:205.34,208.3 2 1
github.com/partitio/gonextcloud/users.go:212.83,220.2 2 8
github.com/partitio/gonextcloud/users.go:222.106,224.16 2 16
github.com/partitio/gonextcloud/users.go:227.2,229.34 3 15
github.com/partitio/gonextcloud/users.go:233.2,233.12 1 15
github.com/partitio/gonextcloud/users.go:224.16,226.3 1 1
github.com/partitio/gonextcloud/users.go:229.34,232.3 2 0

663
coverage.html Normal file
View File

@ -0,0 +1,663 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/partitio/gonextcloud/apps.go (0.0%)</option>
<option value="file1">github.com/partitio/gonextcloud/auth.go (100.0%)</option>
<option value="file2">github.com/partitio/gonextcloud/client.go (85.7%)</option>
<option value="file3">github.com/partitio/gonextcloud/groups.go (39.5%)</option>
<option value="file4">github.com/partitio/gonextcloud/users.go (79.2%)</option>
<option value="file5">github.com/partitio/gonextcloud/utils.go (100.0%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">no coverage</span>
<span class="cov1">low coverage</span>
<span class="cov2">*</span>
<span class="cov3">*</span>
<span class="cov4">*</span>
<span class="cov5">*</span>
<span class="cov6">*</span>
<span class="cov7">*</span>
<span class="cov8">*</span>
<span class="cov9">*</span>
<span class="cov10">high coverage</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package gonextcloud
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
)
func (c *Client) AppList() ([]string, error) <span class="cov0" title="0">{
if !c.loggedIn() </span><span class="cov0" title="0">{
return nil, unauthorized
}</span>
<span class="cov0" title="0">u := c.baseURL.ResolveReference(routes.apps)
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.AppListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Apps, nil
return nil, nil</span>
}
func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error <span class="cov0" title="0">{
res, err := c.baseRequest(routes.apps, name, route, ro, method)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">var r types.UserResponse
res.JSON(&amp;r)
if r.Ocs.Meta.Statuscode != 100 </span><span class="cov0" title="0">{
e := types.ErrorFromMeta(r.Ocs.Meta)
return &amp;e
}</span>
<span class="cov0" title="0">return nil</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package gonextcloud
import (
"fmt"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
)
var unauthorized = fmt.Errorf("login first")
func (c *Client) Login(username string, password string) error <span class="cov3" title="3">{
c.username = username
c.password = password
options := req.RequestOptions{
Headers: c.headers,
Auth: []string{c.username, c.password},
}
c.session = req.NewSession(&amp;options)
// TODO What to do with capabilities ? (other thant connection validation)
u := c.baseURL.ResolveReference(routes.capabilities)
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov1" title="1">{
return err
}</span>
<span class="cov2" title="2">var r types.CapabilitiesResponse
res.JSON(&amp;r)
// No need to check for Ocs.Meta.StatusCode as capabilities are always returned
c.capabilities = &amp;r.Ocs.Data.Capabilities
// Check if authentication failed
if !c.loggedIn() </span><span class="cov1" title="1">{
e := types.APIError{Message: "authentication failed"}
return &amp;e
}</span>
<span class="cov1" title="1">return nil</span>
}
func (c *Client) Logout() error <span class="cov1" title="1">{
c.session.CloseIdleConnections()
c.session.HTTPClient.Jar = nil
// Clear capabilities as it is used to check for valid authentication
c.capabilities = nil
return nil
}</span>
func (c *Client) loggedIn() bool <span class="cov10" title="39">{
// When authentication failed, capabilities doesn't contains core information
if c.capabilities == nil </span><span class="cov1" title="1">{
return false
}</span>
<span class="cov9" title="38">return c.capabilities.Core.WebdavRoot != ""</span>
}
</pre>
<pre class="file" id="file2" style="display: none">/*
Package client is a Go client for the Nextcloud Provisioning 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
Usage
You use the library by creating a client object and calling methods on it.
For example, to list all the Nextcloud's instance users:
package main
import (
"fmt"
"github.com/partitio/gonextcloud/client"
)
func main() {
url := "https://www.mynextcloud.com"
username := "admin"
password := "password"
c, err := client.NewClient(url)
if err != nil {
panic(err)
}
if err := c.Login(username, password); err != nil {
panic(err)
}
defer c.Logout()
users, err := c.UserList()
if err != nil {
panic(err)
}
fmt.Println("Users :", users)
}
*/
package gonextcloud
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/url"
)
type Client struct {
baseURL *url.URL
username string
password string
session *req.Session
headers map[string]string
capabilities *types.Capabilities
}
func NewClient(hostname string) (*Client, error) <span class="cov10" title="3">{
baseURL, err := url.ParseRequestURI(hostname)
if err != nil </span><span class="cov6" title="2">{
baseURL, err = url.ParseRequestURI("https://" + hostname)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
}
<span class="cov10" title="3">c := Client{
baseURL: baseURL,
headers: map[string]string{
"OCS-APIREQUEST": "true",
"Accept": "application/json",
},
}
return &amp;c, nil</span>
}
</pre>
<pre class="file" id="file3" style="display: none">package gonextcloud
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http"
)
func (c *Client) GroupList() ([]string, error) <span class="cov1" title="1">{
res, err := c.baseRequest(routes.groups, "", "", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.GroupListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Groups, nil</span>
}
func (c *Client) GroupUsers(name string) ([]string, error) <span class="cov0" title="0">{
res, err := c.baseRequest(routes.groups, name, "", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.UserListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Users, nil</span>
}
func (c *Client) GroupSearch(search string) ([]string, error) <span class="cov0" title="0">{
ro := &amp;req.RequestOptions{
Params: map[string]string{"search": search},
}
res, err := c.baseRequest(routes.groups, "", "", ro, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.GroupListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Groups, nil</span>
}
func (c *Client) GroupCreate(name string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": name,
},
}
if err := c.groupBaseRequest("", "", ro, http.MethodPost); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return nil</span>
}
func (c *Client) GroupDelete(name string) error <span class="cov1" title="1">{
if err := c.groupBaseRequest(name, "", nil, http.MethodDelete); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return nil</span>
}
func (c *Client) GroupSubAdminList(name string) ([]string, error) <span class="cov0" title="0">{
res, err := c.baseRequest(routes.groups, name, "subadmins", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.UserListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Users, nil</span>
}
func (c *Client) groupBaseRequest(name string, route string, ro *req.RequestOptions, method string) error <span class="cov10" title="2">{
res, err := c.baseRequest(routes.groups, name, route, ro, method)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov10" title="2">var r types.GroupListResponse
res.JSON(&amp;r)
return nil</span>
}
</pre>
<pre class="file" id="file4" style="display: none">package gonextcloud
import (
"encoding/json"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http"
"path"
"strconv"
)
func (c *Client) UserList() ([]string, error) <span class="cov1" title="1">{
if !c.loggedIn() </span><span class="cov0" title="0">{
return nil, unauthorized
}</span>
<span class="cov1" title="1">u := c.baseURL.ResolveReference(routes.users)
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.UserListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Users, nil</span>
}
func (c *Client) User(name string) (*types.User, error) <span class="cov8" title="11">{
if name == "" </span><span class="cov1" title="1">{
return nil, &amp;types.APIError{Message: "name cannot be empty"}
}</span>
<span class="cov8" title="10">if !c.loggedIn() </span><span class="cov0" title="0">{
return nil, unauthorized
}</span>
<span class="cov8" title="10">u := c.baseURL.ResolveReference(routes.users)
u.Path = path.Join(u.Path, name)
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="10">var r types.UserResponse
js := res.String()
// Nextcloud does not encode JSON properly
js = reformatJSON(js)
if err := json.Unmarshal([]byte(js), &amp;r); err != nil </span><span class="cov1" title="1">{
return nil, err
}</span>
<span class="cov8" title="9">if r.Ocs.Meta.Statuscode != 100 </span><span class="cov0" title="0">{
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &amp;e
}</span>
<span class="cov8" title="9">return &amp;r.Ocs.Data, nil</span>
}
func (c *Client) UserSearch(search string) ([]string, error) <span class="cov1" title="1">{
if !c.loggedIn() </span><span class="cov0" title="0">{
return nil, unauthorized
}</span>
<span class="cov1" title="1">u := c.baseURL.ResolveReference(routes.users)
ro := &amp;req.RequestOptions{
Params: map[string]string{"search": search},
}
res, err := c.session.Get(u.String(), ro)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.UserListResponse
res.JSON(&amp;r)
if r.Ocs.Meta.Statuscode != 100 </span><span class="cov0" title="0">{
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &amp;e
}</span>
<span class="cov1" title="1">return r.Ocs.Data.Users, nil</span>
}
func (c *Client) UserCreate(username string, password string) error <span class="cov3" title="2">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"userid": username,
"password": password,
},
}
return c.userBaseRequest("", "", ro, http.MethodPost)
}</span>
func (c *Client) UserDelete(name string) error <span class="cov1" title="1">{
return c.userBaseRequest(name, "", nil, http.MethodDelete)
}</span>
func (c *Client) UserEnable(name string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{},
}
return c.userBaseRequest(name, "enable", ro, http.MethodPut)
}</span>
func (c *Client) UserDisable(name string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{},
}
return c.userBaseRequest(name, "disable", ro, http.MethodPut)
}</span>
func (c *Client) UserSendWelcomeEmail(name string) error <span class="cov0" title="0">{
return c.userBaseRequest(name, "welcome", nil, http.MethodPost)
}</span>
func (c *Client) UserUpdateEmail(name string, email string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "email", email)
}</span>
func (c *Client) UserUpdateDisplayName(name string, displayName string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "displayname", displayName)
}</span>
func (c *Client) UserUpdatePhone(name string, phone string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "phone", phone)
}</span>
func (c *Client) UserUpdateAddress(name string, address string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "address", address)
}</span>
func (c *Client) UserUpdateWebSite(name string, website string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "website", website)
}</span>
func (c *Client) UserUpdateTwitter(name string, twitter string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "twitter", twitter)
}</span>
func (c *Client) UserUpdatePassword(name string, password string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "password", password)
}</span>
func (c *Client) UserUpdateQuota(name string, quota int) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota))
}</span>
func (c *Client) UserGroupList(name string) ([]string, error) <span class="cov1" title="1">{
if !c.loggedIn() </span><span class="cov0" title="0">{
return nil, unauthorized
}</span>
<span class="cov1" title="1">u := c.baseURL.ResolveReference(routes.users)
u.Path = path.Join(u.Path, name, "groups")
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.GroupListResponse
res.JSON(&amp;r)
if r.Ocs.Meta.Statuscode != 100 </span><span class="cov0" title="0">{
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &amp;e
}</span>
<span class="cov1" title="1">return r.Ocs.Data.Groups, nil</span>
}
func (c *Client) UserGroupAdd(name string, group string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "groups", ro, http.MethodPost)
}</span>
func (c *Client) UserGroupRemove(name string, group string) error <span class="cov0" title="0">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "groups", ro, http.MethodDelete)
}</span>
func (c *Client) UserGroupPromote(name string, group string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "subadmins", ro, http.MethodPost)
}</span>
func (c *Client) UserGroupDemote(name string, group string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "subadmins", ro, http.MethodDelete)
}</span>
func (c *Client) UserGroupSubAdminList(name string) ([]string, error) <span class="cov3" title="2">{
if !c.loggedIn() </span><span class="cov0" title="0">{
return nil, unauthorized
}</span>
<span class="cov3" title="2">u := c.baseURL.ResolveReference(routes.users)
u.Path = path.Join(u.Path, name, "subadmins")
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov3" title="2">var r types.BaseResponse
res.JSON(&amp;r)
if r.Ocs.Meta.Statuscode != 100 </span><span class="cov1" title="1">{
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &amp;e
}</span>
<span class="cov1" title="1">return r.Ocs.Data, nil</span>
}
func (c *Client) userUpdateAttribute(name string, key string, value string) error <span class="cov7" title="8">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"key": key,
"value": value,
},
}
return c.userBaseRequest(name, "", ro, http.MethodPut)
}</span>
func (c *Client) userBaseRequest(name string, route string, ro *req.RequestOptions, method string) error <span class="cov10" title="16">{
res, err := c.baseRequest(routes.users, name, route, ro, method)
if err != nil </span><span class="cov1" title="1">{
return err
}</span>
<span class="cov9" title="15">var r types.UserResponse
res.JSON(&amp;r)
if r.Ocs.Meta.Statuscode != 100 </span><span class="cov0" title="0">{
e := types.ErrorFromMeta(r.Ocs.Meta)
return &amp;e
}</span>
<span class="cov9" title="15">return nil</span>
}
</pre>
<pre class="file" id="file5" style="display: none">package gonextcloud
import (
"encoding/json"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http"
"net/url"
"path"
"strings"
)
func (c *Client) baseRequest(route *url.URL, name string, subroute string, ro *req.RequestOptions, method string) (*req.Response, error) <span class="cov10" title="21">{
if !c.loggedIn() </span><span class="cov1" title="1">{
return nil, unauthorized
}</span>
<span class="cov9" title="20">u := c.baseURL.ResolveReference(route)
if name != "" </span><span class="cov9" title="16">{
u.Path = path.Join(u.Path, name)
}</span>
<span class="cov9" title="20">if subroute != "" </span><span class="cov6" title="6">{
u.Path = path.Join(u.Path, subroute)
}</span>
<span class="cov9" title="20">var (
res *req.Response
err error
)
if method == http.MethodGet </span><span class="cov3" title="2">{
res, err = c.session.Get(u.String(), ro)
}</span> else<span class="cov9" title="18"> if method == http.MethodPost </span><span class="cov5" title="5">{
res, err = c.session.Post(u.String(), ro)
}</span> else<span class="cov8" title="13"> if method == http.MethodPut </span><span class="cov7" title="10">{
res, err = c.session.Put(u.String(), ro)
}</span> else<span class="cov4" title="3"> if method == http.MethodDelete </span><span class="cov4" title="3">{
res, err = c.session.Delete(u.String(), ro)
}</span>
<span class="cov9" title="20">if err != nil </span><span class="cov1" title="1">{
return nil, err
}</span>
// As we cannot read the ReaderCloser twice, we use the string content
<span class="cov9" title="19">js := res.String()
var r types.BaseResponse
json.Unmarshal([]byte(js), &amp;r)
if r.Ocs.Meta.Statuscode != 100 </span><span class="cov1" title="1">{
err := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &amp;err
}</span>
<span class="cov9" title="18">return res, nil</span>
}
func reformatJSON(json string) string <span class="cov7" title="10">{
// Nextcloud encode boolean as string
json = strings.Replace(json, "\"true\"", "true", -1)
json = strings.Replace(json, "\"false\"", "false", -1)
// Nextcloud encode quota as an empty array for never connected users
json = strings.Replace(json, "\"quota\":[],", "", -1)
return json
}</span>
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>

View File

@ -1,16 +1,23 @@
package gonextcloud package gonextcloud
import ( import (
"github.com/partitio/gonextcloud/client" "fmt"
"github.com/fatih/structs"
"github.com/partitio/gonextcloud/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"net/http"
"net/url"
"os" "os"
"strings"
"testing" "testing"
) )
var config = Config{} var config = Config{}
var c *client.Client var c *Client
const password = "somecomplicatedpassword"
type Config struct { type Config struct {
URL string `yaml:"url"` URL string `yaml:"url"`
@ -37,10 +44,6 @@ func LoadConfig() error {
return nil return nil
} }
func TestTruth(t *testing.T) {
assert.Equal(t, true, true, "seriously ??!")
}
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {
err := LoadConfig() err := LoadConfig()
assert.Nil(t, err) assert.Nil(t, err)
@ -48,13 +51,13 @@ func TestLoadConfig(t *testing.T) {
func TestClient(t *testing.T) { func TestClient(t *testing.T) {
var err error var err error
c, err = client.NewClient(config.URL) c, err = NewClient(config.URL)
assert.Nil(t, err, "aie") assert.Nil(t, err, "aie")
} }
func TestLoginFail(t *testing.T) { func TestLoginFail(t *testing.T) {
err := c.Login("", "") err := c.Login("", "")
assert.NotNil(t, err) assert.Error(t, err)
} }
func TestLogin(t *testing.T) { func TestLogin(t *testing.T) {
@ -77,13 +80,13 @@ func TestExistingUser(t *testing.T) {
func TestEmptyUser(t *testing.T) { func TestEmptyUser(t *testing.T) {
u, err := c.User("") u, err := c.User("")
assert.NotNil(t, err) assert.Error(t, err)
assert.Empty(t, u) assert.Empty(t, u)
} }
func TestNonExistingUser(t *testing.T) { func TestNonExistingUser(t *testing.T) {
_, err := c.User(config.NotExistingUser) _, err := c.User(config.NotExistingUser)
assert.NotNil(t, err) assert.Error(t, err)
} }
func TestUserSearch(t *testing.T) { func TestUserSearch(t *testing.T) {
@ -93,12 +96,76 @@ func TestUserSearch(t *testing.T) {
} }
func TestUserCreate(t *testing.T) { func TestUserCreate(t *testing.T) {
err := c.UserCreate(config.NotExistingUser, "somecomplicatedpassword") err := c.UserCreate(config.NotExistingUser, password, nil)
assert.Nil(t, err)
}
func TestUserCreateFull(t *testing.T) {
if err := initClient(); err != nil {
return
}
username := fmt.Sprintf("%s-2", config.NotExistingUser)
user := &types.User{
ID: username,
Displayname: strings.ToUpper(username),
Email: "some@address.com",
Address: "Main Street, City",
Twitter: "@me",
Phone: "42 42 242 424",
Website: "my.site.com",
}
err := c.UserCreate(username, password, user)
assert.Nil(t, err)
u, err := c.User(username)
assert.Nil(t, err)
o := structs.Map(user)
r := structs.Map(u)
for k := range o {
if ignoredUserField(k) {
continue
}
assert.Equal(t, o[k], r[k])
}
// Clean up
err = c.UserDelete(u.ID)
assert.Nil(t, err)
}
func TestUserUpdate(t *testing.T) {
if err := initClient(); err != nil {
return
}
username := fmt.Sprintf("%s-2", config.NotExistingUser)
err := c.UserCreate(username, password, nil)
assert.Nil(t, err)
user := &types.User{
ID: username,
Displayname: strings.ToUpper(username),
Email: "some@address.com",
Address: "Main Street, City",
Twitter: "@me",
Phone: "42 42 242 424",
Website: "my.site.com",
}
err = c.UserUpdate(user)
assert.Nil(t, err)
u, err := c.User(username)
assert.Nil(t, err)
o := structs.Map(user)
r := structs.Map(u)
for k := range o {
if ignoredUserField(k) {
continue
}
assert.Equal(t, o[k], r[k])
}
// Clean up
err = c.UserDelete(u.ID)
assert.Nil(t, err) assert.Nil(t, err)
} }
func TestUserCreateExisting(t *testing.T) { func TestUserCreateExisting(t *testing.T) {
err := c.UserCreate(config.NotExistingUser, "somecomplicatedpassword") err := c.UserCreate(config.NotExistingUser, password, nil)
assert.NotNil(t, err) assert.NotNil(t, err)
} }
@ -193,7 +260,7 @@ func TestUserGroupAdd(t *testing.T) {
func TestUserGroupSubAdminList(t *testing.T) { func TestUserGroupSubAdminList(t *testing.T) {
gs, err := c.UserGroupSubAdminList(config.NotExistingUser) gs, err := c.UserGroupSubAdminList(config.NotExistingUser)
assert.NotNil(t, err) assert.Nil(t, err)
assert.Empty(t, gs) assert.Empty(t, gs)
} }
@ -239,7 +306,49 @@ func TestUserDelete(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
} }
func TestInvalidBaseRequest(t *testing.T) {
c.baseURL = &url.URL{}
_, err := c.baseRequest(routes.capabilities, "admin", "invalid", nil, http.MethodGet)
assert.Error(t, err)
}
func TestLogout(t *testing.T) { func TestLogout(t *testing.T) {
err := c.Logout() err := c.Logout()
assert.Nil(t, err) assert.Nil(t, err)
assert.Nil(t, c.session.HTTPClient.Jar)
}
func TestLoggedIn(t *testing.T) {
c := &Client{}
c.capabilities = &types.Capabilities{}
assert.False(t, c.loggedIn())
}
func TestLoginInvalidURL(t *testing.T) {
c, _ = NewClient("")
err := c.Login("", "")
assert.Error(t, err)
}
func TestBaseRequest(t *testing.T) {
c, _ = NewClient("")
_, err := c.baseRequest(routes.capabilities, "admin", "invalid", nil, http.MethodGet)
assert.Error(t, err)
}
func initClient() error {
if c == nil {
if err := LoadConfig(); err != nil {
return err
}
var err error
c, err = NewClient(config.URL)
if err != nil {
return err
}
if err = c.Login(config.Login, config.Password); err != nil {
return err
}
}
return nil
} }

View File

@ -1,8 +1,8 @@
package client package gonextcloud
import ( import (
req "github.com/levigross/grequests" req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types" "github.com/partitio/gonextcloud/types"
"net/http" "net/http"
) )
@ -13,10 +13,6 @@ func (c *Client) GroupList() ([]string, error) {
} }
var r types.GroupListResponse var r types.GroupListResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
@ -27,10 +23,6 @@ func (c *Client) GroupUsers(name string) ([]string, error) {
} }
var r types.UserListResponse var r types.UserListResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
@ -44,10 +36,6 @@ func (c *Client) GroupSearch(search string) ([]string, error) {
} }
var r types.GroupListResponse var r types.GroupListResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
@ -77,23 +65,10 @@ func (c *Client) GroupSubAdminList(name string) ([]string, error) {
} }
var r types.UserListResponse var r types.UserListResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
func (c *Client) groupBaseRequest(name string, route string, ro *req.RequestOptions, method string) error { func (c *Client) groupBaseRequest(name string, route string, ro *req.RequestOptions, method string) error {
res, err := c.baseRequest(routes.groups, name, route, ro, method) _, err := c.baseRequest(routes.groups, name, route, ro, method)
if err != nil { return err
return err
}
var r types.GroupListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return &e
}
return nil
} }

View File

@ -1,6 +1,6 @@
package client package gonextcloud
import "github.com/partitio/gonextcloud/client/types" import "github.com/partitio/gonextcloud/types"
type BaseClient interface { type BaseClient interface {
NewClient(hostname string) (*Client, error) NewClient(hostname string) (*Client, error)

View File

@ -1,4 +1,4 @@
package client package gonextcloud
import "net/url" import "net/url"

55
types/errors.go Normal file
View File

@ -0,0 +1,55 @@
package types
import (
"fmt"
"strings"
)
type APIError struct {
Code int
Message string
}
func ErrorFromMeta(meta Meta) *APIError {
return &APIError{
meta.Statuscode,
meta.Message,
}
}
func (e *APIError) Error() string {
return fmt.Sprintf("%d : %s", e.Code, e.Message)
}
type UpdateError struct {
Field string
Error error
}
type UserUpdateError struct {
Errors map[string]error
}
func (e *UserUpdateError) Error() string {
var errors []string
for k, e := range e.Errors {
errors = append(errors, fmt.Sprintf("%s: %s", k, e.Error()))
}
return strings.Join(errors, ",")
}
func NewUpdateError(errors chan UpdateError) *UserUpdateError {
empty := true
var ue UserUpdateError
for e := range errors {
if ue.Errors == nil {
empty = false
ue.Errors = map[string]error{e.Field: e.Error}
}
ue.Errors[e.Field] = e.Error
}
if !empty {
return &ue
}
return nil
}

View File

@ -1,20 +1,20 @@
package client package gonextcloud
import ( import (
"encoding/json" "encoding/json"
"github.com/fatih/structs"
req "github.com/levigross/grequests" req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types" "github.com/partitio/gonextcloud/types"
"net/http" "net/http"
"path" "path"
"strconv" "strconv"
"strings"
"sync"
) )
func (c *Client) UserList() ([]string, error) { func (c *Client) UserList() ([]string, error) {
if !c.loggedIn() { res, err := c.baseRequest(routes.users, "", "", nil, http.MethodGet)
return nil, unauthorized //res, err := c.session.Get(u.String(), nil)
}
u := c.baseURL.ResolveReference(routes.users)
res, err := c.session.Get(u.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -27,12 +27,7 @@ func (c *Client) User(name string) (*types.User, error) {
if name == "" { if name == "" {
return nil, &types.APIError{Message: "name cannot be empty"} return nil, &types.APIError{Message: "name cannot be empty"}
} }
if !c.loggedIn() { res, err := c.baseRequest(routes.users, name, "", nil, http.MethodGet)
return nil, unauthorized
}
u := c.baseURL.ResolveReference(routes.users)
u.Path = path.Join(u.Path, name)
res, err := c.session.Get(u.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -43,42 +38,36 @@ func (c *Client) User(name string) (*types.User, error) {
if err := json.Unmarshal([]byte(js), &r); err != nil { if err := json.Unmarshal([]byte(js), &r); err != nil {
return nil, err return nil, err
} }
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return &r.Ocs.Data, nil return &r.Ocs.Data, nil
} }
func (c *Client) UserSearch(search string) ([]string, error) { func (c *Client) UserSearch(search string) ([]string, error) {
if !c.loggedIn() {
return nil, unauthorized
}
u := c.baseURL.ResolveReference(routes.users)
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Params: map[string]string{"search": search}, Params: map[string]string{"search": search},
} }
res, err := c.session.Get(u.String(), ro) res, err := c.baseRequest(routes.users, "", "", ro, http.MethodGet)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var r types.UserListResponse var r types.UserListResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Users, nil return r.Ocs.Data.Users, nil
} }
func (c *Client) UserCreate(username string, password string) error { func (c *Client) UserCreate(username string, password string, user *types.User) error {
ro := &req.RequestOptions{ ro := &req.RequestOptions{
Data: map[string]string{ Data: map[string]string{
"userid": username, "userid": username,
"password": password, "password": password,
}, },
} }
return c.userBaseRequest("", "", ro, http.MethodPost) if err := c.userBaseRequest("", "", ro, http.MethodPost); err != nil {
return err
}
if user == nil {
return nil
}
return c.UserUpdate(user)
} }
func (c *Client) UserDelete(name string) error { func (c *Client) UserDelete(name string) error {
@ -103,6 +92,31 @@ func (c *Client) UserSendWelcomeEmail(name string) error {
return c.userBaseRequest(name, "welcome", nil, http.MethodPost) return c.userBaseRequest(name, "welcome", nil, http.MethodPost)
} }
func (c *Client) UserUpdate(user *types.User) error {
m := structs.Map(user)
errs := make(chan types.UpdateError)
var wg sync.WaitGroup
for k := range m {
if !ignoredUserField(k) && m[k].(string) != "" {
wg.Add(1)
go func(key string, value string) {
defer wg.Done()
if err := c.userUpdateAttribute(user.ID, strings.ToLower(key), value); err != nil {
errs <- types.UpdateError{
Field: key,
Error: err,
}
}
}(k, m[k].(string))
}
}
go func() {
wg.Wait()
close(errs)
}()
return types.NewUpdateError(errs)
}
func (c *Client) UserUpdateEmail(name string, email string) error { func (c *Client) UserUpdateEmail(name string, email string) error {
return c.userUpdateAttribute(name, "email", email) return c.userUpdateAttribute(name, "email", email)
} }
@ -136,21 +150,12 @@ func (c *Client) UserUpdateQuota(name string, quota int) error {
} }
func (c *Client) UserGroupList(name string) ([]string, error) { func (c *Client) UserGroupList(name string) ([]string, error) {
if !c.loggedIn() { res, err := c.baseRequest(routes.users, name, "groups", nil, http.MethodGet)
return nil, unauthorized
}
u := c.baseURL.ResolveReference(routes.users)
u.Path = path.Join(u.Path, name, "groups")
res, err := c.session.Get(u.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var r types.GroupListResponse var r types.GroupListResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Groups, nil return r.Ocs.Data.Groups, nil
} }
@ -202,10 +207,6 @@ func (c *Client) UserGroupSubAdminList(name string) ([]string, error) {
} }
var r types.BaseResponse var r types.BaseResponse
res.JSON(&r) res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data, nil return r.Ocs.Data, nil
} }
@ -220,15 +221,16 @@ func (c *Client) userUpdateAttribute(name string, key string, value string) erro
} }
func (c *Client) userBaseRequest(name string, route string, ro *req.RequestOptions, method string) error { func (c *Client) userBaseRequest(name string, route string, ro *req.RequestOptions, method string) error {
res, err := c.baseRequest(routes.users, name, route, ro, method) _, err := c.baseRequest(routes.users, name, route, ro, method)
if err != nil { return err
return err }
}
var r types.UserResponse func ignoredUserField(key string) bool {
res.JSON(&r) keys := []string{"ID", "Quota", "Enabled", "Groups", "Language"}
if r.Ocs.Meta.Statuscode != 100 { for _, k := range keys {
e := types.ErrorFromMeta(r.Ocs.Meta) if key == k {
return &e return true
} }
return nil }
return false
} }

View File

@ -1,7 +1,9 @@
package client package gonextcloud
import ( import (
"encoding/json"
req "github.com/levigross/grequests" req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
@ -35,6 +37,14 @@ func (c *Client) baseRequest(route *url.URL, name string, subroute string, ro *r
if err != nil { if err != nil {
return nil, err return nil, err
} }
// As we cannot read the ReaderCloser twice, we use the string content
js := res.String()
var r types.BaseResponse
json.Unmarshal([]byte(js), &r)
if r.Ocs.Meta.Statuscode != 100 {
err := types.ErrorFromMeta(r.Ocs.Meta)
return nil, err
}
return res, nil return res, nil
} }