-
Notifications
You must be signed in to change notification settings - Fork 25
/
attachment.go
69 lines (65 loc) · 1.58 KB
/
attachment.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
package bot
import (
"context"
"encoding/json"
)
type Attachment struct {
Type string `json:"type"`
AttachmentId string `json:"attachment_id"`
ViewURL string `json:"view_url"`
UploadUrl string `json:"upload_url"`
}
func CreateAttachment(ctx context.Context, user *SafeUser) (*Attachment, error) {
token, err := SignAuthenticationToken("POST", "/attachments", "", user)
if err != nil {
return nil, err
}
body, err := Request(ctx, "POST", "/attachments", nil, token)
if err != nil {
return nil, err
}
var resp struct {
Data Attachment `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error.Code > 0 {
if resp.Error.Code == 401 {
return nil, AuthorizationError(ctx)
} else if resp.Error.Code == 403 {
return nil, ForbiddenError(ctx)
}
return nil, resp.Error
}
return &resp.Data, nil
}
func AttachmentShow(ctx context.Context, id string, user *SafeUser) (*Attachment, error) {
token, err := SignAuthenticationToken("GET", "/attachments/"+id, "", user)
if err != nil {
return nil, err
}
body, err := Request(ctx, "GET", "/attachments/"+id, nil, token)
if err != nil {
return nil, err
}
var resp struct {
Data Attachment `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error.Code > 0 {
if resp.Error.Code == 401 {
return nil, AuthorizationError(ctx)
} else if resp.Error.Code == 403 {
return nil, ForbiddenError(ctx)
}
return nil, resp.Error
}
return &resp.Data, nil
}