-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmanage_direct_message.go
More file actions
117 lines (104 loc) · 3.59 KB
/
Copy pathmanage_direct_message.go
File metadata and controls
117 lines (104 loc) · 3.59 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gotwtr
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func createOneToOneDM(ctx context.Context, c *client, participantID string, body *CreateOneToOneDMBody) (*CreateOneToOneDMResponse, error) {
if participantID == "" {
return nil, errors.New("create a one to one DM: participant id parameter is required")
}
ep := fmt.Sprintf(createOneToOneDMURL, participantID)
j, err := json.Marshal(body)
if err != nil {
return nil, errors.New("create a one to one DM: can not marshal")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ep, bytes.NewBuffer((j)))
if err != nil {
return nil, fmt.Errorf("create a one to one DM with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("create a one to one DM response: %w", err)
}
defer func() { _ = resp.Body.Close() }()
var createOneToOneDM CreateOneToOneDMResponse
if err := json.NewDecoder(resp.Body).Decode(&createOneToOneDM); err != nil {
return nil, fmt.Errorf("create a one to one DM decode: %w", err)
}
if resp.StatusCode != http.StatusCreated {
return &createOneToOneDM, &HTTPError{
APIName: "create one to one DM",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &createOneToOneDM, nil
}
func createNewGroupDM(ctx context.Context, c *client, conversationID string, body *CreateNewGroupDMBody) (*CreateNewGroupDMResponse, error) {
if conversationID == "" {
return nil, errors.New("create new group DM: conversation id parameter is required")
}
ep := fmt.Sprintf(createNewGroupDMURL, conversationID)
j, err := json.Marshal(body)
if err != nil {
return nil, errors.New("create new group DM: can not marshal")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ep, bytes.NewBuffer((j)))
if err != nil {
return nil, fmt.Errorf("create new group DM with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("create new group DM response: %w", err)
}
defer func() { _ = resp.Body.Close() }()
var createNewGroupDM CreateNewGroupDMResponse
if err := json.NewDecoder(resp.Body).Decode(&createNewGroupDM); err != nil {
return nil, fmt.Errorf("create new group DM decode: %w", err)
}
if resp.StatusCode != http.StatusCreated {
return &createNewGroupDM, &HTTPError{
APIName: "create new group DM",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &createNewGroupDM, nil
}
func postDM(ctx context.Context, c *client, body *PostDMBody) (*PostDMResponse, error) {
j, err := json.Marshal(body)
if err != nil {
return nil, errors.New("post DM: can not marshal")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, postDMURL, bytes.NewBuffer((j)))
if err != nil {
return nil, fmt.Errorf("post DM with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("post DM response: %w", err)
}
defer func() { _ = resp.Body.Close() }()
var postDM PostDMResponse
if err := json.NewDecoder(resp.Body).Decode(&postDM); err != nil {
return nil, fmt.Errorf("post DM decode: %w", err)
}
if resp.StatusCode != http.StatusCreated {
return &postDM, &HTTPError{
APIName: "post DM",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &postDM, nil
}