2020-11-09 13:19:03 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2020-11-09 13:19:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-06-21 11:21:42 +02:00
|
|
|
APIURLSuffix = "/api/v1"
|
2020-11-09 13:19:03 +01:00
|
|
|
)
|
|
|
|
|
2021-07-09 03:09:02 +02:00
|
|
|
type RequestReaderError struct {
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rre RequestReaderError) Error() string {
|
|
|
|
return "payload: " + string(rre.buf)
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
type Response struct {
|
|
|
|
StatusCode int
|
|
|
|
Error error
|
|
|
|
Header http.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildResponse(r *http.Response) *Response {
|
|
|
|
return &Response{
|
|
|
|
StatusCode: r.StatusCode,
|
|
|
|
Header: r.Header,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildErrorResponse(r *http.Response, err error) *Response {
|
|
|
|
statusCode := 0
|
|
|
|
header := make(http.Header)
|
|
|
|
if r != nil {
|
|
|
|
statusCode = r.StatusCode
|
|
|
|
header = r.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Response{
|
|
|
|
StatusCode: statusCode,
|
|
|
|
Error: err,
|
|
|
|
Header: header,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeBody(r *http.Response) {
|
|
|
|
if r.Body != nil {
|
|
|
|
_, _ = io.Copy(ioutil.Discard, r.Body)
|
|
|
|
_ = r.Body.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toJSON(v interface{}) string {
|
|
|
|
b, _ := json.Marshal(v)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2021-06-21 11:21:42 +02:00
|
|
|
URL string
|
|
|
|
APIURL string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
HTTPHeader map[string]string
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 09:28:26 +01:00
|
|
|
func NewClient(url, sessionToken string) *Client {
|
2020-11-09 13:19:03 +01:00
|
|
|
url = strings.TrimRight(url, "/")
|
2021-02-03 19:42:06 +01:00
|
|
|
headers := map[string]string{
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
2021-02-10 01:07:41 +01:00
|
|
|
"Authorization": "Bearer " + sessionToken,
|
2021-02-03 19:42:06 +01:00
|
|
|
}
|
2021-06-21 11:21:42 +02:00
|
|
|
return &Client{url, url + APIURLSuffix, &http.Client{}, headers}
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
func (c *Client) DoAPIGet(url, etag string) (*http.Response, error) {
|
|
|
|
return c.DoAPIRequest(http.MethodGet, c.APIURL+url, "", etag)
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
func (c *Client) DoAPIPost(url, data string) (*http.Response, error) {
|
|
|
|
return c.DoAPIRequest(http.MethodPost, c.APIURL+url, data, "")
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
func (c *Client) DoAPIPatch(url, data string) (*http.Response, error) {
|
|
|
|
return c.DoAPIRequest(http.MethodPatch, c.APIURL+url, data, "")
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
func (c *Client) DoAPIPut(url, data string) (*http.Response, error) {
|
|
|
|
return c.DoAPIRequest(http.MethodPut, c.APIURL+url, data, "")
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
func (c *Client) DoAPIDelete(url string) (*http.Response, error) {
|
|
|
|
return c.DoAPIRequest(http.MethodDelete, c.APIURL+url, "", "")
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
func (c *Client) DoAPIRequest(method, url, data, etag string) (*http.Response, error) {
|
|
|
|
return c.doAPIRequestReader(method, url, strings.NewReader(data), etag)
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
func (c *Client) doAPIRequestReader(method, url string, data io.Reader, _ /* etag */ string) (*http.Response, error) {
|
2020-11-09 13:19:03 +01:00
|
|
|
rq, err := http.NewRequest(method, url, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
if c.HTTPHeader != nil && len(c.HTTPHeader) > 0 {
|
|
|
|
for k, v := range c.HTTPHeader {
|
2020-11-09 13:19:03 +01:00
|
|
|
rq.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
rp, err := c.HTTPClient.Do(rq)
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil || rp == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
if rp.StatusCode == http.StatusNotModified {
|
2020-11-09 13:19:03 +01:00
|
|
|
return rp, nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
if rp.StatusCode >= http.StatusMultipleChoices {
|
2020-11-09 13:19:03 +01:00
|
|
|
defer closeBody(rp)
|
|
|
|
b, err := ioutil.ReadAll(rp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return rp, fmt.Errorf("error when parsing response with code %d: %w", rp.StatusCode, err)
|
|
|
|
}
|
2021-07-09 03:09:02 +02:00
|
|
|
return rp, RequestReaderError{b}
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBlocksRoute() string {
|
2021-03-26 19:01:54 +01:00
|
|
|
return "/workspaces/0/blocks"
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBlockRoute(id string) string {
|
|
|
|
return fmt.Sprintf("%s/%s", c.GetBlocksRoute(), id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetSubtreeRoute(id string) string {
|
|
|
|
return fmt.Sprintf("%s/subtree", c.GetBlockRoute(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBlocks() ([]model.Block, *Response) {
|
2021-06-21 11:21:42 +02:00
|
|
|
r, err := c.DoAPIGet(c.GetBlocksRoute(), "")
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BlocksFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
func (c *Client) PatchBlock(blockID string, blockPatch *model.BlockPatch) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIPatch(c.GetBlockRoute(blockID), toJSON(blockPatch))
|
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2020-11-09 13:19:03 +01:00
|
|
|
func (c *Client) InsertBlocks(blocks []model.Block) (bool, *Response) {
|
2021-06-21 11:21:42 +02:00
|
|
|
r, err := c.DoAPIPost(c.GetBlocksRoute(), toJSON(blocks))
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DeleteBlock(blockID string) (bool, *Response) {
|
2021-06-21 11:21:42 +02:00
|
|
|
r, err := c.DoAPIDelete(c.GetBlockRoute(blockID))
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetSubtree(blockID string) ([]model.Block, *Response) {
|
2021-06-21 11:21:42 +02:00
|
|
|
r, err := c.DoAPIGet(c.GetSubtreeRoute(blockID), "")
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BlocksFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
2021-01-13 01:44:50 +01:00
|
|
|
|
|
|
|
// Sharing
|
|
|
|
|
|
|
|
func (c *Client) GetSharingRoute(rootID string) string {
|
2021-03-26 19:01:54 +01:00
|
|
|
return fmt.Sprintf("/workspaces/0/sharing/%s", rootID)
|
2021-01-13 01:44:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetSharing(rootID string) (*model.Sharing, *Response) {
|
2021-06-21 11:21:42 +02:00
|
|
|
r, err := c.DoAPIGet(c.GetSharingRoute(rootID), "")
|
2021-01-13 01:44:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
sharing := model.SharingFromJSON(r.Body)
|
|
|
|
return &sharing, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PostSharing(sharing model.Sharing) (bool, *Response) {
|
2021-06-21 11:21:42 +02:00
|
|
|
r, err := c.DoAPIPost(c.GetSharingRoute(sharing.ID), toJSON(sharing))
|
2021-01-13 01:44:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|