Skip to content

Commit

Permalink
Merge pull request #3005 from QuantumCoderQC/Fix-HTTP-node
Browse files Browse the repository at this point in the history
Refactor HTTP Request Node
  • Loading branch information
luboslenco authored Mar 10, 2024
2 parents 495870a + 84cddee commit bd1254b
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 108 deletions.
183 changes: 97 additions & 86 deletions Sources/armory/logicnode/NetworkHttpRequestNode.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,103 @@ import iron.object.Object;


class NetworkHttpRequestNode extends LogicNode {
public var property0: String;
public var statusInt: Int;
public var response: Dynamic;
public var headers: Map<String,String>;
public var parameters: Map<String,String>;

public function new(tree:LogicTree) {
super(tree);
}

override function run(from:Int) {

var url = inputs[1].get();

if(url == null){return;}
if(property0 == "post") {
headers = inputs[4].get();
parameters = inputs[5].get();
}else{
headers = inputs[2].get();
parameters = inputs[3].get();
}

var request = new haxe.Http(url);
public var property0: String;
public var callbackType: Int;
public var statusInt: Int;
public var response: Dynamic;
public var errorOut: String;
public var headers: Map<String,String>;
public var parameters: Map<String,String>;

public function new(tree:LogicTree) {
super(tree);
}

override function run(from:Int) {

var url = inputs[1].get();

if(url == null){return;}

headers = inputs[2].get();
parameters = inputs[3].get();
var printErrors: Bool = inputs[4].get();

var request = new haxe.Http(url);
#if js
request.async = true;
request.async = true;
#end
if(headers != null){
for (k in headers.keys()) {
request.addHeader( k, headers[k]);
}
}
if(parameters != null){
for (k in parameters.keys()) {
request.addParameter( k, parameters[k]);
}
}

request.onStatus = function(status:Int) {
statusInt = status;
runOutput(0);
}

request.onBytes = function(data:haxe.io.Bytes) {
response = data;
runOutput(0);
}

request.onData = function(data:String) {
response = data;
runOutput(0);
}

request.onError = function(error:String){
trace ("Error: " + error );
runOutput(0);
}

try {
if(property0 == "post") {
var bytes = inputs[2].get();
if(bytes == true){
var data:haxe.io.Bytes = inputs[3].get();
request.setPostBytes(data);
request.request(true);
}else{
var data:Dynamic = inputs[3].get();
request.setPostData(data.toString());
request.request(true);
}
} else {
request.request(false);
}
} catch( e : Dynamic ) {
trace("Could not complete request: " + e);
}

}

override function get(from: Int): Dynamic {

return switch (from) {
case 1: statusInt;
case 2: response;
default: throw "Unreachable";
}

}
if(headers != null){
for (k in headers.keys()) {
request.addHeader( k, headers[k]);
}
}
if(parameters != null){
for (k in parameters.keys()) {
request.addParameter( k, parameters[k]);
}
}

request.onStatus = function(status:Int) {
callbackType = 1;
statusInt = status;
runOutput(0);
}

request.onBytes = function(data:haxe.io.Bytes) {
callbackType = 2;
response = data;
runOutput(0);
}

request.onData = function(data:String) {
callbackType = 3;
response = data;
runOutput(0);
}

request.onError = function(error:String){
callbackType = 4;
errorOut = error;
if(printErrors) {
trace ("Error: " + error );
}
runOutput(0);
}

try {
if(property0 == "post") {
var bytes = inputs[6].get();
if(bytes == true){
var data:haxe.io.Bytes = inputs[5].get();
request.setPostBytes(data);
request.request(true);
}else{
var data:Dynamic = inputs[5].get();
request.setPostData(data.toString());
request.request(true);
}
} else {
request.request(false);
}
} catch( e : Dynamic ) {
trace("Could not complete request: " + e);
}

callbackType = 0;
runOutput(0);

}

override function get(from: Int): Dynamic {

return switch (from) {
case 1: callbackType;
case 2: statusInt;
case 3: response;
case 4: errorOut;
default: throw "Unreachable";
}

}
}
80 changes: 58 additions & 22 deletions blender/arm/logicnode/network/LN_network_http_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,47 @@


