-
Notifications
You must be signed in to change notification settings - Fork 14
/
hide_replies.go
51 lines (45 loc) · 1.32 KB
/
hide_replies.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
package gotwtr
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func hideReplies(ctx context.Context, c *client, tweetID string, hidden bool) (*HideRepliesResponse, error) {
if tweetID == "" {
return nil, errors.New("hide replies: tweetID parameter is required")
}
ep := fmt.Sprintf(hideRepliesURL, tweetID)
body := &hideRepliesBody{
Hidden: hidden,
}
j, err := json.Marshal(body)
if err != nil {
return nil, errors.New("hide replies: failed to marshal body")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, ep, bytes.NewBuffer(j))
if err != nil {
return nil, fmt.Errorf("hide replies: failed to create request: %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("hide replies: failed to send request: %w", err)
}
defer resp.Body.Close()
var hideReplies HideRepliesResponse
if err := json.NewDecoder(resp.Body).Decode(&hideReplies); err != nil {
return nil, fmt.Errorf("hide replies: failed to decode response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &hideReplies, &HTTPError{
APIName: "hide replies",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &hideReplies, nil
}