-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaw.gd
64 lines (48 loc) · 1.29 KB
/
Raw.gd
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
extends HTTPRequest
var request_id := 0
var Error
func _ready():
.set_download_chunk_size(16777216)
self.add_child(preload("./Error.gd").new())
func close():
.cancel_request()
func default_headers():
return [
"user-agent: some useragent",
]
func network_error(error, message, url = null):
push_error("[Network Error]: " + message)
return {
"url": url,
"type": error,
"message": message,
"error": $Error.string_name(error),
}
func fetch(url: String, headers = default_headers(), data = null, type = null, id = null):
yield(get_tree(), "idle_frame")
# We'll track our response ID to prevent race conditions
if id == null:
self.request_id += 1
id = self.request_id
# Make sure we can perform this request
close()
var error
if data:
var query = JSON.print(data)
if not type:
type = HTTPClient.METHOD_POST
error = self.request(url, headers, true, type, query)
else:
error = self.request(url, headers, true)
if error:
close()
return network_error(error, "when making request", url)
var result = yield(self, "request_completed")
error = result[0]
if error:
close()
return network_error(error, "when waiting for request completion", url)
if self.request_id != id:
print("Http: Request was superceded")
return null
return result[3].get_string_from_utf8()