diff --git a/client/auth.go b/client/auth.go index 9bc782f..9019867 100644 --- a/client/auth.go +++ b/client/auth.go @@ -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 } diff --git a/client/groups.go b/client/groups.go index da13c8e..d4b5da1 100644 --- a/client/groups.go +++ b/client/groups.go @@ -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 +} diff --git a/client/types/responses.go b/client/types/responses.go index c17ef84..b7db77a 100644 --- a/client/types/responses.go +++ b/client/types/responses.go @@ -41,7 +41,7 @@ type UserResponse struct { } `json:"ocs"` } -type SubAdminResponse struct { +type BaseResponse struct { Ocs struct { Meta struct { Status string `json:"status"` diff --git a/client/users.go b/client/users.go index 486b978..f65178f 100644 --- a/client/users.go +++ b/client/users.go @@ -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 } diff --git a/client/utils.go b/client/utils.go new file mode 100644 index 0000000..b48446a --- /dev/null +++ b/client/utils.go @@ -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 +}