Added first groups implementation

This commit is contained in:
Philippe-Adrien Nousse 2018-07-09 12:53:53 +02:00
parent ee9ef934a4
commit b1fadec550
5 changed files with 127 additions and 30 deletions

View File

@ -23,6 +23,9 @@ func (c *Client) Login(username string, password string) error {
}
var cs types.CapabilitiesResponse
r.JSON(&cs)
if cs.Ocs.Meta.Statuscode != 100 {
return fmt.Errorf("%d : %s", cs.Ocs.Meta.Statuscode, cs.Ocs.Meta.Message)
}
c.capabilities = &cs.Ocs.Data.Capabilities
return nil
}

View File

@ -1 +1,79 @@
package client
import (
"fmt"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types"
"net/http"
)
func (c *Client) GroupList() ([]string, error) {
res, err := c.baseRequest(routes.groups, "", "", nil, http.MethodGet)
if err != nil {
return nil, err
}
var gr types.GroupListResponse
res.JSON(&gr)
if gr.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", gr.Ocs.Meta.Statuscode, gr.Ocs.Meta.Message)
}
return gr.Ocs.Data.Groups, nil
}
func (c *Client) Group(name string) ([]string, error) {
res, err := c.baseRequest(routes.groups, name, "", nil, http.MethodGet)
if err != nil {
return nil, err
}
var r types.UserListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
}
return r.Ocs.Data.Users, nil
}
func (c *Client) GroupCreate(name string) error {
ro := &req.RequestOptions{
Data: map[string]string{
"groupid": name,
},
}
if err := c.groupBaseRequest("", "", ro, http.MethodPost); err != nil {
return err
}
return nil
}
func (c *Client) GroupDelete(name string) error {
if err := c.groupBaseRequest(name, "", nil, http.MethodDelete); err != nil {
return err
}
return nil
}
func (c *Client) GroupSubAdminList(name string) ([]string, error) {
res, err := c.baseRequest(routes.groups, name, "subadmins", nil, http.MethodGet)
if err != nil {
return nil, err
}
var r types.UserListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
}
return r.Ocs.Data.Users, nil
}
func (c *Client) groupBaseRequest(name string, route string, ro *req.RequestOptions, method string) error {
res, err := c.baseRequest(routes.groups, name, route, ro, method)
if err != nil {
return err
}
var ur types.GroupListResponse
res.JSON(&ur)
if ur.Ocs.Meta.Statuscode != 100 {
return fmt.Errorf("%d : %s", ur.Ocs.Meta.Statuscode, ur.Ocs.Meta.Message)
}
return nil
}

View File

@ -41,7 +41,7 @@ type UserResponse struct {
} `json:"ocs"`
}
type SubAdminResponse struct {
type BaseResponse struct {
Ocs struct {
Meta struct {
Status string `json:"status"`

View File

@ -35,7 +35,7 @@ func (c *Client) User(name string) (*types.User, error) {
var ur types.UserResponse
res.JSON(&ur)
if ur.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf(ur.Ocs.Meta.Message)
return nil, fmt.Errorf("%d : %s", ur.Ocs.Meta.Statuscode, ur.Ocs.Meta.Message)
}
return &ur.Ocs.Data, nil
}
@ -55,7 +55,7 @@ func (c *Client) UserSearch(search string) ([]string, error) {
var r types.UserListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf(r.Ocs.Meta.Message)
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
}
return r.Ocs.Data.Users, nil
}
@ -137,7 +137,7 @@ func (c *Client) UserGroupList(name string) ([]string, error) {
var r types.GroupListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf(r.Ocs.Meta.Message)
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
}
return r.Ocs.Data.Groups, nil
}
@ -188,10 +188,10 @@ func (c *Client) UserGroupSubAdminList(name string) ([]string, error) {
if err != nil {
return nil, err
}
var r types.SubAdminResponse
var r types.BaseResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf(r.Ocs.Meta.Message)
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
}
return r.Ocs.Data, nil
}
@ -207,36 +207,14 @@ 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 {
if !c.loggedIn() {
return unauthorized
}
u := c.baseURL.ResolveReference(routes.users)
if name != "" {
u.Path = path.Join(u.Path, name)
}
if route != "" {
u.Path = path.Join(u.Path, route)
}
var (
res *req.Response
err error
)
if method == http.MethodGet {
res, err = c.session.Get(u.String(), ro)
} else if method == http.MethodPost {
res, err = c.session.Post(u.String(), ro)
} else if method == http.MethodPut {
res, err = c.session.Put(u.String(), ro)
} else if method == http.MethodDelete {
res, err = c.session.Delete(u.String(), ro)
}
res, err := c.baseRequest(routes.users, name, route, ro, method)
if err != nil {
return err
}
var ur types.UserResponse
res.JSON(&ur)
if ur.Ocs.Meta.Statuscode != 100 {
return fmt.Errorf(ur.Ocs.Meta.Message)
return fmt.Errorf("%d : %s", ur.Ocs.Meta.Statuscode, ur.Ocs.Meta.Message)
}
return nil
}

38
client/utils.go Normal file
View File

@ -0,0 +1,38 @@
package client
import (
req "github.com/levigross/grequests"
"net/http"
"net/url"
"path"
)
func (c *Client) baseRequest(route *url.URL, name string, subroute string, ro *req.RequestOptions, method string) (*req.Response, error) {
if !c.loggedIn() {
return nil, unauthorized
}
u := c.baseURL.ResolveReference(route)
if name != "" {
u.Path = path.Join(u.Path, name)
}
if subroute != "" {
u.Path = path.Join(u.Path, subroute)
}
var (
res *req.Response
err error
)
if method == http.MethodGet {
res, err = c.session.Get(u.String(), ro)
} else if method == http.MethodPost {
res, err = c.session.Post(u.String(), ro)
} else if method == http.MethodPut {
res, err = c.session.Put(u.String(), ro)
} else if method == http.MethodDelete {
res, err = c.session.Delete(u.String(), ro)
}
if err != nil {
return nil, err
}
return res, nil
}