mirror of
https://github.com/documize/community.git
synced 2025-07-19 13:19:43 +02:00
initial commit
This commit is contained in:
commit
18933c6767
1841 changed files with 810642 additions and 0 deletions
20
sdk/README.md
Normal file
20
sdk/README.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# SDK for the Documize system
|
||||
|
||||
The SDK is in development, please do not use in a live environment.
|
||||
|
||||
## documize command
|
||||
|
||||
The directory "documize" contains a command line utility to load files onto the Documize server.
|
||||
Run the command with "--help" to see the available flags.
|
||||
|
||||
## test suite (currently disabled)
|
||||
|
||||
The directory "exttest" contains a set of tests that are used both to test this package and to test the main documize code.
|
||||
|
||||
In order to run these tests two environment variables must be set:
|
||||
* DOCUMIZEAPI - the url of the endpoint, which must be http://localhost:5002 at present
|
||||
* DOCUMIZEAUTH - the authorization credentials in the form ```domain:email:password```,
|
||||
which must be of the form ```:mick@jagger.com:demo123``` at present,
|
||||
with the Documize DB organistion record having the default (empty) subdomain.
|
||||
|
||||
There must also be a single folder named "Test" for code to find and use. TODO(Elliott) remove this restriction.
|
30
sdk/api_test.go
Normal file
30
sdk/api_test.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize_test
|
||||
/* TODO(Elliott)
|
||||
import "testing"
|
||||
import "github.com/documize/community/sdk/exttest"
|
||||
|
||||
func TestAPItest(t *testing.T) {
|
||||
exttest.APItest(t)
|
||||
}
|
||||
|
||||
func BenchmarkAPIbench(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
err := exttest.APIbenchmark()
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
b.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
144
sdk/attachment.go
Normal file
144
sdk/attachment.go
Normal file
|
@ -0,0 +1,144 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
const emptyBraces = "{}"
|
||||
|
||||
// GetAttachmentData get the data of a file attachement.
|
||||
func (c *Client) GetAttachmentData(att *entity.Attachment) error {
|
||||
url := fmt.Sprintf("%s/api/public/attachments/%s/%s/%s",
|
||||
c.BaseURL, att.OrgID, att.Job, att.FileID)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
att.Data = b
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAttachment removes a file attachment.
|
||||
func (c *Client) DeleteAttachment(att *entity.Attachment) error {
|
||||
url := fmt.Sprintf("%s/api/documents/%s/attachments/%s",
|
||||
c.BaseURL, att.DocumentID, att.RefID)
|
||||
req, err := http.NewRequest("DELETE", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if string(b) != emptyBraces {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAttachments gets a slice of the attachments for a document ID.
|
||||
func (c *Client) GetAttachments(documentID string) (entAtts []entity.Attachment, err error) {
|
||||
url := fmt.Sprintf("%s/api/documents/%s/attachments",
|
||||
c.BaseURL, documentID)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(b, &entAtts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entAtts, nil
|
||||
}
|
||||
|
||||
// AddAttachment adds a new attachement to a document
|
||||
func (c *Client) AddAttachment(documentID, filename string, data []byte) error {
|
||||
url := fmt.Sprintf("%s/api/documents/%s/attachments",
|
||||
c.BaseURL, documentID)
|
||||
var buf bytes.Buffer
|
||||
w := multipart.NewWriter(&buf)
|
||||
|
||||
writer, err := w.CreateFormFile("attachment", filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := writer.Write(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != len(data) {
|
||||
return errors.New("incorrect length written")
|
||||
}
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewReader(buf.Bytes()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
req.Header.Set("Content-Type",
|
||||
"multipart/form-data; boundary="+w.Boundary())
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if string(b) != emptyBraces {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return nil
|
||||
}
|
95
sdk/auth.go
Normal file
95
sdk/auth.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/documize/api/endpoint/models"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// Client holds the data for a sustained connection to Documize.
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
Domain string
|
||||
Client *http.Client
|
||||
Auth models.AuthenticationModel
|
||||
}
|
||||
|
||||
// HeaderAuthTokenName is the name of the authorization token required in the http header
|
||||
const HeaderAuthTokenName = "Authorization"
|
||||
|
||||
// NewClient authorizes the user on Documize and returns the Client type whose methods allow API access the Documize system.
|
||||
func NewClient(baseurl, domainEmailPassword string) (*Client, error) {
|
||||
c := new(Client)
|
||||
c.Client = new(http.Client)
|
||||
c.BaseURL = strings.TrimSuffix(baseurl, "/")
|
||||
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/public/authenticate", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName,
|
||||
"Basic "+base64.StdEncoding.EncodeToString([]byte(domainEmailPassword)))
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
|
||||
msg, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(msg, &c.Auth)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(msg)) + " : " + err.Error())
|
||||
}
|
||||
|
||||
if err = c.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.Domain = strings.Split(domainEmailPassword, ":")[0]
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Validate the current user credentials.
|
||||
func (c *Client) Validate() error {
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/public/validate", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Authorization", c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
var um entity.User
|
||||
msg, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(msg, &um)
|
||||
if err != nil {
|
||||
return errors.New(string(msg) + " : " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
209
sdk/document.go
Normal file
209
sdk/document.go
Normal file
|
@ -0,0 +1,209 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// DeleteDocument removes the given document from the Documize database.
|
||||
func (c *Client) DeleteDocument(documentID string) error {
|
||||
req, err := http.NewRequest("DELETE",
|
||||
c.BaseURL+"/api/documents/"+documentID,
|
||||
nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if string(b) == emptyBraces {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New(string(b))
|
||||
}
|
||||
|
||||
// GetDocument gets the document information.
|
||||
func (c *Client) GetDocument(documentID string) (*entity.Document, error) {
|
||||
req, err := http.NewRequest("GET",
|
||||
c.BaseURL+"/api/documents/"+documentID,
|
||||
nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var dm entity.Document
|
||||
err = json.Unmarshal(b, &dm)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return &dm, nil
|
||||
}
|
||||
|
||||
// UpdateDocument updates document information obtained from GetDocument.
|
||||
func (c *Client) UpdateDocument(document *entity.Document) error {
|
||||
if document == nil {
|
||||
return errors.New("nil document passed to UpdateDocument")
|
||||
}
|
||||
req, err := http.NewRequest("PUT",
|
||||
c.BaseURL+"/api/documents/"+document.RefID,
|
||||
nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
buf, err := json.Marshal(document)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
_, err = ioutil.ReadAll(resp.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetDocumentMeta gets the metadata for a document.
|
||||
func (c *Client) GetDocumentMeta(documentID string) (*entity.DocumentMeta, error) {
|
||||
req, err := http.NewRequest("GET",
|
||||
c.BaseURL+"/api/documents/"+documentID+"/meta",
|
||||
nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var dm entity.DocumentMeta
|
||||
err = json.Unmarshal(b, &dm)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return &dm, nil
|
||||
}
|
||||
|
||||
// GetDocumentsByFolder returns a slice of document information for a given folder.
|
||||
func (c *Client) GetDocumentsByFolder(folderID string) ([]entity.Document, error) {
|
||||
req, err := http.NewRequest("GET",
|
||||
c.BaseURL+"/api/documents?folder="+folderID,
|
||||
nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var dm []entity.Document
|
||||
err = json.Unmarshal(b, &dm)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return dm, nil
|
||||
}
|
||||
|
||||
// SearchDocuments returns a list of documements which contain the supplied keywords.
|
||||
// TODO explain the format of the keywords string (when not just the single word sought).
|
||||
func (c *Client) SearchDocuments(keywords string) ([]entity.DocumentSearch, error) {
|
||||
req, err := http.NewRequest("GET",
|
||||
c.BaseURL+"/api/search?keywords="+url.QueryEscape(keywords),
|
||||
nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var ds []entity.DocumentSearch
|
||||
err = json.Unmarshal(b, &ds)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return ds, nil
|
||||
}
|
||||
|
||||
// GetDocumentAsDocx returns a file-name and content for the given documentID.
|
||||
// TODO allow the selection of either HTML or DOCX format.
|
||||
func (c *Client) GetDocumentAsDocx(documentID string) (string, []byte, error) {
|
||||
req, err := http.NewRequest("GET",
|
||||
c.BaseURL+"/api/documents/"+documentID+"/export",
|
||||
nil)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
filename := resp.Header.Get("Content-Disposition")
|
||||
filename = strings.TrimSpace(strings.TrimPrefix(filename, "attachment; filename="))
|
||||
if len(filename) == 0 {
|
||||
return "", nil, errors.New("Unknown document to download")
|
||||
}
|
||||
|
||||
return filename, b, nil
|
||||
}
|
86
sdk/documize/main.go
Normal file
86
sdk/documize/main.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
sdk "github.com/documize/community/sdk"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
flagSet := flag.NewFlagSet("documize client flags", flag.ExitOnError)
|
||||
|
||||
url, auth, folder, action := flagSet.String("api", os.Getenv("DOCUMIZEAPI"),
|
||||
"the url of the endpoint (defaults to environment variable DOCUMIZEAPI)"), //e.g. http://localhost:5002
|
||||
flagSet.String("auth", os.Getenv("DOCUMIZEAUTH"), //e.g. demo1:mick@jagger.com:demo123
|
||||
"the authorization credentials in the form domain:email:password (defaults to the environment variable DOCUMIZEAUTH)"),
|
||||
flagSet.String("folder", "", "the Documize folder to use"),
|
||||
flagSet.String("action", "load", "the Documize action to take")
|
||||
|
||||
if err := flagSet.Parse(os.Args[1:]); err != nil {
|
||||
fmt.Println("unable to parse Documize arguments:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if *url == "" {
|
||||
fmt.Println("Please set the environment variable DOCUMIZEAPI or use the -api flag")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if *auth == "" {
|
||||
fmt.Println("Please set the environment variable DOCUMIZEAUTH or use the -auth flag")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
c, e := sdk.NewClient(*url, *auth)
|
||||
if e != nil {
|
||||
fmt.Println("unable to create Documize SDK client for", *auth, "Error:", e)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
switch *action {
|
||||
case "load":
|
||||
folderID := checkFolder(c, folder)
|
||||
for _, arg := range flagSet.Args() {
|
||||
_, ce := c.LoadFile(folderID, arg)
|
||||
if ce == nil {
|
||||
fmt.Println("Loaded file " + arg + " into Documize folder " + *folder)
|
||||
} else {
|
||||
fmt.Println("Failed to load file " + arg + " into Documize folder " + *folder + " Error: " + ce.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkFolder(c *sdk.Client, folder *string) string {
|
||||
if *folder == "" {
|
||||
*folder = os.Getenv("DOCUMIZEFOLDER")
|
||||
if *folder == "" {
|
||||
fmt.Println("Please set the environment variable DOCUMIZEFOLDER or use the -folder flag")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
fids, err := c.GetNamedFolderIDs(*folder)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading folder IDs: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
if len(fids) != 1 {
|
||||
fmt.Println("There is no single folder called: " + *folder)
|
||||
os.Exit(1)
|
||||
}
|
||||
return fids[0]
|
||||
}
|
27
sdk/errors.go
Normal file
27
sdk/errors.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
errPfx = "{Error: '"
|
||||
errSfx = "'}"
|
||||
)
|
||||
|
||||
func trimErrors(e string) string {
|
||||
return strings.TrimPrefix(strings.TrimSuffix(e, errSfx), errPfx)
|
||||
}
|
||||
|
||||
func isError(e string) bool {
|
||||
return strings.HasPrefix(e, errPfx)
|
||||
}
|
40
sdk/exttest/apibenchmark.go
Normal file
40
sdk/exttest/apibenchmark.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
)
|
||||
|
||||
// APIbenchmark is the main entry point for the benchmark code, it is called by both internal and external tests.
|
||||
func APIbenchmark() error {
|
||||
testEndPt := os.Getenv("DOCUMIZEAPI") //e.g. "http://localhost:5002"
|
||||
testAuth := os.Getenv("DOCUMIZEAUTH") //e.g. "demo1:jim@davidson.com:demo123"
|
||||
c, err := documize.NewClient(testEndPt, testAuth) // should work
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
folders, err := c.GetNamedFolderIDs("Test")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(folders) == 0 {
|
||||
return errors.New("no Test folder for test user")
|
||||
}
|
||||
testFolder := folders[0]
|
||||
_ = testFolder
|
||||
// TODO add benchmark code
|
||||
return nil
|
||||
}
|
339
sdk/exttest/apitest.go
Normal file
339
sdk/exttest/apitest.go
Normal file
|
@ -0,0 +1,339 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
"github.com/documize/community/sdk"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testFileName string
|
||||
|
||||
func setupTestFile() error {
|
||||
testFileName = os.TempDir()
|
||||
if !strings.HasSuffix(testFileName, string(os.PathSeparator)) {
|
||||
testFileName += string(os.PathSeparator)
|
||||
}
|
||||
testFileName += "TESTDATA.html"
|
||||
return ioutil.WriteFile(testFileName, []byte(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>TESTDATA Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>TESTDATA Heading</h1>
|
||||
<p>TESTDATA paragraph.</p>
|
||||
</body>
|
||||
</html>
|
||||
`), os.ModePerm)
|
||||
}
|
||||
|
||||
// APItest is the main entry point for the test code, it is called by both internal and external tests.
|
||||
func APItest(t *testing.T) {
|
||||
setupTestFile()
|
||||
defer os.Remove(testFileName) // ignore error
|
||||
|
||||
c, err := auth(t)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
if c == nil {
|
||||
t.Error("unable to log-in to Documize, nil pointer returned")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
t.Logf("INFO: Auth client=%#v\n", *c)
|
||||
|
||||
testMeta(t, c)
|
||||
|
||||
// create a testing folder here, with a defer to remove it
|
||||
myFolder := &entity.Label{
|
||||
Name: "Folder created during Go unit tests",
|
||||
// OrgID is set by the endpoint
|
||||
UserID: c.Auth.User.RefID,
|
||||
Type: 2, // only the user can see it
|
||||
}
|
||||
err = c.AddFolder(myFolder)
|
||||
if err != nil {
|
||||
t.Error("c.AddFolder():", err)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
// find a folder to move the deleted myFolder's contents to
|
||||
fids, err := c.GetNamedFolderIDs("Test")
|
||||
if err != nil {
|
||||
t.Error("c.GetNamedFolderIDs():", err)
|
||||
t.Fail()
|
||||
}
|
||||
if len(fids) == 0 {
|
||||
t.Error("can't find a folder named Test")
|
||||
t.Fail()
|
||||
}
|
||||
reserveFolder := fids[0] // use the 1st we found
|
||||
defer func() {
|
||||
err = c.RemoveFolder(myFolder.RefID, reserveFolder)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
testFolder := myFolder.RefID
|
||||
if testFolder == "" {
|
||||
t.Error("myFolder.RefID is empty!")
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
t.Log("INFO: myFolder.RefID='" + testFolder + "'")
|
||||
|
||||
testFile := loadFile(c, t, testFolder, testFileName)
|
||||
testData := loadData(c, t, testFolder)
|
||||
|
||||
testPages(t, c, testFolder, testFile, testData)
|
||||
|
||||
testFolderAPI(t, c, testFolder, testFile)
|
||||
testSearch(t, c, testFolder, testFile)
|
||||
testDownload(t, c, testFolder, testFile)
|
||||
|
||||
testGetUpdDocument(t, c, testFile, testData)
|
||||
testGetDocumentMeta(t, c, testFile, testData)
|
||||
testDocAttachments(t, c, testFile, testData)
|
||||
testTemplates(t, c, testFolder, testFile, testData)
|
||||
testDelete(t, c, testFile, testData)
|
||||
|
||||
testOrg(t, c)
|
||||
testFolders(t, c, myFolder)
|
||||
testUsers(t, c)
|
||||
|
||||
}
|
||||
|
||||
func testOrg(t *testing.T, c *documize.Client) {
|
||||
orgs, err := c.GetOrganizations()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("INFO: organizations = %#v", orgs)
|
||||
}
|
||||
if len(orgs) > 0 {
|
||||
err = c.UpdateOrganization(&orgs[0])
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
// TODO actually check that the update did what we expected
|
||||
}
|
||||
}
|
||||
|
||||
func testGetUpdDocument(t *testing.T, c *documize.Client, testFile, testData string) {
|
||||
var err error
|
||||
doc, err := c.GetDocument(testFile)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if doc.RefID != testFile {
|
||||
t.Error("wrong RefId got", doc.RefID, "want", testFile)
|
||||
}
|
||||
}
|
||||
|
||||
err = c.UpdateDocument(doc)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
// NOTE updates to unknown documents do not generate errors
|
||||
|
||||
docData, err := c.GetDocument(testData)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if docData.BaseEntity.RefID != testData {
|
||||
t.Error("wrong RefID want", testData, "got", docData.RefID)
|
||||
}
|
||||
if _, err = c.GetDocument(""); err == nil {
|
||||
t.Error("GetDocument did not error on empty DocumentID")
|
||||
} else {
|
||||
t.Log("INFO: GetDocument emptyDocID msg:", err)
|
||||
}
|
||||
if _, err = c.GetDocument("XXXXXXXXXXXXX"); err == nil {
|
||||
t.Error("GetDocument did not error on bad DocumentID")
|
||||
} else {
|
||||
t.Log("INFO: GetDocument badDocID msg:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func testGetDocumentMeta(t *testing.T, c *documize.Client, testFile, testData string) {
|
||||
var err error
|
||||
docM, err := c.GetDocumentMeta(testFile)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if len(docM.Editors) < 2 { // TODO review
|
||||
t.Error("wrong number of editors expected >=2 got", len(docM.Editors))
|
||||
}
|
||||
}
|
||||
docDataM, err := c.GetDocumentMeta(testData)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if len(docDataM.Editors) != 2 { // TODO review
|
||||
t.Error("wrong number of editors expected 0 got", len(docDataM.Editors))
|
||||
}
|
||||
}
|
||||
if _, err = c.GetDocumentMeta(""); err == nil {
|
||||
t.Error("GetDocumentMeta did not error on empty DocumentID")
|
||||
} else {
|
||||
t.Log("INFO: GetDocumentMeta emptyDocID msg:", err)
|
||||
}
|
||||
/* TODO reivew
|
||||
if _, err = c.GetDocumentMeta("XXXXXXXXXXXXX"); err == nil {
|
||||
t.Error("GetDocumentMeta did not error on bad DocumentID")
|
||||
} else {
|
||||
t.Log("INFO: GetDocumentMeta badDocID msg:", err)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func testDelete(t *testing.T, c *documize.Client, testFile, testData string) {
|
||||
var err error
|
||||
if err = c.DeleteDocument(testFile); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = c.DeleteDocument(testData); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err = c.DeleteDocument(""); err == nil {
|
||||
t.Error("DeleteDocument did not error on empty DocumentID")
|
||||
} else {
|
||||
t.Log("INFO: Delete Doc emptyDocID msg:", err)
|
||||
}
|
||||
/* TODO reivew
|
||||
if err = c.DeleteDocument("XXXXXXXXXXXXX"); err == nil {
|
||||
t.Error("DeleteDocument did not error on bad DocumentID")
|
||||
} else {
|
||||
t.Log("Delete Doc badDocID msg:", err)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func testMeta(t *testing.T, c *documize.Client) {
|
||||
sitemap, err := c.GetSitemap()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if sitemap == nil {
|
||||
t.Error("sitemap []byte is nil")
|
||||
} else {
|
||||
if !bytes.Contains(sitemap, []byte("http://www.sitemaps.org/schemas/sitemap")) {
|
||||
t.Errorf("Incorrect Site Map: %#v", string(sitemap))
|
||||
}
|
||||
}
|
||||
}
|
||||
robots, err := c.GetRobots()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if robots == nil {
|
||||
t.Error("robots []byte is nil")
|
||||
} else {
|
||||
if !bytes.HasPrefix(robots, []byte("User-agent:")) {
|
||||
t.Errorf("Incorrect Robots data: %#v", string(robots))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testFolderAPI(t *testing.T, c *documize.Client, testFolder, testFile string) {
|
||||
|
||||
_, err := c.GetDocumentsByFolder("")
|
||||
if err == nil {
|
||||
t.Error("did not error on blank folder ID")
|
||||
} else {
|
||||
t.Log("INFO: ", err)
|
||||
}
|
||||
|
||||
docs, err := c.GetDocumentsByFolder(testFolder)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
for _, doc := range docs {
|
||||
if doc.RefID == testFile {
|
||||
goto foundDoc
|
||||
}
|
||||
}
|
||||
t.Error("Unable to find " + testFile + " in the test folder")
|
||||
return
|
||||
foundDoc:
|
||||
|
||||
docs2, err := c.GetDocumentsByFolder("什么都没有")
|
||||
if err == nil {
|
||||
t.Error("did not error on bad folder ID for GetDocumentsByFolder")
|
||||
if len(docs2) != 0 {
|
||||
t.Error("found documents in folder where there should be none")
|
||||
}
|
||||
} else {
|
||||
t.Log("INFO: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func testSearch(t *testing.T, c *documize.Client, testFolder, testFile string) {
|
||||
|
||||
// NOTE: search does not error on blank search term
|
||||
srch, err := c.SearchDocuments("TESTDATA")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
for _, doc := range srch {
|
||||
//t.Logf("DEBUG search %#v", doc)
|
||||
if doc.DocumentID == testFile {
|
||||
goto foundSrch
|
||||
}
|
||||
}
|
||||
t.Error("Unable to find " + testFileName + " in the search list")
|
||||
foundSrch:
|
||||
|
||||
srch2, err := c.SearchDocuments("石磊先生是谁?")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(srch2) != 0 {
|
||||
t.Error("found documents in search where there should be none")
|
||||
}
|
||||
}
|
||||
|
||||
func testDownload(t *testing.T, c *documize.Client, testFolder, testFile string) {
|
||||
|
||||
nam, content, err := c.GetDocumentAsDocx(testFile)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
/* TODO
|
||||
if !strings.HasPrefix(nam, "addpage") || len(content) < 10000 {
|
||||
t.Error(" docAsDocx not as expected", nam, len(content))
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
nam, content, err = c.GetDocumentAsDocx("XXXXXXXXXX")
|
||||
if err == nil {
|
||||
t.Errorf("did not error on bad document id for get doc as docx, name: %s len(%d)",
|
||||
nam, len(content))
|
||||
} else {
|
||||
t.Log("INFO: get doc as docx bad doc id msg:", err)
|
||||
}
|
||||
|
||||
}
|
107
sdk/exttest/attachment.go
Normal file
107
sdk/exttest/attachment.go
Normal file
|
@ -0,0 +1,107 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
)
|
||||
|
||||
func testDocAttachments(t *testing.T, c *documize.Client, testFile, testData string) {
|
||||
atts, err := c.GetAttachments(testData)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
for a := range atts {
|
||||
err = c.GetAttachmentData(&atts[a])
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if atts[a].Filename == "test.txt" {
|
||||
if string(atts[a].Data) != "This is a test text file.\n" {
|
||||
t.Error("incorrect content to attachment")
|
||||
}
|
||||
goto foundAtt
|
||||
}
|
||||
//t.Logf("DEBUG %d atts= %#v ; err=%v; data=%s", a, atts[a], err,string(atts[a].Data))
|
||||
}
|
||||
t.Error("Attachment test.txt not found")
|
||||
foundAtt:
|
||||
dingbat := "dingbat\n"
|
||||
err = c.AddAttachment(testFile, "dingbat.txt", []byte(dingbat))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
atts, err = c.GetAttachments(testFile)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if len(atts) != 1 {
|
||||
t.Error("should be exactly 1 attachment")
|
||||
} else {
|
||||
err = c.GetAttachmentData(&atts[0])
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if string(atts[0].Data) != dingbat {
|
||||
t.Error("Wrong data in attachement")
|
||||
}
|
||||
err = c.DeleteAttachment(&atts[0])
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
atts, err = c.GetAttachments(testFile)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if len(atts) != 0 {
|
||||
t.Error("should be no attachments")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// errors
|
||||
atts, err = c.GetAttachments("XXXXXXX")
|
||||
if len(atts) != 0 {
|
||||
if err == nil {
|
||||
t.Error("Get attachments of unknown file did not error")
|
||||
} else {
|
||||
t.Log("INFO: get attachments of unknown file msg:", err)
|
||||
}
|
||||
}
|
||||
/* TODO improve failure modes
|
||||
att := &entity.Attachment{}
|
||||
err = c.GetAttachmentData(att)
|
||||
if len(att.Data) > 0 {
|
||||
if err == nil {
|
||||
t.Error("Get attachment data of blank file did not error")
|
||||
} else {
|
||||
t.Log("INFO: get attachments of blank file msg:", err)
|
||||
}
|
||||
}
|
||||
err = c.AddAttachment("YYYYYYYYYYYY", "dingbat.txt", []byte(dingbat))
|
||||
if err != nil {
|
||||
t.Error("Did not error adding attachment to bad file id")
|
||||
} else {
|
||||
t.Log("INFO: add attachment to unknown file msg:", err)
|
||||
}
|
||||
err = c.DeleteAttachment(&entity.Attachment{})
|
||||
if err != nil {
|
||||
t.Error("Did not error deleting attachment of blank data")
|
||||
} else {
|
||||
t.Log("INFO: delete attachment to blank file msg:", err)
|
||||
}
|
||||
*/
|
||||
}
|
98
sdk/exttest/auth.go
Normal file
98
sdk/exttest/auth.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
)
|
||||
|
||||
// auth provides authorization tests to be run locally or from the main Documize repo.
|
||||
func auth(t *testing.T) (*documize.Client, error) {
|
||||
|
||||
testEndPt := os.Getenv("DOCUMIZEAPI") //e.g. "http://localhost:5002"
|
||||
testAuth := os.Getenv("DOCUMIZEAUTH") //e.g. "demo1:jim@davidson.com:demo123"
|
||||
|
||||
testCreds(t, testEndPt, testAuth)
|
||||
testEndpoint(t, testEndPt, testAuth)
|
||||
|
||||
//t.Log("Auth", testEndPt, testAuth)
|
||||
|
||||
c, err := documize.NewClient(testEndPt, testAuth) // should work
|
||||
if err == nil && c == nil {
|
||||
err = errors.New("unable to authorize, new client nil")
|
||||
}
|
||||
return c, err
|
||||
}
|
||||
|
||||
func testCreds(t *testing.T, testEndPt, testAuth string) {
|
||||
_, err := documize.NewClient(testEndPt, "")
|
||||
if err == nil {
|
||||
t.Error("ExtTestAuth did not error on empty auth string ")
|
||||
} else {
|
||||
t.Log("INFO: Empty Auth string error:", err)
|
||||
}
|
||||
_, err = documize.NewClient(testEndPt, "AAA:BBB")
|
||||
if err == nil {
|
||||
t.Error("ExtTestAuth did not error on AAA:BBB auth string ")
|
||||
} else {
|
||||
t.Log("INFO: Malfomed auth string error:", err)
|
||||
}
|
||||
credentials := strings.SplitN(testAuth, ":", 3)
|
||||
if len(credentials) == 3 {
|
||||
base := []string{"XXX", "YYY", "ZZZ"}
|
||||
for i := range credentials {
|
||||
ta := make([]string, 3)
|
||||
copy(ta, base)
|
||||
for j := range ta {
|
||||
if j != i { // make sure one of the three is wrong
|
||||
ta[j] = credentials[j]
|
||||
}
|
||||
}
|
||||
as := strings.Join(ta, ":")
|
||||
//t.Log(as)
|
||||
if credentials[i] != "" { // to avoid the case where the sub-domain is empty
|
||||
_, err = documize.NewClient(testEndPt, as)
|
||||
if err == nil {
|
||||
t.Error("ExtTestAuth did not error on bad auth string: ", as)
|
||||
} else {
|
||||
t.Log("INFO: Bad component to auth string error:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testEndpoint(t *testing.T, testEndPt, testAuth string) {
|
||||
_, err := documize.NewClient("", testAuth)
|
||||
if err == nil {
|
||||
t.Error("ExtTestAuth did not error on empty end point")
|
||||
} else {
|
||||
t.Log("INFO: Empty end-point error:", err)
|
||||
}
|
||||
_, err = documize.NewClient("XXXXX", testAuth)
|
||||
if err == nil {
|
||||
t.Error("ExtTestAuth did not error on bad end point")
|
||||
} else {
|
||||
t.Log("INFO: Bad end point error:", err)
|
||||
}
|
||||
_, err = documize.NewClient("http://XXXXXYYYYYYZZZZZZ.com", testAuth)
|
||||
if err == nil {
|
||||
t.Error("ExtTestAuth did not error on invalid end point")
|
||||
} else {
|
||||
t.Log("INFO: Invalid end point error:", err)
|
||||
}
|
||||
}
|
57
sdk/exttest/folders.go
Normal file
57
sdk/exttest/folders.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
func testFolders(t *testing.T, c *documize.Client, myFolder *entity.Label) {
|
||||
|
||||
perms, err := c.GetFolderPermissions(myFolder.RefID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
//t.Logf("INFO: folder perms %#v", *perms)
|
||||
}
|
||||
err = c.SetFolderPermissions(myFolder.RefID, "test message", perms)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
//t.Logf("INFO: set empty folder perms")
|
||||
}
|
||||
|
||||
fv, err := c.GetFoldersVisibility()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
//t.Logf("INFO: folder vis %#v", fv)
|
||||
_ = fv
|
||||
}
|
||||
|
||||
myFolder.Name += " - Modified"
|
||||
err = c.UpdateFolder(myFolder)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
fi, err := c.GetFolder(myFolder.RefID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
//t.Logf("INFO: folder info %#v", fi)
|
||||
_ = fi
|
||||
}
|
||||
}
|
92
sdk/exttest/loaddata.go
Normal file
92
sdk/exttest/loaddata.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
"github.com/documize/community/wordsmith/api"
|
||||
)
|
||||
|
||||
// loadData provides data-loading tests to be run locally or from the main Documize repo.
|
||||
func loadData(c *documize.Client, t *testing.T, testFolder string) string {
|
||||
ret, err := c.LoadData(testFolder, "LoadDataTest", &api.DocumentConversionResponse{
|
||||
//Err error
|
||||
PagesHTML: []byte{}, // If empty, use Pages
|
||||
Pages: []api.Page{
|
||||
{
|
||||
Level: 1, // uint64 // overall document is level 1, <H1> => level 2
|
||||
Title: "Test Data Title top", // string
|
||||
Body: []byte("This is the body of page 0"), // []byte
|
||||
},
|
||||
{
|
||||
Level: 2, // uint64 // overall document is level 1, <H1> => level 2
|
||||
Title: "Test Data Title second", // string
|
||||
Body: []byte("This is the body of page 1"), // []byte
|
||||
},
|
||||
},
|
||||
EmbeddedFiles: []api.EmbeddedFile{
|
||||
{
|
||||
ID: "TestID1",
|
||||
Type: "txt",
|
||||
Name: "test.txt", // do not change, used in exttest
|
||||
Data: []byte("This is a test text file.\n"), // do not change, used in exttest
|
||||
},
|
||||
{
|
||||
ID: "TestID2",
|
||||
Type: "go",
|
||||
Name: "blob.go",
|
||||
Data: []byte("// Package blob is a test go file.\npackage blob\n"),
|
||||
},
|
||||
},
|
||||
Excerpt: "Ext Test Load Data - Excerpt",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = c.LoadData(testFolder, "LoadDataTest", &api.DocumentConversionResponse{
|
||||
//Err error
|
||||
PagesHTML: []byte{}, // If empty, use Pages
|
||||
Pages: []api.Page{
|
||||
{
|
||||
Level: 1, // overall document is level 1, <H1> => level 2
|
||||
Title: "Test Data Title top",
|
||||
Body: []byte("This is the body of page 0"),
|
||||
},
|
||||
},
|
||||
EmbeddedFiles: []api.EmbeddedFile{
|
||||
{
|
||||
ID: "TestID1",
|
||||
Type: "txt",
|
||||
Name: "test", // wrong, does not have correct extension
|
||||
Data: []byte("This is a test text file.\n"),
|
||||
},
|
||||
},
|
||||
Excerpt: "Ext Test Load Data - Excerpt",
|
||||
})
|
||||
if err == nil {
|
||||
t.Error("data load did not error on bad embedded file name")
|
||||
} else {
|
||||
t.Log("INFO: Bad embedded file name error:", err)
|
||||
}
|
||||
|
||||
_, err = c.LoadData(testFolder, "LoadDataTest", &api.DocumentConversionResponse{})
|
||||
if err == nil {
|
||||
t.Error("data load did not error on no pages ")
|
||||
} else {
|
||||
t.Log("INFO: No pages error:", err)
|
||||
}
|
||||
|
||||
return ret.BaseEntity.RefID
|
||||
}
|
45
sdk/exttest/loadfile.go
Normal file
45
sdk/exttest/loadfile.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
)
|
||||
|
||||
// loadFile provides file load tests to be run locally or from the main Documize repo.
|
||||
func loadFile(c *documize.Client, t *testing.T, testFolder, testFileName string) string {
|
||||
docID := "" //NOT-FOUND
|
||||
ret, err := c.LoadFile(testFolder, testFileName)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if ret == nil {
|
||||
t.Error("nil pointer returned for LoadFile")
|
||||
} else {
|
||||
docID = ret.BaseEntity.RefID
|
||||
}
|
||||
_, err = c.LoadFile("XXX", testFileName)
|
||||
if err == nil {
|
||||
t.Error("did not error on bad folder")
|
||||
} else {
|
||||
t.Log("INFO: Bad folder error:", err)
|
||||
}
|
||||
_, err = c.LoadFile(testFolder, "XXX")
|
||||
if err == nil {
|
||||
t.Error("did not error on bad file name")
|
||||
} else {
|
||||
t.Log("INFO: Bad file name error:", err)
|
||||
}
|
||||
return docID
|
||||
}
|
132
sdk/exttest/pages.go
Normal file
132
sdk/exttest/pages.go
Normal file
|
@ -0,0 +1,132 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/documize/api/endpoint/models"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
"github.com/documize/community/sdk"
|
||||
)
|
||||
|
||||
func testPages(t *testing.T, c *documize.Client, testFolder, testFile, testData string) {
|
||||
|
||||
lastSeq := 0.0
|
||||
dp, err := c.GetDocumentPages(testFile)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
var pageIDs []string
|
||||
for _, v := range dp {
|
||||
pageIDs = append(pageIDs, v.RefID)
|
||||
if v.Sequence > lastSeq {
|
||||
lastSeq = v.Sequence
|
||||
}
|
||||
}
|
||||
pageIDlist := strings.Join(pageIDs, ",")
|
||||
if dpb, err2 := c.GetDocumentPagesBatch(testFile, pageIDlist); err != nil {
|
||||
t.Error(err2)
|
||||
} else {
|
||||
for k, v := range dp {
|
||||
if v.Body != dpb[k].Body {
|
||||
t.Errorf("page %d from GetDocumentPages != from GetDocumentPagesBatch", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const numPages = 100
|
||||
pagesAdded := make([]*entity.Page, 0, numPages)
|
||||
for i := 0; i < numPages; i++ {
|
||||
lastSeq += 1000.0
|
||||
pg := entity.Page{
|
||||
DocumentID: testFile,
|
||||
Level: 1,
|
||||
Title: "AddPage() title " + fmt.Sprintf("%d", i),
|
||||
Body: "AddPage() body " + fmt.Sprintf("%d", i),
|
||||
ContentType: "wysiwyg",
|
||||
Sequence: lastSeq,
|
||||
}
|
||||
if newPg, err2 := c.AddDocumentPage(testFile, &pg); err != nil {
|
||||
t.Error(err2)
|
||||
} else {
|
||||
pagesAdded = append(pagesAdded, newPg)
|
||||
}
|
||||
}
|
||||
|
||||
if len(pagesAdded) < 2 {
|
||||
t.Error("Less than two pages added, cannot continue with pages test")
|
||||
return
|
||||
}
|
||||
|
||||
err = c.ChangeDocumentPageLevel(testFile, &[]models.PageLevelRequestModel{
|
||||
{PageID: pagesAdded[0].RefID, Level: int(pagesAdded[0].Level + 42)},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = c.ChangeDocumentPageSequence(testFile, &[]models.PageSequenceRequestModel{
|
||||
{PageID: pagesAdded[0].RefID, Sequence: pagesAdded[0].Sequence - 1.0},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
pagesAdded[0].Body += " - Modified!"
|
||||
err = c.UpdateDocumentPage(pagesAdded[0])
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
revs, err := c.GetDocumentPageRevisions(testFile, pagesAdded[0].RefID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
diff, err2 := c.GetDocumentPageDiff(testFile, pagesAdded[0].RefID, revs[0].RefID)
|
||||
if err2 != nil {
|
||||
t.Error(err2)
|
||||
} else {
|
||||
t.Logf("INFO: Revised single doc page diff: %s", string(diff))
|
||||
}
|
||||
err = c.RollbackDocumentPage(testFile, pagesAdded[0].RefID, revs[0].RefID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("INFO: Rolled-back revised single doc page %s", revs[0].RefID)
|
||||
}
|
||||
}
|
||||
|
||||
err = c.DeleteDocumentPage(testFile, pagesAdded[0].RefID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Log("INFO: Deleted single doc page", pagesAdded[0].RefID)
|
||||
}
|
||||
|
||||
delList := []string{pagesAdded[1].RefID}
|
||||
for k, v := range pagesAdded {
|
||||
if k > 1 {
|
||||
delList = append(delList, v.RefID)
|
||||
}
|
||||
}
|
||||
err = c.DeleteDocumentPages(testFile, delList)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Log("INFO: Deleted multiple doc pages:", len(delList))
|
||||
}
|
||||
|
||||
}
|
64
sdk/exttest/templates.go
Normal file
64
sdk/exttest/templates.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
)
|
||||
|
||||
func testTemplates(t *testing.T, c *documize.Client, testFolder, testFile, testData string) {
|
||||
|
||||
temps, err := c.GetTemplates(true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if len(temps) == 0 {
|
||||
t.Log("INFO: no stock templates found in the database")
|
||||
} else {
|
||||
t.Logf("INFO: testing with stock template %#v", temps[0])
|
||||
docID, errStart := c.StartDocumentFromTemplate(true, temps[0].ID, testFolder)
|
||||
if errStart != nil {
|
||||
t.Error(errStart)
|
||||
} else {
|
||||
t.Log("INFO: created document", docID)
|
||||
err = c.DeleteDocument(docID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
temps, err = c.GetTemplates(false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
if len(temps) == 0 {
|
||||
t.Log("INFO: no saved templates found in the database")
|
||||
} else {
|
||||
t.Logf("INFO: testing with saved template %#v", temps[0])
|
||||
docID, err := c.StartDocumentFromTemplate(false, temps[0].ID, testFolder)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Log("INFO: created document", docID)
|
||||
err = c.DeleteDocument(docID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
63
sdk/exttest/users.go
Normal file
63
sdk/exttest/users.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package exttest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/documize/community/sdk"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
func testUsers(t *testing.T, c *documize.Client) {
|
||||
|
||||
usrs, err := c.GetUsers()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("INFO: users info %#v", usrs)
|
||||
}
|
||||
usr, err := c.GetUserInfo()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("INFO: this user info %#v", usr)
|
||||
}
|
||||
perms, err := c.GetUserFolderPermissions()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("INFO: testing user folder perms %#v", len(*perms))
|
||||
}
|
||||
|
||||
testUser := &entity.User{
|
||||
Firstname: "TestFirstname",
|
||||
Lastname: "TestLastname",
|
||||
Email: "tt@a.b",
|
||||
Admin: false,
|
||||
}
|
||||
err = c.AddUser(testUser)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
t.Logf("INFO: created test user %#v", *testUser)
|
||||
err = c.UpdateUser(testUser)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = c.DeleteUser(testUser.BaseEntity.RefID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
251
sdk/folders.go
Normal file
251
sdk/folders.go
Normal file
|
@ -0,0 +1,251 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/documize/api/endpoint/models"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// GetFolders returns the folders that the current user can see.
|
||||
func (c *Client) GetFolders() ([]entity.Label, error) {
|
||||
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
|
||||
var folders []entity.Label
|
||||
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&folders)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return folders, nil
|
||||
}
|
||||
|
||||
// GetNamedFolderIDs returns those folder IDs with the given name (folder names are not unique).
|
||||
func (c *Client) GetNamedFolderIDs(name string) ([]string, error) {
|
||||
ret := make([]string, 0, 2)
|
||||
|
||||
Folders, err := c.GetFolders()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, f := range Folders {
|
||||
if name == f.Name {
|
||||
ret = append(ret, f.RefID)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// GetFoldersVisibility returns the visibility of folders that the current user can see.
|
||||
func (c *Client) GetFoldersVisibility() ([]entity.Label, error) {
|
||||
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders?filter=viewers", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
|
||||
folders := make([]entity.Label, 0, 10)
|
||||
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&folders)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return folders, nil
|
||||
}
|
||||
|
||||
// GetFolder returns the documents in the given folder that the current user can see.
|
||||
func (c *Client) GetFolder(folderID string) (*entity.Label, error) {
|
||||
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders/"+folderID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
|
||||
var folder entity.Label
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
err = json.Unmarshal(b, &folder)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &folder, nil
|
||||
}
|
||||
|
||||
// GetFolderPermissions returns the given user's permissions.
|
||||
func (c *Client) GetFolderPermissions(folderID string) (*[]entity.LabelRole, error) {
|
||||
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders/"+folderID+"/permissions", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
|
||||
folderPerm := make([]entity.LabelRole, 0, 6)
|
||||
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&folderPerm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &folderPerm, nil
|
||||
}
|
||||
|
||||
// SetFolderPermissions sets the given user's permissions.
|
||||
func (c *Client) SetFolderPermissions(folderID, msg string, perms *[]entity.LabelRole) error {
|
||||
frm := new(models.FolderRolesModel)
|
||||
frm.Message = msg
|
||||
frm.Roles = *perms
|
||||
b, err := json.Marshal(frm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/folders/"+folderID+"/permissions", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddFolder adds the given folder for the current user.
|
||||
// Fields added by the host are added to the folder structure referenced.
|
||||
func (c *Client) AddFolder(fldr *entity.Label) error {
|
||||
b, err := json.Marshal(fldr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/folders", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
err = json.Unmarshal(b, fldr)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateFolder changes the folder info the given folder for the current user, returning the changed version in the referenced folder structure.
|
||||
func (c *Client) UpdateFolder(fldr *entity.Label) error {
|
||||
b, err := json.Marshal(fldr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/folders/"+fldr.RefID, bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
err = json.Unmarshal(b, fldr)
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveFolder removes the given folder and moves its contents to another.
|
||||
func (c *Client) RemoveFolder(folderID, moveToID string) error {
|
||||
req, err := http.NewRequest("DELETE", c.BaseURL+"/api/folders/"+folderID+"/move/"+moveToID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return nil
|
||||
}
|
47
sdk/loaddata.go
Normal file
47
sdk/loaddata.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/wordsmith/api"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// LoadData uploads and converts the raw data comprising a Documize document into Documize, returning a fileID and error.
|
||||
func (c *Client) LoadData(folderID, docName string, docData *api.DocumentConversionResponse) (*entity.Document, error) {
|
||||
if len(docData.PagesHTML) == 0 && len(docData.Pages) == 0 {
|
||||
return nil, errors.New("no data to load") // NOTE attachements must have a base document
|
||||
}
|
||||
for _, att := range docData.EmbeddedFiles {
|
||||
if !strings.HasSuffix(strings.ToLower(att.Name), "."+strings.ToLower(att.Type)) {
|
||||
return nil, errors.New("attachment " + att.Name + " does not have the extention " + att.Type)
|
||||
}
|
||||
}
|
||||
buf, err := json.Marshal(*docData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cv, err := c.upload(folderID, docName+".documizeapi", bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//cv, err := c.convert(folderID, job, nil)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
return cv, nil
|
||||
}
|
131
sdk/loadfile.go
Normal file
131
sdk/loadfile.go
Normal file
|
@ -0,0 +1,131 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
func (c *Client) upload(folderID, fileName string, fileReader io.Reader) (*entity.Document, error) {
|
||||
|
||||
bodyBuf := &bytes.Buffer{}
|
||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
||||
|
||||
_, fn := path.Split(fileName)
|
||||
fileWriter, err := bodyWriter.CreateFormFile("attachment", fn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = io.Copy(fileWriter, fileReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
contentType := bodyWriter.FormDataContentType()
|
||||
err = bodyWriter.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST",
|
||||
c.BaseURL+"/api/import/folder/"+folderID,
|
||||
bodyBuf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
|
||||
var du entity.Document
|
||||
|
||||
msg, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(msg, &du)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(msg)))
|
||||
}
|
||||
|
||||
return &du, nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (c *Client) convert(folderID string, du *endpoint.DocumentUploadModel, cjr *api.ConversionJobRequest) (*endpoint.DocumentConversionModel, error) {
|
||||
|
||||
if cjr == nil {
|
||||
cjr = &api.ConversionJobRequest{}
|
||||
}
|
||||
|
||||
buf, err := json.Marshal(*cjr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST",
|
||||
c.BaseURL+"/api/convert/folder/"+folderID+"/"+du.JobID,
|
||||
bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
|
||||
var dc endpoint.DocumentConversionModel
|
||||
|
||||
msg, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(msg, &dc)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(msg)))
|
||||
}
|
||||
|
||||
return &dc, nil
|
||||
}
|
||||
*/
|
||||
|
||||
// LoadFile uploads and converts a file into Documize, returning a fileID and error.
|
||||
func (c *Client) LoadFile(folderID, targetFile string) (*entity.Document, error) {
|
||||
file, err := os.Open(targetFile) // For read access.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cv, err := c.upload(folderID, targetFile, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//cv, err := c.convert(folderID, job, nil)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
return cv, nil
|
||||
}
|
75
sdk/meta.go
Normal file
75
sdk/meta.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetSiteMeta returns the site information based on the sub-domain of the URL called.
|
||||
/* TODO - cant get sub-domain easily in test environment
|
||||
func (c *Client) GetSiteMeta() (*entity.SiteMeta, error) {
|
||||
tgt := c.BaseURL + "/api/public/meta"
|
||||
req, err := http.NewRequest("GET", tgt, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var sm entity.SiteMeta
|
||||
err = json.Unmarshal(b, &sm)
|
||||
if err != nil {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return &sm, nil
|
||||
}
|
||||
*/
|
||||
|
||||
// GetSitemap returns the site map information based on the domain supplied in the URL called.
|
||||
func (c *Client) GetSitemap() ([]byte, error) {
|
||||
tgt := c.BaseURL + "/sitemap.xml"
|
||||
return c.getFile(tgt)
|
||||
}
|
||||
|
||||
// GetRobots returns the site map information based on the domain supplied in the URL called.
|
||||
func (c *Client) GetRobots() ([]byte, error) {
|
||||
tgt := c.BaseURL + "/robots.txt"
|
||||
return c.getFile(tgt)
|
||||
}
|
||||
|
||||
// getFile is an internal function to avoid code duplication when fetching the plain output of the GET.
|
||||
func (c *Client) getFile(tgt string) ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", tgt, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
81
sdk/organization.go
Normal file
81
sdk/organization.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// GetOrganizations returns the user's organizations.
|
||||
func (c *Client) GetOrganizations() ([]entity.Organization, error) {
|
||||
ret := make([]entity.Organization, 0, len(c.Auth.User.Accounts))
|
||||
for orgNum := range c.Auth.User.Accounts {
|
||||
url := c.BaseURL + "/api/organizations/" + c.Auth.User.Accounts[orgNum].OrgID
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
var m entity.Organization
|
||||
err = json.Unmarshal(b, &m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret = append(ret, m)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// UpdateOrganization returns the user's organization information.
|
||||
func (c *Client) UpdateOrganization(org *entity.Organization) error {
|
||||
url := c.BaseURL + "/api/organizations/" + org.RefID
|
||||
bod, err := json.Marshal(org)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("PUT", url, bytes.NewReader(bod))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return nil
|
||||
}
|
287
sdk/pages.go
Normal file
287
sdk/pages.go
Normal file
|
@ -0,0 +1,287 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/documize/api/endpoint/models"
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// GetDocumentPages returns all the pages in a document.
|
||||
func (c *Client) GetDocumentPages(documentID string) ([]entity.Page, error) {
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/documents/"+documentID+"/pages", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
pages := make([]entity.Page, 0, 12)
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&pages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
// GetDocumentPagesBatch returns those pages in a document whose RefIDs are in a comma-separated list.
|
||||
func (c *Client) GetDocumentPagesBatch(documentID, pageIDlist string) ([]entity.Page, error) {
|
||||
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/batch", strings.NewReader(pageIDlist))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
pages := make([]entity.Page, 0, 12)
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&pages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
// AddDocumentPage adds the given page into the indicated document.
|
||||
func (c *Client) AddDocumentPage(documentID string, pg *entity.Page) (*entity.Page, error) {
|
||||
|
||||
pageJSON, err := json.Marshal(pg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages", bytes.NewReader(pageJSON))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
var page entity.Page
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&page)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &page, nil
|
||||
}
|
||||
|
||||
// DeleteDocumentPage deletes the given page from the indicated document.
|
||||
func (c *Client) DeleteDocumentPage(documentID, pageID string) error {
|
||||
req, err := http.NewRequest("DELETE", c.BaseURL+"/api/documents/"+documentID+"/pages/"+pageID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
res, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(res)) {
|
||||
return errors.New(trimErrors(string(res)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteDocumentPages deletes the given pageIDs in a slice from the indicated document.
|
||||
func (c *Client) DeleteDocumentPages(documentID string, pageIDlist []string) error {
|
||||
model := make([]models.PageLevelRequestModel, len(pageIDlist))
|
||||
for k := range pageIDlist {
|
||||
model[k].PageID = pageIDlist[k]
|
||||
}
|
||||
modelJSON, err := json.Marshal(&model)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/unused", bytes.NewReader(modelJSON))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
res, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(res)) {
|
||||
return errors.New(trimErrors(string(res)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateDocumentPage updates the given page from the indicated document.
|
||||
func (c *Client) UpdateDocumentPage(pg *entity.Page) error {
|
||||
pgJSON, err := json.Marshal(pg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/documents/"+pg.DocumentID+"/pages/"+pg.RefID, bytes.NewReader(pgJSON))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
res, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(res)) {
|
||||
return errors.New(trimErrors(string(res)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDocumentPageRevisions returns all the previous versions of a given page in a document.
|
||||
func (c *Client) GetDocumentPageRevisions(documentID, pageID string) ([]entity.Revision, error) {
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/documents/"+documentID+"/pages/"+pageID+"/revisions", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
revs := make([]entity.Revision, 0, 3)
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&revs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return revs, nil
|
||||
}
|
||||
|
||||
// GetDocumentPageDiff returns html showing the difference between the given page revision and the current version of
|
||||
// a given page in a document.
|
||||
func (c *Client) GetDocumentPageDiff(documentID, pageID, revID string) ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/documents/"+documentID+"/pages/"+pageID+"/revisions/"+revID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
diff, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
// RollbackDocumentPage reverts the given document page back to the chosen revision.
|
||||
func (c *Client) RollbackDocumentPage(documentID, pageID, revID string) error {
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/"+pageID+"/revisions/"+revID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
diff, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(diff)) {
|
||||
return errors.New(trimErrors(string(diff)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangeDocumentPageLevel sets the levels of the pages in the PageLevelRequestModel for the given document.
|
||||
func (c *Client) ChangeDocumentPageLevel(documentID string, plrm *[]models.PageLevelRequestModel) error {
|
||||
b, err := json.Marshal(plrm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/level", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
diff, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(diff)) {
|
||||
return errors.New(trimErrors(string(diff)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangeDocumentPageSequence sets the sequences of the pages in the PageSequenceRequestModel for the given document.
|
||||
func (c *Client) ChangeDocumentPageSequence(documentID string, psrm *[]models.PageSequenceRequestModel) error {
|
||||
b, err := json.Marshal(psrm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/sequence", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
diff, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(diff)) {
|
||||
return errors.New(trimErrors(string(diff)))
|
||||
}
|
||||
return nil
|
||||
}
|
91
sdk/templates.go
Normal file
91
sdk/templates.go
Normal file
|
@ -0,0 +1,91 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// GetTemplates returns the available templates; from the stock templates if useStock is set.
|
||||
func (c *Client) GetTemplates(useStock bool) (temps []entity.Template, err error) {
|
||||
url := fmt.Sprintf("%s/api/templates",
|
||||
c.BaseURL)
|
||||
if useStock {
|
||||
url += "/stock"
|
||||
}
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, errDo := c.Client.Do(req)
|
||||
if errDo != nil {
|
||||
return nil, errDo
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, errRA := ioutil.ReadAll(resp.Body)
|
||||
if errRA != nil {
|
||||
return nil, errRA
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
err = json.Unmarshal(b, &temps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return temps, nil
|
||||
}
|
||||
|
||||
// StartDocumentFromTemplate returns the documentID created in the given folderID from the given templateID,
|
||||
// using either a stock template if isStock==true or a saved template.
|
||||
func (c *Client) StartDocumentFromTemplate(isStock bool, templateID, folderID string) (DocumentID string, err error) {
|
||||
url := fmt.Sprintf("%s/api/templates/%s/folder/%s?type=",
|
||||
c.BaseURL, templateID, folderID)
|
||||
if isStock {
|
||||
url += "stock"
|
||||
} else {
|
||||
url += "saved"
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return "", errors.New(trimErrors(string(b)))
|
||||
}
|
||||
var model entity.Document
|
||||
err = json.Unmarshal(b, &model)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if model.RefID == "" {
|
||||
return "", errors.New("empty DocumentID returned")
|
||||
}
|
||||
return model.RefID, nil
|
||||
}
|
172
sdk/users.go
Normal file
172
sdk/users.go
Normal file
|
@ -0,0 +1,172 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package documize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/documize/api/entity"
|
||||
)
|
||||
|
||||
// GetUsers returns the users in the user's organization.
|
||||
func (c *Client) GetUsers() ([]entity.User, error) {
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/users", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
users := make([]entity.User, 0, 5)
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
err = dec.Decode(&users)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// GetUserInfo returns the user's information.
|
||||
func (c *Client) GetUserInfo() (*entity.User, error) {
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/users/"+c.Auth.User.BaseEntity.RefID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
user := new(entity.User)
|
||||
err = json.Unmarshal(b, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// AddUser adds the given user to the system.
|
||||
// The version of the user record written to the database
|
||||
// is written into the referenced User record.
|
||||
func (c *Client) AddUser(usrp *entity.User) error {
|
||||
b, err := json.Marshal(usrp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/users", bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
err = json.Unmarshal(b, usrp)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateUser updates the given user, writing the changed version back into the given User structure.
|
||||
func (c *Client) UpdateUser(usrp *entity.User) error {
|
||||
b, err := json.Marshal(usrp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/users/"+usrp.BaseEntity.RefID, bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
err = json.Unmarshal(b, usrp)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteUser deletes the given user.
|
||||
func (c *Client) DeleteUser(userID string) error {
|
||||
req, err := http.NewRequest("DELETE", c.BaseURL+"/api/users/"+userID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return errors.New(trimErrors(string(b)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserFolderPermissions gets the folder permissions for the current user.
|
||||
func (c *Client) GetUserFolderPermissions() (*[]entity.LabelRole, error) {
|
||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/users/"+c.Auth.User.RefID+"/permissions", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() // ignore error
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isError(string(b)) {
|
||||
return nil, errors.New(trimErrors(string(b)))
|
||||
}
|
||||
perm := make([]entity.LabelRole, 0, 2)
|
||||
err = json.Unmarshal(b, &perm)
|
||||
return &perm, err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue