mirror of
				https://gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud
				synced 2025-11-04 06:21:45 +00:00 
			
		
		
		
	Nextcloud API Client, Uers/Response types, User list/search/get/create/delete
This commit is contained in:
		
							
								
								
									
										1
									
								
								client/apps.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								client/apps.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
package client
 | 
			
		||||
							
								
								
									
										37
									
								
								client/auth.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								client/auth.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
package client
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	req "github.com/levigross/grequests"
 | 
			
		||||
	"github.com/partitio/gonextcloud/client/types"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var unauthorized = fmt.Errorf("login first")
 | 
			
		||||
 | 
			
		||||
func (c *Client) Login(username string, password string) error {
 | 
			
		||||
	c.username = username
 | 
			
		||||
	c.password = password
 | 
			
		||||
	options := req.RequestOptions{
 | 
			
		||||
		Headers: c.headers,
 | 
			
		||||
		Auth:    []string{c.username, c.password},
 | 
			
		||||
	}
 | 
			
		||||
	c.session = req.NewSession(&options)
 | 
			
		||||
	u := c.baseURL.ResolveReference(routes.capabilities)
 | 
			
		||||
	r, err := c.session.Get(u.String(), nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	var cs types.CapabilitiesResponse
 | 
			
		||||
	r.JSON(&cs)
 | 
			
		||||
	c.capabilities = &cs.Ocs.Data.Capabilities
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) Logout() error {
 | 
			
		||||
	c.session.CloseIdleConnections()
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) loggedIn() bool {
 | 
			
		||||
	return c.capabilities != nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								client/client.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								client/client.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
package client
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	req "github.com/levigross/grequests"
 | 
			
		||||
	"github.com/partitio/gonextcloud/client/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) {
 | 
			
		||||
	baseURL, err := url.Parse(hostname)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	c := Client{
 | 
			
		||||
		baseURL: baseURL,
 | 
			
		||||
		headers: map[string]string{
 | 
			
		||||
			"OCS-APIREQUEST": "true",
 | 
			
		||||
			"Accept":         "application/json",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	return &c, nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								client/groups.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								client/groups.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
package client
 | 
			
		||||
							
								
								
									
										19
									
								
								client/routes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								client/routes.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
package client
 | 
			
		||||
 | 
			
		||||
import "net/url"
 | 
			
		||||
 | 
			
		||||
type Routes struct {
 | 
			
		||||
	capabilities *url.URL
 | 
			
		||||
	users        *url.URL
 | 
			
		||||
	groups       *url.URL
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	apiPath = &url.URL{Path: "/ocs/v1.php/cloud"}
 | 
			
		||||
	routes  = Routes{
 | 
			
		||||
		capabilities: &url.URL{Path: apiPath.Path + "/capabilities"},
 | 
			
		||||
		users:        &url.URL{Path: apiPath.Path + "/users"},
 | 
			
		||||
		groups:       &url.URL{Path: apiPath.Path + "/groups"},
 | 
			
		||||
	}
 | 
			
		||||
	badRequest = 998
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										94
									
								
								client/types/capabilities.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								client/types/capabilities.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
			
		||||
package types
 | 
			
		||||
 | 
			
		||||
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"`
 | 
			
		||||
	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"`
 | 
			
		||||
		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"`
 | 
			
		||||
	} `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"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								client/types/responses.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								client/types/responses.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
package types
 | 
			
		||||
 | 
			
		||||
type ErrorResponse struct {
 | 
			
		||||
	Ocs struct {
 | 
			
		||||
		Meta struct {
 | 
			
		||||
			Status       string `json:"status"`
 | 
			
		||||
			Statuscode   int    `json:"statuscode"`
 | 
			
		||||
			Message      string `json:"message"`
 | 
			
		||||
			Totalitems   string `json:"totalitems"`
 | 
			
		||||
			Itemsperpage string `json:"itemsperpage"`
 | 
			
		||||
		} `json:"meta"`
 | 
			
		||||
		Data []interface{} `json:"data"`
 | 
			
		||||
	} `json:"ocs"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type UserListResponse struct {
 | 
			
		||||
	Ocs struct {
 | 
			
		||||
		Meta struct {
 | 
			
		||||
			Status       string `json:"status"`
 | 
			
		||||
			Statuscode   int    `json:"statuscode"`
 | 
			
		||||
			Message      string `json:"message"`
 | 
			
		||||
			Totalitems   string `json:"totalitems"`
 | 
			
		||||
			Itemsperpage string `json:"itemsperpage"`
 | 
			
		||||
		} `json:"meta"`
 | 
			
		||||
		Data struct {
 | 
			
		||||
			Users []string `json:"users"`
 | 
			
		||||
		} `json:"data"`
 | 
			
		||||
	} `json:"ocs"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type UserResponse struct {
 | 
			
		||||
	Ocs struct {
 | 
			
		||||
		Meta struct {
 | 
			
		||||
			Status       string `json:"status"`
 | 
			
		||||
			Statuscode   int    `json:"statuscode"`
 | 
			
		||||
			Message      string `json:"message"`
 | 
			
		||||
			Totalitems   string `json:"totalitems"`
 | 
			
		||||
			Itemsperpage string `json:"itemsperpage"`
 | 
			
		||||
		} `json:"meta"`
 | 
			
		||||
		Data User `json:"data"`
 | 
			
		||||
	} `json:"ocs"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type GroupListResponse struct {
 | 
			
		||||
	Ocs struct {
 | 
			
		||||
		Meta struct {
 | 
			
		||||
			Status       string `json:"status"`
 | 
			
		||||
			Statuscode   int    `json:"statuscode"`
 | 
			
		||||
			Message      string `json:"message"`
 | 
			
		||||
			Totalitems   string `json:"totalitems"`
 | 
			
		||||
			Itemsperpage string `json:"itemsperpage"`
 | 
			
		||||
		} `json:"meta"`
 | 
			
		||||
		Data struct {
 | 
			
		||||
			Groups []string `json:"groups"`
 | 
			
		||||
		} `json:"data"`
 | 
			
		||||
	} `json:"ocs"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type CapabilitiesResponse struct {
 | 
			
		||||
	Ocs struct {
 | 
			
		||||
		Meta struct {
 | 
			
		||||
			Status       string `json:"status"`
 | 
			
		||||
			Statuscode   int    `json:"statuscode"`
 | 
			
		||||
			Message      string `json:"message"`
 | 
			
		||||
			Totalitems   string `json:"totalitems"`
 | 
			
		||||
			Itemsperpage string `json:"itemsperpage"`
 | 
			
		||||
		} `json:"meta"`
 | 
			
		||||
		Data struct {
 | 
			
		||||
			Version struct {
 | 
			
		||||
				Major   int    `json:"major"`
 | 
			
		||||
				Minor   int    `json:"minor"`
 | 
			
		||||
				Micro   int    `json:"micro"`
 | 
			
		||||
				String  string `json:"string"`
 | 
			
		||||
				Edition string `json:"edition"`
 | 
			
		||||
			} `json:"version"`
 | 
			
		||||
			Capabilities Capabilities `json:"capabilities"`
 | 
			
		||||
		} `json:"data"`
 | 
			
		||||
	} `json:"ocs"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								client/types/user.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								client/types/user.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
package types
 | 
			
		||||
 | 
			
		||||
type User struct {
 | 
			
		||||
	Enabled string `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"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										103
									
								
								client/users.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								client/users.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,103 @@
 | 
			
		||||
package client
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	req "github.com/levigross/grequests"
 | 
			
		||||
	"github.com/partitio/gonextcloud/client/types"
 | 
			
		||||
	"path"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (c *Client) UserList() ([]string, error) {
 | 
			
		||||
	if !c.loggedIn() {
 | 
			
		||||
		return nil, unauthorized
 | 
			
		||||
	}
 | 
			
		||||
	u := c.baseURL.ResolveReference(routes.users)
 | 
			
		||||
	res, err := c.session.Get(u.String(), nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	var ul types.UserListResponse
 | 
			
		||||
	res.JSON(&ul)
 | 
			
		||||
	return ul.Ocs.Data.Users, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) User(name string) (*types.User, error) {
 | 
			
		||||
	if !c.loggedIn() {
 | 
			
		||||
		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 {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	var ur types.UserResponse
 | 
			
		||||
	res.JSON(&ur)
 | 
			
		||||
	if ur.Ocs.Meta.Statuscode != 100 {
 | 
			
		||||
		return nil, fmt.Errorf(ur.Ocs.Meta.Message)
 | 
			
		||||
	}
 | 
			
		||||
	return &ur.Ocs.Data, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) UserSearch(search string) ([]string, error) {
 | 
			
		||||
	if !c.loggedIn() {
 | 
			
		||||
		return nil, unauthorized
 | 
			
		||||
	}
 | 
			
		||||
	u := c.baseURL.ResolveReference(routes.users)
 | 
			
		||||
	ro := &req.RequestOptions{
 | 
			
		||||
		Params: map[string]string{"search": search},
 | 
			
		||||
	}
 | 
			
		||||
	res, err := c.session.Get(u.String(), ro)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	var r types.UserListResponse
 | 
			
		||||
	res.JSON(&r)
 | 
			
		||||
	if r.Ocs.Meta.Statuscode != 100 {
 | 
			
		||||
		return nil, fmt.Errorf(r.Ocs.Meta.Message)
 | 
			
		||||
	}
 | 
			
		||||
	return r.Ocs.Data.Users, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) UserCreate(username string, password string) error {
 | 
			
		||||
	if !c.loggedIn() {
 | 
			
		||||
		return unauthorized
 | 
			
		||||
	}
 | 
			
		||||
	u := c.baseURL.ResolveReference(routes.users)
 | 
			
		||||
	ro := &req.RequestOptions{
 | 
			
		||||
		Data: map[string]string{
 | 
			
		||||
			"userid":   username,
 | 
			
		||||
			"password": password,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	res, err := c.session.Post(u.String(), ro)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(res.String())
 | 
			
		||||
	var r types.UserResponse
 | 
			
		||||
	res.JSON(&r)
 | 
			
		||||
	if r.Ocs.Meta.Statuscode != 100 {
 | 
			
		||||
		return fmt.Errorf(r.Ocs.Meta.Message)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) UserDelete(name string) error {
 | 
			
		||||
	if !c.loggedIn() {
 | 
			
		||||
		return unauthorized
 | 
			
		||||
	}
 | 
			
		||||
	u := c.baseURL.ResolveReference(routes.users)
 | 
			
		||||
	u.Path = path.Join(u.Path, name)
 | 
			
		||||
	res, err := c.session.Delete(u.String(), nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	var ur types.UserResponse
 | 
			
		||||
	fmt.Println(res.String())
 | 
			
		||||
	res.JSON(&ur)
 | 
			
		||||
	if ur.Ocs.Meta.Statuscode != 100 {
 | 
			
		||||
		return fmt.Errorf(ur.Ocs.Meta.Message)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user