forked from g8rswimmer/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compliance_batch_obj.go
169 lines (147 loc) · 4.96 KB
/
compliance_batch_obj.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package twitter
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// ComplianceBatchJobStatus is the compliance batch job status
type ComplianceBatchJobStatus string
const (
// ComplianceBatchJobStatusCreated is the created status
ComplianceBatchJobStatusCreated ComplianceBatchJobStatus = "created"
// ComplianceBatchJobStatusComplete is the complete status
ComplianceBatchJobStatusComplete ComplianceBatchJobStatus = "complete"
// ComplianceBatchJobStatusInProgress is the in_progress status
ComplianceBatchJobStatusInProgress ComplianceBatchJobStatus = "in_progress"
// ComplianceBatchJobStatusFailed is the failed status
ComplianceBatchJobStatusFailed ComplianceBatchJobStatus = "failed"
// ComplianceBatchJobStatusExpired is the expired status
ComplianceBatchJobStatusExpired ComplianceBatchJobStatus = "expired"
)
// ComplianceBatchJobType is the compliance batch job type
type ComplianceBatchJobType string
const (
// ComplianceBatchJobTypeTweets is the tweets job
ComplianceBatchJobTypeTweets ComplianceBatchJobType = "tweets"
// ComplianceBatchJobTypeUsers is the users job
ComplianceBatchJobTypeUsers ComplianceBatchJobType = "users"
)
// ComplianceBatchJobResult is the downloaded result
type ComplianceBatchJobResult struct {
ID string `json:"id"`
Action string `json:"action"`
CreatedAt string `json:"created_at"`
RedactedAt string `json:"redacted_at"`
Reason string `json:"reason"`
}
// ComplianceBatchJobDownloadResponse is the response from dowload results
type ComplianceBatchJobDownloadResponse struct {
Results []*ComplianceBatchJobResult
RateLimit *RateLimit
}
// ComplianceBatchJobObj is the compliance batch job
type ComplianceBatchJobObj struct {
Resumable bool `json:"resumable"`
Type ComplianceBatchJobType `json:"type"`
ID string `json:"id"`
CreatedAt string `json:"created_at"`
Name string `json:"name"`
UploadURL string `json:"upload_url"`
UploadExpiresAt string `json:"upload_expires_at"`
DownloadURL string `json:"download_url"`
DownloadExpiresAt string `json:"download_expires_at"`
Status ComplianceBatchJobStatus `json:"status"`
Error string `json:"error"`
client *http.Client
}
// Upload will upload ids from a reader
func (c ComplianceBatchJobObj) Upload(ctx context.Context, ids io.Reader) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, c.UploadURL, ids)
if err != nil {
return fmt.Errorf("compliance batch job upload request: %w", err)
}
req.Header.Add("Content-Type", "text/plain")
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("compliance batch job upload response: %w", err)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
rl := rateFromHeader(resp.Header)
if resp.StatusCode != http.StatusOK {
e := &ErrorResponse{}
if err := decoder.Decode(e); err != nil {
return &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
RateLimit: rl,
}
}
e.StatusCode = resp.StatusCode
e.RateLimit = rl
return e
}
return nil
}
// Download will download the results of the job
func (c ComplianceBatchJobObj) Download(ctx context.Context) (*ComplianceBatchJobDownloadResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.DownloadURL, nil)
if err != nil {
return nil, fmt.Errorf("compliance batch job download request: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("compliance batch job download response: %w", err)
}
defer resp.Body.Close()
rl := rateFromHeader(resp.Header)
if resp.StatusCode != http.StatusOK {
e := &ErrorResponse{}
if err := json.NewDecoder(resp.Body).Decode(e); err != nil {
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
RateLimit: rl,
}
}
e.StatusCode = resp.StatusCode
e.RateLimit = rl
return nil, e
}
results := []*ComplianceBatchJobResult{}
scanner := bufio.NewScanner(resp.Body)
scanner.Split(batchResultsSeparator)
for scanner.Scan() {
result := &ComplianceBatchJobResult{}
if err := json.Unmarshal(scanner.Bytes(), result); err != nil {
return nil, &ResponseDecodeError{
Name: "compliance batch job download",
Err: err,
RateLimit: rl,
}
}
results = append(results, result)
}
return &ComplianceBatchJobDownloadResponse{
Results: results,
RateLimit: rl,
}, nil
}
func batchResultsSeparator(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if idx := bytes.Index(data, []byte("\r\n")); idx != -1 {
return idx + len("\r\n"), data[0:idx], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}