-
Notifications
You must be signed in to change notification settings - Fork 1
/
attachments.go
75 lines (59 loc) · 2.39 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
package asana
import (
"context"
"fmt"
"io"
"time"
"github.com/pkg/errors"
)
// Attachment represents any file attached to a task in Asana,
// whether it’s an uploaded file or one associated via a third-party service
// such as Dropbox or Google Drive.
type Attachment struct {
// Read-only. Globally unique ID of the object
ID string `json:"gid,omitempty"`
// Read-only. The name of the object.
Name string `json:"name,omitempty"`
// Read-only. The task this object is attached to.
Parent *Task `json:"parent,omitempty"`
// Read-only. The time at which this object was created.
CreatedAt *time.Time `json:"created_at,omitempty"`
// Read-only. The URL containing the content of the attachment.
//
// Note: May be null if the attachment is hosted by box. If present, this
// URL may only be valid for 1 hour from the time of retrieval. You should
// avoid persisting this URL somewhere and just refresh it on demand to
// ensure you do not keep stale URLs.
DownloadURL string `json:"download_url,omitempty"`
// Read-only. The service hosting the attachment. Valid values are asana,
// dropbox, gdrive and box.
Host string `json:"host,omitempty"`
// Read-only. The URL where the attachment can be viewed, which may be
// friendlier to users in a browser than just directing them to a raw
// file.
ViewURL string `json:"view_url,omitempty"`
// Undocumented. A permanent asana.com link which should be a permalink
PermanentURL string `json:"permanent_url,omitempty"`
}
// Attachments lists all attachments attached to a task
func (t *Task) Attachments(ctx context.Context, client *Client, opts ...*Options) ([]*Attachment, *NextPage, error) {
client.trace("Listing attachments for %q", t.Name)
var result []*Attachment
// Make the request
nextPage, err := client.get(ctx, fmt.Sprintf("/tasks/%s/attachments", t.ID), nil, &result, opts...)
return result, nextPage, err
}
type NewAttachment struct {
Reader io.ReadCloser
FileName string
ContentType string
}
func (t *Task) CreateAttachment(ctx context.Context, client *Client, request *NewAttachment) (*Attachment, error) {
client.trace("Uploading attachment for %q", t.Name)
result := &Attachment{}
err := client.postMultipart(ctx, fmt.Sprintf("/tasks/%s/attachments", t.ID), result, "file", request.Reader, request.FileName, request.ContentType)
if err != nil {
return nil, errors.Wrap(err, "Upload attachment")
}
return result, nil
}