Skip to content

Commit

Permalink
Create main.gd (#2)
Browse files Browse the repository at this point in the history
Extend HTTPClient to use a signal-based approach
  • Loading branch information
brandonlamb authored Dec 26, 2017
1 parent cb3968c commit 3242a91
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 4 deletions.
81 changes: 81 additions & 0 deletions addons/godot-unirest/http_client.gd
Original file line number Diff line number Diff line change
@@ -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
28 changes: 24 additions & 4 deletions addons/godot-unirest/unirest.gd
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions example/main.gd
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3242a91

Please sign in to comment.