-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameJoltAPIBaseClass.gd
More file actions
156 lines (109 loc) · 4.43 KB
/
GameJoltAPIBaseClass.gd
File metadata and controls
156 lines (109 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
########################################
##### Base Class of GameJoltAPI ########
########################################
extends HTTPRequest
#Use _auth_user instead of changing both of these manually
var username : String
var user_token : String
#If the clinet is using the GameJolt client, it will change
var client_version : String = "none"
#Called when session changes
signal api_session_status_changed(status)
const BASE_LINK = "https://api.gamejolt.com/api/game/"
#Change this on Project Settings
var GAME_ID : String = ProjectSettings.get_setting("GameJoltAPI/Game/GameID")
var PRIVATE_KEY : String = ProjectSettings.get_setting("GameJoltAPI/Game/PrivateKey")
var MULTITHREAD : bool = ProjectSettings.get_setting("GameJoltAPI/Requests/Multithread")
var VERBOSE : bool = ProjectSettings.get_setting("GameJoltAPI/Debug/Verbose")
var DOWNLOAD_PATH : String = ProjectSettings.get_setting("GameJoltAPI/Requests/DownloadPath")
var requests := {
"counter" : 0, #The number of requests done (used to define the ID's)
"nodes" : {}, #The request Nodes
}
signal api_download_file_completed(response, id, result, metadata)
func get_gj_credentials() -> Dictionary:
#Get Credentials from GameJolt credentials file
var directory = Directory.new()
var path = ".gj-credentials"
var credentials = {}
if directory.file_exists(path):
var file = File.new()
file.open(path, file.READ)
#Get each credential from each line
credentials = {
"version" : file.get_line(),
"username" : file.get_line(),
"user_token" : file.get_line(),
}
file.close()
return credentials
func add_request_node_to_list (node: HTTPRequest, type: String, metadata:= {}, id: int = -1) -> int:
#Add a request to the queue or parallel list
if id == -1:
id = requests.counter
requests.counter += 1
#Parallelization
add_child(node)
node.connect("request_completed", self, "on_request_completed", [id])
print_verbose("New request (ID: " + String(id) + ")!")
requests.nodes[id] = ({
"type" : type,
"node" : node,
"metadata" : metadata
})
return id
func on_request_completed(result, response_code, headers, body, id):
print_verbose("Request (ID = " + String(id) + ") completed with code: " + String(response_code) +
"(Result = " + String(result) + ")")
var request_node = requests.nodes.get(id)
# Remove the node from nodes list
requests.nodes.erase(id)
request_node.node.disconnect("request_completed", self, "on_request_completed")
var parsed_body = body.get_string_from_utf8()
if not parsed_body: parsed_body = {'success': 'false'}
else: parsed_body = JSON.parse(parsed_body).result.response
var response = {
"response_code": response_code,
"headers": headers,
"body": body,
}
# Emit a general signal and then a specific one
emit_signal("api_request_completed", request_node.type, response, id, result, request_node.metadata)
emit_signal("api_" + request_node.type + "_completed", response, id, result, request_node.metadata)
#Remove the node
request_node.node.queue_free()
func make_request(url: String, type: String, path:=DOWNLOAD_PATH + String(requests.counter).md5_text(), metadata:= {}) -> int:
#Add the request to the queue and return request id
var formated_URL = url
if formated_URL.find("format") == -1:
formated_URL = url_format(url, {"format": "json"})
formated_URL = sign_url(formated_URL)
print_verbose("Final URL: " + formated_URL)
var node = HTTPRequest.new()
node.request(formated_URL)
node.use_threads = MULTITHREAD
node.download_file = path
metadata["path"] = path
metadata["url"] = url
return add_request_node_to_list(node, type, metadata)
func download_from_link(url: String, type:="download_file", path:=DOWNLOAD_PATH + String(requests.counter).md5_text(), metadata:= {}) -> int:
return make_request(url, type, path, metadata)
func url_format (base: String, args: Dictionary = {}) -> String:
#Get an url and arguments and return a formated url
var link = base
if (!base.ends_with("?")):
if (base.find("?") == -1):
link += "?"
else:
link += "&"
for key in args:
if (args.get(key)):
link += String(key).http_escape() + "=" + String(args.get(key)).http_escape() + "&"
link = link.rstrip("&")
return link
func sign_url(url: String) -> String:
var signature = url + PRIVATE_KEY
signature = signature.sha1_text()
return url_format(url, {"signature": signature})
func print_verbose(msg):
if VERBOSE: print("[GameJoltAPI] ", msg)