From 3242a919748a59e3206577ab94063ac6430a813f Mon Sep 17 00:00:00 2001 From: Brandon Lamb Date: Mon, 25 Dec 2017 20:38:39 -0800 Subject: [PATCH] Create main.gd (#2) Extend HTTPClient to use a signal-based approach --- addons/godot-unirest/http_client.gd | 81 +++++++++++++++++++++++++++++ addons/godot-unirest/unirest.gd | 28 ++++++++-- example/main.gd | 23 ++++++++ 3 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 addons/godot-unirest/http_client.gd create mode 100644 example/main.gd diff --git a/addons/godot-unirest/http_client.gd b/addons/godot-unirest/http_client.gd new file mode 100644 index 0000000..75380db --- /dev/null +++ b/addons/godot-unirest/http_client.gd @@ -0,0 +1,81 @@ +extends HTTPClient + +signal connected() +signal connecting() +signal disconnected() +signal resolving() +signal resolve_failed() +signal body_received(bytes) +signal ssl_handshake_failed() + +var uri + +""" +Factory convenience method +""" +static func create_client(url): + var client = new() + client.uri = URI.create(url) + + return client + +class URI: + var scheme + var host + var port = -1 + var path + var query = {} + + static func create(url): + var uri = new() + + if url.begins_with("https://"): + uri.scheme = "https" + url = url.substr(0, 8) + elif url.begins_with("http://"): + uri.scheme = "http" + url = url.substr(0, 7) + else: + uri.scheme = "http" + + # URL should now be domain.com:port/path/?name=Bob&age=30 + var query_pos = url.find("?") + if query_pos >= 0: + # q: name=Bob&age=30 + var q = url.substr(query_pos, len(url)) + + # params: ["name=Bob", "age=30"] + var params = q.split("&") + + # query: { "name": "Bob", "age": 30 } + for i in params: + var parts = params[i].split("=") + uri.query[parts[0]] = parts[1] + + # URL should now be domain.com:port/path/ + url = url.substr(0, query_pos) + + var slash_pos = url.find("/") + if slash_pos >= 0: + uri.path = url.substr(slash_pos, len(url)) + + # URL should now be domain.com:port + url = url.substr(0, slash_pos) + + var port_pos = url.find(":") + if port_pos >= 0: + uri.port = url.substr(port_pos, len(url)) + + # URL should now be domain.com + url = url.substr(0, port_pos) + + # Assign remaining string to host + uri.host = url + + if uri.port < 0: + match uri.scheme: + "https": uri.port = 443 + "http": uri.port = 80 + _: uri.port = 80 + + return uri diff --git a/addons/godot-unirest/unirest.gd b/addons/godot-unirest/unirest.gd index 0ab4757..e8dd5ca 100644 --- a/addons/godot-unirest/unirest.gd +++ b/addons/godot-unirest/unirest.gd @@ -1,10 +1,27 @@ extends Node +const HttpClient = preload("res://addons/godot-unirest/http_client.gd") + const USER_AGENT = "unirest-gdscript/1.0.0" var _default_user_agent = USER_AGENT var _default_headers = {} -var _client = HTTPClient.new() +#var _client = HTTPClient.new() +var _client + +""" +Factory method to create a new unirest object +""" +static func create(url): + return new(HttpClient.create_client(url)) + +""" +Constructor + +@param {HttpClient} client +""" +func _init(client): + _client = client """ Create a new request object @@ -19,7 +36,6 @@ Create a new request object """ func request(method, url, params, headers, auth, callback = null): var r = Request.new() - add_child(r) print("UNIREST: request=", url, ", params=", params) @@ -308,6 +324,10 @@ class Request: var _options = Options.new() var response + func _on_disconnected(): + print("Client disconnected") + _options.client.close() + """ Signal handler for when HTTP request completes """ @@ -391,9 +411,9 @@ class Request: @return {Request} """ func client(value): - if !(client is HTTPClient): + if !(value is HTTPClient): return print("Client is not a HTTPClient object") - _options.client = client + _options.client = value return self diff --git a/example/main.gd b/example/main.gd new file mode 100644 index 0000000..9eb028a --- /dev/null +++ b/example/main.gd @@ -0,0 +1,23 @@ +extends Node + +const Unirest = preload("res://addons/godot-unirest/unirest.gd") + +onready var unirest = Unirest.create("https://api.mydomain.com/v1") + +func test(): + # Set some default headers for all requests + unirest.default_headers({ + "Accept": Unirest.MediaType.APPLICATION_JSON, + "Content-Type": Unirest.MediaType.APPLICATION_JSON + }) + + var r = unirest.get("/resource/123").header("Accept-Language", "en").complete(funcref(self, "_on_complete")) + var status = r.execute() + +func _on_complete(response): + print("http status: ", response.response_code) + + for i in response.headers: + print("header: name=", i, ", value=", response.headers[i]) + + print("body=", response.body)