-
Notifications
You must be signed in to change notification settings - Fork 5
/
rpc_request.cpp
63 lines (49 loc) · 1.95 KB
/
rpc_request.cpp
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
#include "rpc_request.h"
#include "core/io/json.h"
#include "core/project_settings.h"
#include "scene/main/http_request.h"
Error RPCRequest::request(const String &p_method, const Array &p_params) {
String url = GLOBAL_GET("web3/rpc_url");
ERR_FAIL_COND_V_MSG(url.empty(), ERR_UNCONFIGURED, "RPC URL is empty in project settings.");
Dictionary data;
data["id"] = 1;
data["jsonrpc"] = "2.0";
data["method"] = p_method;
data["params"] = p_params;
return http_request->request(url, Vector<String>(), true, HTTPClient::METHOD_POST, JSON::print(data));
}
void RPCRequest::_request_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
if (p_status != HTTPRequest::RESULT_SUCCESS || p_code != 200) {
emit_signal("request_completed", RESULT_HTTP_ERROR, Dictionary());
return;
}
String response_json;
response_json.parse_utf8((const char *)p_data.read().ptr(), p_data.size());
Variant json;
String errs;
int errline;
Error err = JSON::parse(response_json, json, errs, errline);
if (err != OK) {
emit_signal("request_completed", RESULT_JSON_ERROR, Dictionary());
return;
}
Dictionary result = json;
if (result.has("error")) {
emit_signal("request_completed", RESULT_RPC_ERROR, result);
return;
}
emit_signal("request_completed", RESULT_SUCCESS, result);
}
void RPCRequest::_bind_methods() {
ClassDB::bind_method(D_METHOD("request", "method", "params"), &RPCRequest::request);
ClassDB::bind_method("_request_completed", &RPCRequest::_request_completed);
ADD_SIGNAL(MethodInfo("request_completed", PropertyInfo(Variant::INT, "status"), PropertyInfo(Variant::DICTIONARY, "result")));
BIND_ENUM_CONSTANT(RESULT_SUCCESS);
BIND_ENUM_CONSTANT(RESULT_HTTP_ERROR);
BIND_ENUM_CONSTANT(RESULT_JSON_ERROR);
}
RPCRequest::RPCRequest() {
http_request = memnew(HTTPRequest);
add_child(http_request);
http_request->connect("request_completed", this, "_request_completed");
}