Skip to content

Commit

Permalink
add pogr client node
Browse files Browse the repository at this point in the history
Update pogr_client.h
  • Loading branch information
Ughuuu committed Dec 13, 2024
1 parent bd2eea4 commit 1e66326
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/blazium_sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def get_doc_classes():
"AuthoritativeClient",
"LobbyCallResponse",
"LobbyCallResult",
"POGRClient",
"POGRResult",
"POGRResponse",
]


Expand Down
82 changes: 82 additions & 0 deletions modules/blazium_sdk/doc_classes/POGRClient.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="POGRClient" inherits="BlaziumClient" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Client for the pogr.io analytics website.
</brief_description>
<description>
Client for the pogr.io analytics website.
</description>
<tutorials>
</tutorials>
<methods>
<method name="data">
<return type="POGRResponse" />
<param index="0" name="data" type="Dictionary" />
<description>
Send unstructured data.
</description>
</method>
<method name="event">
<return type="POGRResponse" />
<param index="0" name="event_name" type="String" />
<param index="1" name="event_data" type="Dictionary" />
<param index="2" name="event_flag" type="String" />
<param index="3" name="event_key" type="String" />
<param index="4" name="event_type" type="String" />
<param index="5" name="event_sub_type" type="String" />
<description>
Send event with unstructured data.
</description>
</method>
<method name="init">
<return type="POGRResponse" />
<description>
Send init request. Needs to be called first.
</description>
</method>
<method name="logs">
<return type="POGRResponse" />
<param index="0" name="tags" type="Dictionary" />
<param index="1" name="data" type="Dictionary" />
<param index="2" name="environment" type="String" />
<param index="3" name="log" type="String" />
<param index="4" name="service" type="String" />
<param index="5" name="severity" type="String" />
<param index="6" name="type" type="String" />
<description>
Add logs to the analytics.
</description>
</method>
<method name="metrics">
<return type="POGRResponse" />
<param index="0" name="tags" type="Dictionary" />
<param index="1" name="environment" type="String" />
<param index="2" name="metrics" type="Dictionary" />
<param index="3" name="service" type="String" />
<description>
Add metrics to the analytics.
</description>
</method>
<method name="monitor">
<return type="POGRResponse" />
<param index="0" name="settings" type="Dictionary" />
<description>
Send monitor request.
</description>
</method>
</methods>
<members>
<member name="build_id" type="String" setter="" getter="get_build_id" default="&quot;4.3.stable.custom_build&quot;">
Readonly build id.
</member>
<member name="client_id" type="String" setter="" getter="get_client_id" default="&quot;Blazium&quot;">
Readonly client id.
</member>
<member name="pogr_url" type="String" setter="" getter="get_pogr_url" default="&quot;https://api.pogr.io/v1/intake&quot;">
Readonly pogr url.
</member>
<member name="session_id" type="String" setter="" getter="get_session_id" default="&quot;&quot;">
Readonly session id. Updated after a call to init. Cleared after a call to end.
</member>
</members>
</class>
19 changes: 19 additions & 0 deletions modules/blazium_sdk/doc_classes/POGRResponse.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="POGRResponse" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
A response from a pogr.io request.
</brief_description>
<description>
A response from a pogr.io request.
</description>
<tutorials>
</tutorials>
<signals>
<signal name="finished">
<param index="0" name="result" type="POGRResult" />
<description>
Signal emitted when the request is finished.
</description>
</signal>
</signals>
</class>
27 changes: 27 additions & 0 deletions modules/blazium_sdk/doc_classes/POGRResult.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="POGRResult" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
A result from a response from a pogr.io request.
</brief_description>
<description>
A result from a response from a pogr.io request.
</description>
<tutorials>
</tutorials>
<methods>
<method name="has_error" qualifiers="const">
<return type="bool" />
<description>
Returns true if there is an error.
</description>
</method>
</methods>
<members>
<member name="error" type="String" setter="" getter="get_error" default="&quot;&quot;">
The error message, if any.
</member>
<member name="result" type="String" setter="" getter="get_result" default="&quot;&quot;">
The result, if any.
</member>
</members>
</class>
1 change: 1 addition & 0 deletions modules/blazium_sdk/icons/POGRClient.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
249 changes: 249 additions & 0 deletions modules/blazium_sdk/pogr/pogr_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
/**************************************************************************/
/* lobby_client.h */
/**************************************************************************/
/* This file is part of: */
/* BLAZIUM ENGINE */
/* https://blazium.app */
/**************************************************************************/
/* Copyright (c) 2024-present Blazium Engine contributors. */
/* Copyright (c) 2024 Dragos Daian, Randolph William Aarseth II. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef POGR_CLIENT_H
#define POGR_CLIENT_H

#include "../blazium_client.h"
#include "core/io/json.h"
#include "core/templates/vector.h"
#include "core/version.h"
#include "main/performance.h"
#include "scene/main/http_request.h"

class POGRClient : public BlaziumClient {
GDCLASS(POGRClient, BlaziumClient);

private:
String session_id;
String POGR_URL = "https://api.pogr.io/v1/intake";
String POGR_CLIENT = "Blazium";
String POGR_BUILD = VERSION_FULL_BUILD;

Vector<String> get_init_headers() {
Vector<String> headers;
headers.append("POGR_CLIENT: " + POGR_CLIENT);
headers.append("POGR_BUILD: " + POGR_BUILD);
return headers;
}

Vector<String> get_session_headers() {
Vector<String> headers;
headers.append("INTAKE_SESSION_ID: " + session_id);
return headers;
}

protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("init"), &POGRClient::init);
ClassDB::bind_method(D_METHOD("init_finished", "result"), &POGRClient::init_finished);
ClassDB::bind_method(D_METHOD("data", "data"), &POGRClient::data);
ClassDB::bind_method(D_METHOD("event", "event_name", "event_data", "event_flag", "event_key", "event_type", "event_sub_type"), &POGRClient::event);
ClassDB::bind_method(D_METHOD("logs", "tags", "data", "environment", "log", "service", "severity", "type"), &POGRClient::logs);
ClassDB::bind_method(D_METHOD("metrics", "tags", "environment", "metrics", "service"), &POGRClient::metrics);
ClassDB::bind_method(D_METHOD("monitor", "settings"), &POGRClient::monitor);

ClassDB::bind_method(D_METHOD("get_client_id"), &POGRClient::get_client_id);
ClassDB::bind_method(D_METHOD("get_build_id"), &POGRClient::get_build_id);
ClassDB::bind_method(D_METHOD("get_pogr_url"), &POGRClient::get_pogr_url);
ClassDB::bind_method(D_METHOD("get_session_id"), &POGRClient::get_session_id);

ADD_PROPERTY(PropertyInfo(Variant::STRING, "client_id", PROPERTY_HINT_NONE, ""), "", "get_client_id");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "build_id", PROPERTY_HINT_NONE, ""), "", "get_build_id");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "pogr_url", PROPERTY_HINT_NONE, ""), "", "get_pogr_url");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "session_id", PROPERTY_HINT_NONE, ""), "", "get_session_id");
}

public:
class POGRResult : public RefCounted {
GDCLASS(POGRResult, RefCounted);
String error;
String result;

protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("has_error"), &POGRResult::has_error);
ClassDB::bind_method(D_METHOD("get_error"), &POGRResult::get_error);
ClassDB::bind_method(D_METHOD("get_result"), &POGRResult::get_result);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "error"), "", "get_error");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "result"), "", "get_result");
}

public:
void set_error(String p_error) { error = p_error; }
void set_result(String p_result) { result = p_result; }

bool has_error() const { return error != ""; }
String get_error() const { return error; }
String get_result() const { return result; }
};
class POGRResponse : public RefCounted {
GDCLASS(POGRResponse, RefCounted);
HTTPRequest *request;

protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("_on_request_completed", "status", "code", "headers", "data"), &POGRResponse::_on_request_completed);
ADD_SIGNAL(MethodInfo("finished", PropertyInfo(Variant::OBJECT, "result", PROPERTY_HINT_RESOURCE_TYPE, "POGRResult")));
}

public:
void _on_request_completed(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data) {
request->disconnect("request_completed", Callable(this, "_on_request_completed"));
Ref<POGRResult> result;
result.instantiate();
String result_str = String::utf8((const char *)p_data.ptr(), p_data.size());
if (p_status != 200 || result_str == "") {
result->set_error("Request failed with code: " + String::num(p_code) + " " + result_str);
} else {
if (result_str != "") {
Dictionary result_dict = JSON::parse_string(result_str);
if (!result_dict.get("success", false)) {
result->set_error("Request failed with code: " + String::num(p_code) + " " + result_str);
} else {
result->set_result(result_str);
}
}
}
emit_signal(SNAME("finished"), result);
}
void post_request(String p_url, Vector<String> p_headers, Dictionary p_data, POGRClient *p_client) {
request = memnew(HTTPRequest);
p_client->add_child(request);
request->connect("request_completed", Callable(this, "_on_request_completed"));
request->request(p_url, p_headers, HTTPClient::METHOD_POST, JSON::stringify(p_data));
}
};

Ref<POGRResponse> init() {
Ref<POGRResponse> response;
response.instantiate();
response->connect("finished", Callable(this, "init_finished"));
Dictionary dict_data;
dict_data["association_id"] = OS::get_singleton()->get_unique_id();
response->post_request(POGR_URL + "/init", get_init_headers(), dict_data, this);
return response;
}

void init_finished(Ref<POGRResult> result) {
String result_str = result->get_result();
if (result.is_valid() && result_str != "") {
Dictionary result_dict = JSON::parse_string(result_str);
Dictionary payload = result_dict.get("payload", Dictionary());
set_session_id(payload.get("session_id", ""));
}
}

Ref<POGRResponse> data(Dictionary p_data) {
Ref<POGRResponse> response;
response.instantiate();
response->post_request(POGR_URL + "/data", get_session_headers(), p_data, this);
return response;
}

Ref<POGRResponse> end() {
Ref<POGRResponse> response;
response.instantiate();
response->post_request(POGR_URL + "/end", get_session_headers(), Dictionary(), this);
return response;
}

Ref<POGRResponse> event(String event_name, Dictionary event_data, String event_flag, String event_key, String event_type, String event_sub_type) {
Ref<POGRResponse> response;
response.instantiate();
Dictionary data;
data["event"] = event_name;
data["event_data"] = event_data;
data["event_flag"] = event_flag;
data["event_key"] = event_key;
data["event_type"] = event_type;
data["sub_event"] = event_sub_type;
response->post_request(POGR_URL + "/end", get_session_headers(), data, this);
return response;
}

Ref<POGRResponse> logs(Dictionary p_tags, Dictionary p_data, String p_environment, String p_log, String p_service, String p_severity, String p_type) {
Ref<POGRResponse> response;
response.instantiate();
Dictionary data;
data["tags"] = p_tags;
data["data"] = p_data;
data["environment"] = p_environment;
data["log"] = p_log;
data["service"] = p_service;
data["severity"] = p_severity;
data["type"] = p_type;
response->post_request(POGR_URL + "/logs", get_session_headers(), data, this);
return response;
}

Ref<POGRResponse> metrics(Dictionary p_tags, String p_environment, Dictionary p_metrics, String p_service) {
Ref<POGRResponse> response;
response.instantiate();
Dictionary data;
data["tags"] = p_tags;
data["environment"] = p_environment;
data["metrics"] = p_metrics;
data["service"] = p_service;
response->post_request(POGR_URL + "/logs", get_session_headers(), data, this);
return response;
}

Ref<POGRResponse> monitor(Dictionary p_settings) {
Ref<POGRResponse> response;
response.instantiate();
Dictionary data;
data["settings"] = p_settings;
data["cpu_usage"] = Performance::get_singleton()->get_monitor(Performance::Monitor::TIME_FPS);
data["dlls_loaded"] = Array();
data["memory_usage"] = OS::get_singleton()->get_static_memory_usage();
response->post_request(POGR_URL + "/logs", get_session_headers(), data, this);
return response;
}

String get_client_id() const { return POGR_CLIENT; }
String get_build_id() const { return POGR_BUILD; }
String get_pogr_url() const { return POGR_URL; }
String get_session_id() {
return session_id;
}
void set_session_id(String p_session_id) {
if (p_session_id == "") {
return;
}
session_id = p_session_id;
}
POGRClient() {
}
~POGRClient() {
}
};

#endif // POGR_CLIENT_H
Loading

0 comments on commit 1e66326

Please sign in to comment.