class NetworkHttpRequestNode(ArmLogicTreeNode):
"""Network Http Request"""
"""Network HTTP Request.
@option Get/ Post: Use HTTP GET or POST methods.
@input In: Action input.
@input Url: Url as string.
@input Headers: Headers as a Haxe map.
@input Parameters: Parameters for the request as Haxe map.
@seeNode Create Map
@input Print Error: Print Error in console.
@input Data: Data to send. Any type.
@input Bytes: Is the data sent as bytes or as a string.
@output Out: Multi-functional output. Type of output given by `Callback Type`.
@output Callback Type: Type of output.
0 = Node Executed
1 = Status Callback
2 = Bytes Data Response Callback
3 = String Data Response Callback
4 = Error String Callback
@utput Status: Status value
@utput Response: Response value
@output Error: Error
"""

bl_idname = 'LNNetworkHttpRequestNode'
bl_label = 'Http Request'
arm_version = 1
arm_version = 2

default_inputs_count = 5


@staticmethod
Expand All @@ -26,32 +63,19 @@ def set_enum(self, value):
select_current = self.get_enum_id_value(self, 'property0', value)
select_prev = self.property0

if select_prev != select_current:

for i in self.inputs:
self.inputs.remove(i)
for i in self.outputs:
self.outputs.remove(i)
if select_prev == select_current:
return

if (self.get_count_in(select_current) == 0):
self.add_input('ArmNodeSocketAction', 'In')
self.add_input('ArmStringSocket', 'Url')
self.add_input('ArmDynamicSocket', 'Headers')
self.add_input('ArmDynamicSocket', 'Parameters')
self.add_output('ArmNodeSocketAction', 'Out')
self.add_output('ArmIntSocket', 'Status')
self.add_output('ArmDynamicSocket', 'Response')
idx = 0
for inp in self.inputs:
if idx >= self.default_inputs_count:
self.inputs.remove(inp)
idx += 1
self['property0'] = value
else:
self.add_input('ArmNodeSocketAction', 'In')
self.add_input('ArmStringSocket', 'Url')
self.add_input('ArmDynamicSocket', 'Data')
self.add_input('ArmBoolSocket', 'Bytes')
self.add_input('ArmDynamicSocket', 'Headers')
self.add_input('ArmDynamicSocket', 'Parameters')
self.add_output('ArmNodeSocketAction', 'Out')
self.add_output('ArmIntSocket', 'Status')
self.add_output('ArmDynamicSocket', 'Response')
self['property0'] = value


Expand All @@ -69,10 +93,22 @@ def arm_init(self, context):
self.add_input('ArmStringSocket', 'Url')
self.add_input('ArmDynamicSocket', 'Headers')
self.add_input('ArmDynamicSocket', 'Parameters')
self.add_input('ArmBoolSocket', 'Print Error')

self.add_output('ArmNodeSocketAction', 'Out')
self.add_output('ArmIntSocket', 'Callback Type')
self.add_output('ArmIntSocket', 'Status')
self.add_output('ArmDynamicSocket', 'Response')
self.add_output('ArmStringSocket', 'Error')

def draw_buttons(self, context, layout):
layout.prop(self, 'property0')

def get_replacement_node(self, node_tree: bpy.types.NodeTree):
if self.arm_version not in (0, 1):
raise LookupError()

return NodeReplacement(
'LNNetworkHttpRequestNode', self.arm_version, 'LNNetworkHttpRequestNode', 2,
in_socket_mapping = {0:0, 1:1}, out_socket_mapping={0:0}
)

0 comments on commit bd1254b

Please sign in to comment.