-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest_handler.go
352 lines (287 loc) · 9.45 KB
/
rest_handler.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
"log"
)
var baseURL = os.Getenv("BASE_URL")
var bearerToken = "Bearer " + os.Getenv("BEARER_TOKEN")
const (
// analytics layer
endpointPostClassificationTwitter = "/hitec/classify/domain/tweets/lang/"
endpointPostExtractTweetTopics = "/analytics-backend/tweetClassification"
// collection layer
endpointGetCrawlTweets = "/hitec/crawl/tweets/mention/%s/lang/%s/fast"
endpointGetCrawlAllAvailableTweets = "/hitec/crawl/tweets/mention/%s/lang/%s"
endpointGetTwitterAccountNameExists = "/hitec/crawl/tweets/%s/exists"
// storage layer
endpointPostObserveTwitterAccount = "/hitec/repository/twitter/store/observable/"
endpointGetObservablesTwitterAccounts = "/hitec/repository/twitter/observables"
endpointDeleteObservablesTwitterAccounts = "/hitec/repository/twitter/observables"
endpointGetUnclassifiedTweets = "/hitec/repository/twitter/account_name/%s/lang/%s/unclassified"
endpointPostTweet = "/hitec/repository/twitter/store/tweet/"
endpointPostClassifiedTweet = "/hitec/repository/twitter/store/classified/tweet/"
endpointPostTweetTopics = "/hitec/repository/twitter/store/topics"
GET = "GET"
POST = "POST"
DELETE = "DELETE"
AUTHORIZATION = "Authorization"
ACCEPT = "Accept"
TYPE_JSON = "application/json"
errJsonMessageTemplate = "ERR - json formatting error: %v\n"
)
var client = getHTTPClient()
func getHTTPClient() *http.Client {
pwd, _ := os.Getwd()
caCert, err := ioutil.ReadFile(pwd + "/ca_chain.crt")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
timeout := time.Duration(4 * time.Minute)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
// InsecureSkipVerify: true,
},
},
Timeout: timeout,
CheckRedirect: func(req *http.Request, _ []*http.Request) error {
req.Header.Add(AUTHORIZATION, bearerToken)
return nil
},
}
return client
}
func createRequest(method string, url string, payload io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, payload)
req.Header.Set(AUTHORIZATION, bearerToken)
req.Header.Add(ACCEPT, TYPE_JSON)
return req, err
}
// RESTPostStoreObserveTwitterAccount returns ok
func RESTPostStoreObserveTwitterAccount(obserable ObservableTwitter) bool {
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(obserable)
if err != nil {
log.Printf(errJsonMessageTemplate, err)
}
url := baseURL + endpointPostObserveTwitterAccount
req, _ := createRequest(POST, url, requestBody)
res, err := client.Do(req)
if err != nil {
log.Printf("ERR post store observable %v\n", err)
}
defer res.Body.Close()
return res.StatusCode == 200
}
// RESTGetObservablesTwitterAccounts retrieve all observables from the storage layer
func RESTGetObservablesTwitterAccounts() []ObservableTwitter {
var obserables []ObservableTwitter
url := baseURL + endpointGetObservablesTwitterAccounts
req, _ := createRequest(GET, url, bytes.NewBuffer(nil))
res, err := client.Do(req)
if err != nil {
fmt.Println("ERR cannot send observable account get request", err)
return obserables
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&obserables)
if err != nil {
fmt.Println("ERR cannot decode twitter observable json", err)
return obserables
}
return obserables
}
// RESTDeleteObservablesTwitterAccounts returns ok
func RESTDeleteObservablesTwitterAccounts(observable ObservableTwitter) bool {
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(observable)
if err != nil {
log.Printf(errJsonMessageTemplate, err)
}
url := baseURL + endpointDeleteObservablesTwitterAccounts
req, _ := createRequest(DELETE, url, requestBody)
res, err := client.Do(req)
if err != nil {
log.Printf("ERR cannot send request to delte observable %v\n", err)
}
defer res.Body.Close()
return res.StatusCode == 200
}
// RESTGetTwitterAccountNameExists check if a twitter account exists
func RESTGetTwitterAccountNameExists(accountName string) CrawlerResponseMessage {
var response CrawlerResponseMessage
endpoint := fmt.Sprintf(endpointGetTwitterAccountNameExists, accountName)
url := baseURL + endpoint
req, _ := createRequest(GET, url, bytes.NewBuffer(nil))
res, err := client.Do(req)
if err != nil {
fmt.Println("ERR cannot send get request to check if Twitter account exists", err)
return response
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
fmt.Println("ERR cannot decode response from the twitter crawler json", err)
return response
}
return response
}
// RESTGetUnclassifiedTweets retrieve all tweets from a specified account that have not been classified yet
func RESTGetUnclassifiedTweets(accountName, lang string) []Tweet {
var tweet []Tweet
endpoint := fmt.Sprintf(endpointGetUnclassifiedTweets, accountName, lang)
url := baseURL + endpoint
req, _ := createRequest(GET, url, bytes.NewBuffer(nil))
res, err := client.Do(req)
if err != nil {
fmt.Println("ERR cannot send get request to get unclassified tweets", err)
return tweet
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&tweet)
if err != nil {
fmt.Println("ERR cannot decode unclassified tweets json", err)
return tweet
}
return tweet
}
// RESTGetCrawlTweets retrieve all tweets from the collection layer that addresses the given account name
func RESTGetCrawlTweets(accountName string, lang string) []Tweet {
var tweets []Tweet
endpoint := fmt.Sprintf(endpointGetCrawlTweets, accountName, lang)
url := baseURL + endpoint
req, _ := createRequest(GET, url, bytes.NewBuffer(nil))
res, err := client.Do(req)
if err != nil {
fmt.Println("ERR cannot send request to tweet crawler", err)
return tweets
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&tweets)
if err != nil {
fmt.Println("ERR cannot decode crawled tweets", err)
return tweets
}
return tweets
}
func RESTGetCrawlMaximumNumberOfTweets(accountName string, lang string) []Tweet {
var tweets []Tweet
endpoint := fmt.Sprintf(endpointGetCrawlAllAvailableTweets, accountName, lang)
url := baseURL + endpoint
req, _ := createRequest(GET, url, bytes.NewBuffer(nil))
res, err := client.Do(req)
if err != nil {
fmt.Println("ERR crawl max number of tweets", err)
return tweets
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&tweets)
if err != nil {
fmt.Println("ERR cannot decode tweets", err)
return tweets
}
return tweets
}
// RESTPostClassifyTweets returns ok
func RESTPostClassifyTweets(tweets []Tweet, lang string) []Tweet {
var classifiedTweets []Tweet
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(tweets)
if err != nil {
log.Printf(errJsonMessageTemplate, err)
}
url := baseURL + endpointPostClassificationTwitter + lang
req, _ := createRequest(POST, url, requestBody)
res, err := client.Do(req)
if err != nil {
log.Printf("ERR %v\n", err)
return classifiedTweets
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&classifiedTweets)
if err != nil {
log.Printf("ERR cannot decode classified tweets %v\n", err)
}
return classifiedTweets
}
// RESTPostStoreTweets returns ok
func RESTPostStoreTweets(tweets []Tweet) bool {
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(tweets)
if err != nil {
log.Printf(errJsonMessageTemplate, err)
}
url := baseURL + endpointPostTweet
req, _ := createRequest(POST, url, requestBody)
res, err := client.Do(req)
if err != nil {
log.Printf("ERR cannot send request to store tweets %v\n", err)
}
defer res.Body.Close()
return res.StatusCode == 200
}
// RESTPostStoreClassifiedTweets returns ok
func RESTPostStoreClassifiedTweets(tweets []Tweet) bool {
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(tweets)
if err != nil {
log.Printf(errJsonMessageTemplate, err)
}
url := baseURL + endpointPostClassifiedTweet
req, _ := createRequest(POST, url, requestBody)
res, err := client.Do(req)
if err != nil {
log.Printf("ERR cannot send request to store tweets %v\n", err)
}
defer res.Body.Close()
return res.StatusCode == 200
}
// RESTPostExtractTweetTopics returns ok
func RESTPostExtractTweetTopics(tweet Tweet) TweetTopics {
var tweetTopics TweetTopics
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(TweetTopicExtractionPayload{Message: tweet.Text})
if err != nil {
log.Printf(errJsonMessageTemplate, err)
}
url := baseURL + endpointPostExtractTweetTopics
req, _ := createRequest(POST, url, requestBody)
res, err := client.Do(req)
if err != nil {
log.Printf("ERR %v\n", err)
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&tweetTopics)
if err != nil {
log.Printf("ERR cannot decode tweets with topics %v\n", err)
}
return tweetTopics
}
// RESTPostStoreTweetTopics returns ok
func RESTPostStoreTweetTopics(tweet Tweet) bool {
requestBody := new(bytes.Buffer)
err := json.NewEncoder(requestBody).Encode(tweet)
if err != nil {
log.Printf(errJsonMessageTemplate, err)
}
url := baseURL + endpointPostTweetTopics
req, _ := createRequest(POST, url, requestBody)
res, err := client.Do(req)
if err != nil {
log.Printf("ERR cannot send request to store tweet topics %v\n", err)
}
defer res.Body.Close()
return res.StatusCode == 200
}