Skip to content

Commit 3242a91

Browse files
authored
Create main.gd (#2)
Extend HTTPClient to use a signal-based approach
1 parent cb3968c commit 3242a91

File tree

3 files changed

+128
-4
lines changed

3 files changed

+128
-4
lines changed

addons/godot-unirest/http_client.gd

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
extends HTTPClient
2+
3+
signal connected()
4+
signal connecting()
5+
signal disconnected()
6+
signal resolving()
7+
signal resolve_failed()
8+
signal body_received(bytes)
9+
signal ssl_handshake_failed()
10+
11+
var uri
12+
13+
"""
14+
Factory convenience method
15+
"""
16+
static func create_client(url):
17+
var client = new()
18+
client.uri = URI.create(url)
19+
20+
return client
21+
22+
class URI:
23+
var scheme
24+
var host
25+
var port = -1
26+
var path
27+
var query = {}
28+
29+
static func create(url):
30+
var uri = new()
31+
32+
if url.begins_with("https://"):
33+
uri.scheme = "https"
34+
url = url.substr(0, 8)
35+
elif url.begins_with("http://"):
36+
uri.scheme = "http"
37+
url = url.substr(0, 7)
38+
else:
39+
uri.scheme = "http"
40+
41+
# URL should now be domain.com:port/path/?name=Bob&age=30
42+
var query_pos = url.find("?")
43+
if query_pos >= 0:
44+
# q: name=Bob&age=30
45+
var q = url.substr(query_pos, len(url))
46+
47+
# params: ["name=Bob", "age=30"]
48+
var params = q.split("&")
49+
50+
# query: { "name": "Bob", "age": 30 }
51+
for i in params:
52+
var parts = params[i].split("=")
53+
uri.query[parts[0]] = parts[1]
54+
55+
# URL should now be domain.com:port/path/
56+
url = url.substr(0, query_pos)
57+
58+
var slash_pos = url.find("/")
59+
if slash_pos >= 0:
60+
uri.path = url.substr(slash_pos, len(url))
61+
62+
# URL should now be domain.com:port
63+
url = url.substr(0, slash_pos)
64+
65+
var port_pos = url.find(":")
66+
if port_pos >= 0:
67+
uri.port = url.substr(port_pos, len(url))
68+
69+
# URL should now be domain.com
70+
url = url.substr(0, port_pos)
71+
72+
# Assign remaining string to host
73+
uri.host = url
74+
75+
if uri.port < 0:
76+
match uri.scheme:
77+
"https": uri.port = 443
78+
"http": uri.port = 80
79+
_: uri.port = 80
80+
81+
return uri

addons/godot-unirest/unirest.gd

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
extends Node
22

3+
const HttpClient = preload("res://addons/godot-unirest/http_client.gd")
4+
35
const USER_AGENT = "unirest-gdscript/1.0.0"
46

57
var _default_user_agent = USER_AGENT
68
var _default_headers = {}
7-
var _client = HTTPClient.new()
9+
#var _client = HTTPClient.new()
10+
var _client
11+
12+
"""
13+
Factory method to create a new unirest object
14+
"""
15+
static func create(url):
16+
return new(HttpClient.create_client(url))
17+
18+
"""
19+
Constructor
20+
21+
@param {HttpClient} client
22+
"""
23+
func _init(client):
24+
_client = client
825

926
"""
1027
Create a new request object
@@ -19,7 +36,6 @@ Create a new request object
1936
"""
2037
func request(method, url, params, headers, auth, callback = null):
2138
var r = Request.new()
22-
add_child(r)
2339

2440
print("UNIREST: request=", url, ", params=", params)
2541

@@ -308,6 +324,10 @@ class Request:
308324
var _options = Options.new()
309325
var response
310326

327+
func _on_disconnected():
328+
print("Client disconnected")
329+
_options.client.close()
330+
311331
"""
312332
Signal handler for when HTTP request completes
313333
"""
@@ -391,9 +411,9 @@ class Request:
391411
@return {Request}
392412
"""
393413
func client(value):
394-
if !(client is HTTPClient):
414+
if !(value is HTTPClient):
395415
return print("Client is not a HTTPClient object")
396-
_options.client = client
416+
_options.client = value
397417

398418
return self
399419

example/main.gd

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extends Node
2+
3+
const Unirest = preload("res://addons/godot-unirest/unirest.gd")
4+
5+
onready var unirest = Unirest.create("https://api.mydomain.com/v1")
6+
7+
func test():
8+
# Set some default headers for all requests
9+
unirest.default_headers({
10+
"Accept": Unirest.MediaType.APPLICATION_JSON,
11+
"Content-Type": Unirest.MediaType.APPLICATION_JSON
12+
})
13+
14+
var r = unirest.get("/resource/123").header("Accept-Language", "en").complete(funcref(self, "_on_complete"))
15+
var status = r.execute()
16+
17+
func _on_complete(response):
18+
print("http status: ", response.response_code)
19+
20+
for i in response.headers:
21+
print("header: name=", i, ", value=", response.headers[i])
22+
23+
print("body=", response.body)

0 commit comments

Comments
 (0)