From 5b3c74f0bebaff9f9ab78d9dc83c611b9c134613 Mon Sep 17 00:00:00 2001 From: Mario Steele Date: Wed, 30 Apr 2025 14:37:49 -0500 Subject: [PATCH] HTTPClient Test HTTPClient Test Code. --- http_client.gd | 52 ++++++++++++++++++++++++++++++++++++++++++++++ http_client.gd.uid | 1 + http_client.tscn | 6 ++++++ 3 files changed, 59 insertions(+) create mode 100644 http_client.gd create mode 100644 http_client.gd.uid create mode 100644 http_client.tscn diff --git a/http_client.gd b/http_client.gd new file mode 100644 index 0000000..2c1151f --- /dev/null +++ b/http_client.gd @@ -0,0 +1,52 @@ +extends Node2D + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + var client := HTTPClient.new() + var res = client.connect_to_host("127.0.0.1",9450) + + assert(res == OK) + + while client.get_status() == HTTPClient.STATUS_CONNECTING or client.get_status() == HTTPClient.STATUS_RESOLVING: + client.poll() + print("Connecting...") + await get_tree().process_frame + + assert(client.get_status() == HTTPClient.STATUS_CONNECTED) + + res = client.request(HTTPClient.METHOD_GET, "/api?request=getVariable&name=twitch_default_channel", []) + assert(res == OK) + + while client.get_status() == HTTPClient.STATUS_REQUESTING: + client.poll() + print("Requesting...") + await get_tree().process_frame + + assert(client.get_status() == HTTPClient.STATUS_BODY or client.get_status() == HTTPClient.STATUS_CONNECTED) + + print("response? ", client.has_response()) + + if client.has_response(): + var headers := client.get_response_headers_as_dictionary() + print("code: ", client.get_response_code()) + print("**headers:\\n", headers) + + if client.is_response_chunked(): + print("Response is Chunked!") + else: + var bl := client.get_response_body_length() + print("Response Length: ", bl) + var rb := PackedByteArray() + + while client.get_status() == HTTPClient.STATUS_BODY: + client.poll() + var chunk = client.read_response_body_chunk() + if chunk.size() == 0: + await get_tree().process_frame + else: + rb = rb + chunk + + print("bytes got: ", rb.size()) + var text = rb.get_string_from_ascii() + print("Text: ", text) diff --git a/http_client.gd.uid b/http_client.gd.uid new file mode 100644 index 0000000..6292e7e --- /dev/null +++ b/http_client.gd.uid @@ -0,0 +1 @@ +uid://d18wt8smvaeid diff --git a/http_client.tscn b/http_client.tscn new file mode 100644 index 0000000..c0bafa2 --- /dev/null +++ b/http_client.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://d247bxhjaip8h"] + +[ext_resource type="Script" uid="uid://d18wt8smvaeid" path="res://http_client.gd" id="1_4bxm2"] + +[node name="HttpClient" type="Node2D"] +script = ExtResource("1_4bxm2")