-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjsonrpc_example.gd
52 lines (44 loc) · 1.87 KB
/
jsonrpc_example.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
extends Label
# Called when the node enters the scene tree for the first time.
func _ready():
return
# 1. First example: use c++ module
var op = Optimism.new()
var output = op.block_number("001")
var body1 = output["response_body"]
var json = JSON.new()
json.parse(body1.get_string_from_utf8())
var response = json.get_data()
print("example output: ", response)
return
# 2. Second example: use HTTPRequest scene
# Create an HTTP request node and connect its completion signal.
var http_request = HTTPRequest.new()
add_child(http_request)
# bind call back function
http_request.request_completed.connect(self._http_request_completed)
# Perform a POST request. The URL below returns JSON as of writing.
# Note: Don't make simultaneous requests using a single HTTPRequest node.
# The snippet below is provided for reference only.
var body = JSON.new().stringify({"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": "get_01"})
print("body: ", body)
print("type of body: ", typeof(body)) # output: String
var error = http_request.request("https://optimism.llamarpc.com", ['Content-Type: application/json'], HTTPClient.METHOD_POST, body)
if error != OK:
push_error("An error occurred in the HTTP request.")
pass # Replace with function body.
# Called when the HTTP request is completed.
func _http_request_completed(result, response_code, headers, body):
print("response_code: ", response_code, "\n")
print("result: ", result, "\n")
print("headers: ", headers, "\n")
print("body: ", body, "\n")
var json = JSON.new()
json.parse(body.get_string_from_utf8())
var response = json.get_data()
# Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
#print(response.headers["User-Agent"])
print("response: ", response)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass