-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistener.groovy
More file actions
140 lines (128 loc) · 4.43 KB
/
listener.groovy
File metadata and controls
140 lines (128 loc) · 4.43 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
class Listener {
def payload
def token_json
def api_key = '00000000-0000-0000-0000-000000000000'
String token_directory = 'tmp'
String token_path = 'tmp/token.json'
int loop_number = 5
String url = 'https://webhook.site/76660e37-06fb-48bb-9ce6-5de86bbb73ea'
Boolean isTokenValid = false
def createTokenDir() {
File f = new File(token_directory)
if (!f.exists()) {
println 'Creating directory..'
def tree = new FileTreeBuilder()
tree.dir(token_directory)
}
}
def saveToken(json) {
if (new File(token_directory).exists()) {
println 'Saving token..'
if (json != null) {
new File(token_path).write(json)
} else {
println 'Missing token data!'
}
}
}
def readTokenFromFile() {
if (new File(token_directory).exists() && new File(token_path).exists()) {
println 'Reading token..'
def jsonSlurper = new JsonSlurper()
def data = jsonSlurper.parse(new File(token_path))
token_json = JsonOutput.toJson(data)
} else {
println 'Missing token data!'
}
}
def requestNewToken() {
println 'Requesting token..'
try {
def newTokenPost = new URL('http://127.0.0.1:5000/token')
def connection = newTokenPost.openConnection()
connection.setRequestMethod('POST')
connection.setRequestProperty('Content-Type', 'application/json')
connection.setRequestProperty('X-Api-Key', api_key)
token_json = JsonOutput.prettyPrint(connection.getInputStream().getText())
println('POST ' + connection.responseCode)
} catch (Exception ex) {
println(ex)
}
}
def validateToken() {
println 'Validating token..'
def object
try {
def validateGet = new URL('http://127.0.0.1:5000/validate')
def connection = validateGet.openConnection()
if (token_json != null) {
def jsonSlurper = new JsonSlurper()
object = jsonSlurper.parseText(token_json)
connection.setRequestProperty('Authorization', 'Bearer ' + object.token)
}
connection.setRequestProperty('Content-Type', 'application/json')
connection.setRequestMethod('GET')
println('GET ' + connection.responseCode)
if (connection.responseCode == 200) {
isTokenValid = true
} else {
isTokenValid = false
}
} catch (Exception ex) {
println(ex)
}
}
def currentDateAndTime() {
Date date = new Date()
return date.format('dd/MM/YYYY-hhmm')
}
def updatePayload(attempt, date) {
def json = JsonOutput.toJson(
[requestID:'14',
reporter:'adam.nowak',
title:'Megacorp rate limit',
attempt: attempt,
details: [requestDate: date, requestor: 'webhook-user']])
payload = JsonOutput.prettyPrint(json)
}
def get() {
try {
def webhookGet = new URL(url)
def connection = webhookGet.openConnection()
connection.setRequestMethod('POST')
connection.setDoOutput(true)
connection.setRequestProperty('Content-Type', 'application/json')
connection.setRequestProperty('X-HTTP-Method-Override', 'GET')
connection.with {
outputStream.withWriter { outputStreamWriter ->
outputStreamWriter << payload
}
}
println('GET ' + connection.responseCode)
} catch (Exception ex) {
println(ex)
}
}
static void main(String[] args) {
Listener l = new Listener()
l.readTokenFromFile()
l.validateToken()
if (l.isTokenValid == false) {
l.requestNewToken()
l.createTokenDir()
l.saveToken(l.token_json)
l.readTokenFromFile()
l.validateToken()
}
if (l.isTokenValid == true) {
for (int i = 0; i < l.loop_number; i++) {
l.updatePayload(i + 1, l.currentDateAndTime())
l.get()
}
} else {
println 'Invalid Token..'
}
}
}