2020-11-09 13:19:03 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-08-10 04:57:45 +02:00
|
|
|
"bytes"
|
2020-11-09 13:19:03 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2021-08-10 04:57:45 +02:00
|
|
|
"mime/multipart"
|
2020-11-09 13:19:03 +01:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-10 04:57:45 +02:00
|
|
|
"github.com/mattermost/focalboard/server/api"
|
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
|
2021-08-10 04:57:45 +02:00
|
|
|
// Token if token is empty indicate client is not login yet
|
|
|
|
Token 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-08-10 04:57:45 +02:00
|
|
|
|
2021-02-03 19:42:06 +01:00
|
|
|
headers := map[string]string{
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
|
|
}
|
2021-08-10 04:57:45 +02:00
|
|
|
|
|
|
|
return &Client{url, url + APIURLSuffix, &http.Client{}, headers, sessionToken}
|
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
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) DoAPIDelete(url string, data string) (*http.Response, error) {
|
|
|
|
return c.DoAPIRequest(http.MethodDelete, c.APIURL+url, data, "")
|
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-08-10 04:57:45 +02:00
|
|
|
type requestOption func(r *http.Request)
|
|
|
|
|
|
|
|
func (c *Client) doAPIRequestReader(method, url string, data io.Reader, _ /* etag */ string, opts ...requestOption) (*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-08-10 04:57:45 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(rq)
|
|
|
|
}
|
|
|
|
|
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-08-10 04:57:45 +02:00
|
|
|
if c.Token != "" {
|
|
|
|
rq.Header.Set("Authorization", "Bearer "+c.Token)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetTeamRoute(teamID string) string {
|
|
|
|
return fmt.Sprintf("%s/%s", c.GetTeamsRoute(), teamID)
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetTeamsRoute() string {
|
|
|
|
return "/teams"
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetBlockRoute(boardID, blockID string) string {
|
|
|
|
return fmt.Sprintf("%s/%s", c.GetBlocksRoute(boardID), blockID)
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetSubtreeRoute(boardID, blockID string) string {
|
|
|
|
return fmt.Sprintf("%s/subtree", c.GetBlockRoute(boardID, blockID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBoardsRoute() string {
|
|
|
|
return "/boards"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBoardRoute(boardID string) string {
|
|
|
|
return fmt.Sprintf("%s/%s", c.GetBoardsRoute(), boardID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBlocksRoute(boardID string) string {
|
|
|
|
return fmt.Sprintf("%s/blocks", c.GetBoardRoute(boardID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBoardsAndBlocksRoute() string {
|
|
|
|
return "/boards-and-blocks"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetTeam(teamID string) (*model.Team, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetTeamRoute(teamID), "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.TeamFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBlocksForBoard(boardID string) ([]model.Block, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetBlocksRoute(boardID), "")
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) PatchBlock(boardID, blockID string, blockPatch *model.BlockPatch) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIPatch(c.GetBlockRoute(boardID, blockID), toJSON(blockPatch))
|
2021-08-06 14:10:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) DuplicateBoard(boardID string, asTemplate bool, teamID string) (bool, *Response) {
|
|
|
|
queryParams := "?asTemplate=false&"
|
|
|
|
if asTemplate {
|
|
|
|
queryParams = "?asTemplate=true"
|
|
|
|
}
|
|
|
|
r, err := c.DoAPIPost(c.GetBoardRoute(boardID)+"/duplicate"+queryParams, "")
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
return false, BuildErrorResponse(r, err)
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
return true, BuildResponse(r)
|
2020-11-09 13:19:03 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) DuplicateBlock(boardID, blockID string, asTemplate bool) (bool, *Response) {
|
|
|
|
queryParams := "?asTemplate=false"
|
|
|
|
if asTemplate {
|
|
|
|
queryParams = "?asTemplate=true"
|
|
|
|
}
|
|
|
|
r, err := c.DoAPIPost(c.GetBlockRoute(boardID, blockID)+"/duplicate"+queryParams, "")
|
2020-11-09 13:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) UndeleteBlock(boardID, blockID string) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetBlockRoute(boardID, blockID)+"/undelete", "")
|
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) InsertBlocks(boardID string, blocks []model.Block) ([]model.Block, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetBlocksRoute(boardID), toJSON(blocks))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BlocksFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DeleteBlock(boardID, blockID string) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIDelete(c.GetBlockRoute(boardID, blockID), "")
|
2022-02-22 18:42:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetSubtree(boardID, blockID string) ([]model.Block, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetSubtreeRoute(boardID, 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
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// Boards and blocks.
|
|
|
|
func (c *Client) CreateBoardsAndBlocks(bab *model.BoardsAndBlocks) (*model.BoardsAndBlocks, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetBoardsAndBlocksRoute(), toJSON(bab))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardsAndBlocksFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PatchBoardsAndBlocks(pbab *model.PatchBoardsAndBlocks) (*model.BoardsAndBlocks, *Response) {
|
|
|
|
r, err := c.DoAPIPatch(c.GetBoardsAndBlocksRoute(), toJSON(pbab))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardsAndBlocksFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DeleteBoardsAndBlocks(dbab *model.DeleteBoardsAndBlocks) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIDelete(c.GetBoardsAndBlocksRoute(), toJSON(dbab))
|
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:44:50 +01:00
|
|
|
// Sharing
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetSharingRoute(boardID string) string {
|
|
|
|
return fmt.Sprintf("%s/sharing", c.GetBoardRoute(boardID))
|
2021-01-13 01:44:50 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetSharing(boardID string) (*model.Sharing, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetSharingRoute(boardID), "")
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-08-10 04:57:45 +02:00
|
|
|
|
|
|
|
func (c *Client) GetRegisterRoute() string {
|
|
|
|
return "/register"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Register(request *api.RegisterRequest) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetRegisterRoute(), toJSON(&request))
|
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetLoginRoute() string {
|
|
|
|
return "/login"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Login(request *api.LoginRequest) (*api.LoginResponse, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetLoginRoute(), toJSON(&request))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
data, err := api.LoginResponseFromJSON(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Token != "" {
|
|
|
|
c.Token = data.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetMeRoute() string {
|
|
|
|
return "/users/me"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetMe() (*model.User, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetMeRoute(), "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
me, err := model.UserFromJSON(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
return me, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetUserRoute(id string) string {
|
|
|
|
return fmt.Sprintf("/users/%s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetUser(id string) (*model.User, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetUserRoute(id), "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
user, err := model.UserFromJSON(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
return user, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetUserChangePasswordRoute(id string) string {
|
|
|
|
return fmt.Sprintf("/users/%s/changepassword", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) UserChangePassword(id string, data *api.ChangePasswordRequest) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetUserChangePasswordRoute(id), toJSON(&data))
|
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) CreateBoard(board *model.Board) (*model.Board, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetBoardsRoute(), toJSON(board))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PatchBoard(boardID string, patch *model.BoardPatch) (*model.Board, *Response) {
|
|
|
|
r, err := c.DoAPIPatch(c.GetBoardRoute(boardID), toJSON(patch))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DeleteBoard(boardID string) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIDelete(c.GetBoardRoute(boardID), "")
|
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBoard(boardID, readToken string) (*model.Board, *Response) {
|
|
|
|
url := c.GetBoardRoute(boardID)
|
|
|
|
if readToken != "" {
|
|
|
|
url += fmt.Sprintf("?read_token=%s", readToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := c.DoAPIGet(url, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetBoardsForTeam(teamID string) ([]*model.Board, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetTeamRoute(teamID)+"/boards", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardsFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SearchBoardsForTeam(teamID, term string) ([]*model.Board, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetTeamRoute(teamID)+"/boards/search?q="+term, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardsFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetMembersForBoard(boardID string) ([]*model.BoardMember, *Response) {
|
|
|
|
r, err := c.DoAPIGet(c.GetBoardRoute(boardID)+"/members", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardMembersFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) AddMemberToBoard(member *model.BoardMember) (*model.BoardMember, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetBoardRoute(member.BoardID)+"/members", toJSON(member))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardMemberFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) UpdateBoardMember(member *model.BoardMember) (*model.BoardMember, *Response) {
|
|
|
|
r, err := c.DoAPIPut(c.GetBoardRoute(member.BoardID)+"/members/"+member.UserID, toJSON(member))
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return model.BoardMemberFromJSON(r.Body), BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DeleteBoardMember(member *model.BoardMember) (bool, *Response) {
|
|
|
|
r, err := c.DoAPIDelete(c.GetBoardRoute(member.BoardID)+"/members/"+member.UserID, "")
|
|
|
|
if err != nil {
|
|
|
|
return false, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return true, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetTeamUploadFileRoute(teamID, boardID string) string {
|
|
|
|
return fmt.Sprintf("%s/%s/files", c.GetTeamRoute(teamID), boardID)
|
2021-08-10 04:57:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) TeamUploadFile(teamID, boardID string, data io.Reader) (*api.FileUploadResponse, *Response) {
|
2021-08-10 04:57:45 +02:00
|
|
|
body := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
part, err := writer.CreateFormFile(api.UploadFormFileKey, "file")
|
|
|
|
if err != nil {
|
|
|
|
return nil, &Response{Error: err}
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(part, data); err != nil {
|
|
|
|
return nil, &Response{Error: err}
|
|
|
|
}
|
|
|
|
writer.Close()
|
|
|
|
|
|
|
|
opt := func(r *http.Request) {
|
|
|
|
r.Header.Add("Content-Type", writer.FormDataContentType())
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
r, err := c.doAPIRequestReader(http.MethodPost, c.APIURL+c.GetTeamUploadFileRoute(teamID, boardID), body, "", opt)
|
2021-08-10 04:57:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
fileUploadResponse, err := api.FileUploadResponseFromJSON(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileUploadResponse, BuildResponse(r)
|
|
|
|
}
|
2021-12-10 16:46:37 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetSubscriptionsRoute() string {
|
|
|
|
return "/subscriptions"
|
2021-12-10 16:46:37 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) CreateSubscription(sub *model.Subscription) (*model.Subscription, *Response) {
|
|
|
|
r, err := c.DoAPIPost(c.GetSubscriptionsRoute(), toJSON(&sub))
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
subNew, err := model.SubscriptionFromJSON(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
return subNew, BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) DeleteSubscription(blockID string, subscriberID string) *Response {
|
|
|
|
url := fmt.Sprintf("%s/%s/%s", c.GetSubscriptionsRoute(), blockID, subscriberID)
|
2021-12-10 16:46:37 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
r, err := c.DoAPIDelete(url, "")
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
return BuildResponse(r)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (c *Client) GetSubscriptions(subscriberID string) ([]*model.Subscription, *Response) {
|
|
|
|
url := fmt.Sprintf("%s/%s", c.GetSubscriptionsRoute(), subscriberID)
|
2021-12-10 16:46:37 +01:00
|
|
|
|
|
|
|
r, err := c.DoAPIGet(url, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
defer closeBody(r)
|
|
|
|
|
|
|
|
var subs []*model.Subscription
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&subs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BuildErrorResponse(r, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return subs, BuildResponse(r)
|
|
|
|
}
|