TbpWrraper/Src/MQL5/Base.mqh
Nique_372 637363fc21
2026-10-13 07:37:43 -05:00

155 lines
4.6 KiB
MQL5

//+------------------------------------------------------------------+
//| Main.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372"
#property strict
#ifndef TBPWRRAPER_SRC_MQL5_BASE_MQH
#define TBPWRRAPER_SRC_MQL5_BASE_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "Defines.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CTbpBase
{
protected:
//--- Variables miembro
string m_api_key;
CJsonParser m_json;
int m_timeout;
int m_last_http;
ENUM_TSN_TBP_ERR m_last_err;
//---
// Solo request, resultado crudo en m_json.m_raw
bool Send(const string& url);
// Request + CalcLen + CorrectPadding + Parse
bool SendAndParse(const string& url);
public:
CTbpBase(void) : m_api_key(NULL), m_timeout(5000), m_last_http(0), m_last_err(TSN_TBP_ERR_NONE) {}
~CTbpBase(void) {}
//--- Getters \ Setters
//- Apy key
__forceinline string ApyKey() const { return m_api_key; }
void ApyKey(const string& apykey) { m_api_key = apykey; }
//- json
CJsonParser* GetLastJson() { return &m_json; }
//- Ultimo error
ENUM_TSN_TBP_ERR LastErr() const { return m_last_err; }
//- Ultimo codigo http
int LastHttp() const { return m_last_http; }
//- Timeout
__forceinline int Timeout() const { return m_timeout; }
void Timeout(const int timeout) { m_timeout = timeout; }
//---
bool GetAllClients();
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CTbpBase::Send(const string& url)
{
//---
m_last_err = TSN_TBP_ERR_NONE;
const string req_headers = "api-key: " + m_api_key + "\r\n";
//---
static string res_headers;
static uchar data[];
//---
m_last_http = WebRequest(
"GET",
url,
req_headers,
m_timeout,
data,
m_json.m_raw,
res_headers
);
//---
if(m_last_http != 200)
{
m_last_err = TSN_TBP_ERR_SEND_HTTP;
return false;
}
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CTbpBase::SendAndParse(const string& url)
{
//---
m_last_err = TSN_TBP_ERR_NONE;
const string req_headers = "api-key: " + m_api_key + "\r\n";
//---
static string res_headers;
static uchar data[];
//---
m_last_http = WebRequest(
"GET",
url,
req_headers,
m_timeout,
data,
m_json.m_raw,
res_headers
);
//---
if(m_last_http != 200)
{
m_last_err = TSN_TBP_ERR_SEND_HTTP;
return false;
}
//---
m_json.CalcLen();
m_json.CorrectPadding();
//---
if(!m_json.Parse())
{
m_last_err = TSN_TBP_ERR_JSON_PARSING;
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CTbpBase::GetAllClients()
{
return SendAndParse(THE_BOT_PLACE_API_CUSTOMERS);
}
}
//+------------------------------------------------------------------+
#endif // TBPWRRAPER_SRC_MQL5_BASE_MQH