-
Notifications
You must be signed in to change notification settings - Fork 3
/
attachments.go
97 lines (79 loc) · 2.85 KB
/
attachments.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2022, John Moore
// All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package clickup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"path/filepath"
"strconv"
)
type CreateAttachmentResponse struct {
ID string `json:"id"`
Version string `json:"version"`
Date int `json:"date"`
Title string `json:"title"`
Extension string `json:"extension"`
ThumbnailSmall string `json:"thumbnail_small"`
ThumbnailLarge string `json:"thumbnail_large"`
URL string `json:"url"`
}
type AttachmentParams struct {
FileName string
Reader io.Reader
}
// CreateTaskAttachment attaches a binary document such as an image, text file, etc. to a specific Clickup task using the
// io.Reader on params.
func (c *Client) CreateTaskAttachment(ctx context.Context, taskID, workspaceID string, useCustomTaskIDs bool, params *AttachmentParams) (*CreateAttachmentResponse, error) {
if useCustomTaskIDs && workspaceID == "" {
return nil, fmt.Errorf("workspaceID must be provided if querying by custom task id: %w", ErrValidation)
}
contents, err := ioutil.ReadAll(params.Reader)
if err != nil {
return nil, fmt.Errorf("failed to read contents of Reader: %w", err)
}
var body bytes.Buffer
multipartWriter := multipart.NewWriter(&body)
part, err := multipartWriter.CreateFormFile("attachment", filepath.Base(params.FileName)) // must be "attachment"
if err != nil {
return nil, fmt.Errorf("failed to create multipart field: %w", err)
}
part.Write(contents)
if err := multipartWriter.Close(); err != nil {
return nil, fmt.Errorf("failed to close multipart writer: %w", err)
}
urlValues := url.Values{}
urlValues.Set("custom_task_ids", strconv.FormatBool(useCustomTaskIDs))
urlValues.Add("team_id", workspaceID)
endpoint := fmt.Sprintf("%s/task/%s/attachment/?%s", c.baseURL, taskID, urlValues.Encode())
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, &body)
if err != nil {
return nil, fmt.Errorf("create attachment request failed: %w", err)
}
if err := c.AuthenticateFor(req); err != nil {
return nil, fmt.Errorf("failed to authenticate client: %w", err)
}
req.Header.Add("Content-Type", multipartWriter.FormDataContentType())
res, err := c.doer.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make create attachment request: %w", err)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
if res.StatusCode != http.StatusOK {
return nil, errorFromResponse(res, decoder)
}
var attachmentResponse CreateAttachmentResponse
if err := decoder.Decode(&attachmentResponse); err != nil {
return nil, fmt.Errorf("failed to parse attachment response: %w", err)
}
return &attachmentResponse, nil
}