From 7d95e67308ee0abcce5c7bd66265146d3dfefd74 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 30 Mar 2025 11:35:03 +0300 Subject: useful helper JsonReply moved to the core --- include/m_netlib.h | 14 +++++++++++ libs/win32/mir_app.lib | Bin 302564 -> 303882 bytes libs/win64/mir_app.lib | Bin 302636 -> 303988 bytes protocols/Discord/src/http.cpp | 19 --------------- protocols/Discord/src/proto.h | 14 ----------- protocols/Facebook/src/http.cpp | 27 --------------------- protocols/Facebook/src/proto.h | 13 ---------- protocols/SkypeWeb/src/skype_utils.cpp | 27 --------------------- protocols/SkypeWeb/src/skype_utils.h | 13 ---------- protocols/Weather/src/stdafx.h | 17 ------------- protocols/Weather/src/weather_utils.cpp | 39 ------------------------------ protocols/Weather/weather.vcxproj | 1 - protocols/Weather/weather.vcxproj.filters | 3 --- src/mir_app/src/MHttpResponse.cpp | 27 +++++++++++++++++++++ src/mir_app/src/mir_app.def | 5 ++++ src/mir_app/src/mir_app64.def | 5 ++++ 16 files changed, 51 insertions(+), 173 deletions(-) delete mode 100644 protocols/Weather/src/weather_utils.cpp diff --git a/include/m_netlib.h b/include/m_netlib.h index 421ca5b05c..71e2696c4f 100644 --- a/include/m_netlib.h +++ b/include/m_netlib.h @@ -503,6 +503,20 @@ public: } }; +class MIR_APP_EXPORT JsonReply : public MNonCopyable +{ + class JSONNode *m_root = nullptr; + int m_errorCode = 0; + +public: + explicit JsonReply(MHttpResponse *response); + ~JsonReply(); + + __forceinline JSONNode &data() const { return *m_root; } + __forceinline int error() const { return m_errorCode; } + __forceinline operator bool() const { return m_errorCode == 200; } +}; + ///////////////////////////////////////////////////////////////////////////////////////// // Free the memory used by a MHttpRequest structure // diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib index f64c48e8a2..c83fd58fce 100644 Binary files a/libs/win32/mir_app.lib and b/libs/win32/mir_app.lib differ diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib index 9af7b02a6f..4366c3e67d 100644 Binary files a/libs/win64/mir_app.lib and b/libs/win64/mir_app.lib differ diff --git a/protocols/Discord/src/http.cpp b/protocols/Discord/src/http.cpp index 32976d39cf..f6a912e9e4 100644 --- a/protocols/Discord/src/http.cpp +++ b/protocols/Discord/src/http.cpp @@ -74,25 +74,6 @@ AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR m_iReqNum = ::InterlockedIncrement(&g_reqNum); } -JsonReply::JsonReply(MHttpResponse *pReply) -{ - if (pReply == nullptr) { - m_errorCode = 500; - return; - } - - m_errorCode = pReply->resultCode; - - m_root = json_parse(pReply->body); - if (m_root == nullptr) - m_errorCode = 500; -} - -JsonReply::~JsonReply() -{ - json_delete(m_root); -} - ///////////////////////////////////////////////////////////////////////////////////////// void CDiscordProto::ServerThread(void*) diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index a147124a43..04fe34110b 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -71,20 +71,6 @@ struct AsyncHttpRequest : public MTHttpRequest MCONTACT hContact; }; -class JsonReply -{ - JSONNode *m_root = nullptr; - int m_errorCode = 0; - -public: - JsonReply(MHttpResponse *); - ~JsonReply(); - - __forceinline int error() const { return m_errorCode; } - __forceinline JSONNode& data() const { return *m_root; } - __forceinline operator bool() const { return m_errorCode == 200; } -}; - ///////////////////////////////////////////////////////////////////////////////////////// struct CDiscordRole : public MZeroedObject diff --git a/protocols/Facebook/src/http.cpp b/protocols/Facebook/src/http.cpp index eaa91d8aff..033fc47b80 100644 --- a/protocols/Facebook/src/http.cpp +++ b/protocols/Facebook/src/http.cpp @@ -69,33 +69,6 @@ AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const INT_PARAM ¶m) ///////////////////////////////////////////////////////////////////////////////////////// -JsonReply::JsonReply(MHttpResponse *pReply) -{ - if (pReply == nullptr) { - m_errorCode = 500; - return; - } - - m_errorCode = pReply->resultCode; - if (m_errorCode != 200) - return; - - m_root = json_parse(pReply->body); - if (m_root == nullptr) { - m_errorCode = 500; - return; - } - - m_errorCode = (*m_root)["error_code"].as_int(); -} - -JsonReply::~JsonReply() -{ - json_delete(m_root); -} - -///////////////////////////////////////////////////////////////////////////////////////// - AsyncHttpRequest* FacebookProto::CreateRequest(const char *url, const char *szName, const char *szMethod) { AsyncHttpRequest *pReq = new AsyncHttpRequest(); diff --git a/protocols/Facebook/src/proto.h b/protocols/Facebook/src/proto.h index 00a436b524..87e31351f7 100644 --- a/protocols/Facebook/src/proto.h +++ b/protocols/Facebook/src/proto.h @@ -326,19 +326,6 @@ struct AsyncHttpRequest : public MTHttpRequest AsyncHttpRequest *operator<<(AsyncHttpRequest *, const CHAR_PARAM &); AsyncHttpRequest *operator<<(AsyncHttpRequest *, const INT_PARAM &); -class JsonReply -{ - JSONNode *m_root = nullptr; - int m_errorCode = 0; - -public: - JsonReply(MHttpResponse *); - ~JsonReply(); - - __forceinline JSONNode &data() const { return *m_root; } - __forceinline int error() const { return m_errorCode; } -}; - ///////////////////////////////////////////////////////////////////////////////////////// struct FacebookUser diff --git a/protocols/SkypeWeb/src/skype_utils.cpp b/protocols/SkypeWeb/src/skype_utils.cpp index 26cf39e7bb..87a0863a4d 100644 --- a/protocols/SkypeWeb/src/skype_utils.cpp +++ b/protocols/SkypeWeb/src/skype_utils.cpp @@ -883,30 +883,3 @@ INT_PTR CSkypeProto::GlobalParseSkypeUriService(WPARAM wParam, LPARAM lParam) return 1; } - -///////////////////////////////////////////////////////////////////////////////////////// - -JsonReply::JsonReply(MHttpResponse *pReply) -{ - if (pReply == nullptr) { - m_errorCode = 500; - return; - } - - m_errorCode = pReply->resultCode; - if (m_errorCode != 200) - return; - - m_root = json_parse(pReply->body); - if (m_root == nullptr) { - m_errorCode = 500; - return; - } - - m_errorCode = (*m_root)["status"]["code"].as_int(); -} - -JsonReply::~JsonReply() -{ - json_delete(m_root); -} diff --git a/protocols/SkypeWeb/src/skype_utils.h b/protocols/SkypeWeb/src/skype_utils.h index 64b72124b3..aecb8f66fe 100644 --- a/protocols/SkypeWeb/src/skype_utils.h +++ b/protocols/SkypeWeb/src/skype_utils.h @@ -60,17 +60,4 @@ struct CFileUploadParam : public MZeroedObject } }; -class JsonReply -{ - JSONNode *m_root = nullptr; - int m_errorCode = 0; - -public: - JsonReply(MHttpResponse *response); - ~JsonReply(); - - __forceinline JSONNode &data() const { return *m_root; } - __forceinline int error() const { return m_errorCode; } -}; - #endif //_UTILS_H_ diff --git a/protocols/Weather/src/stdafx.h b/protocols/Weather/src/stdafx.h index 956ce498b9..96e5410c71 100644 --- a/protocols/Weather/src/stdafx.h +++ b/protocols/Weather/src/stdafx.h @@ -150,20 +150,3 @@ const wchar_t *GetDefaultText(int c); // function from multiwin module void UpdateMwinData(MCONTACT hContact); - -///////////////////////////////////////////////////////////////////////////////////////// -// utils - -class JsonReply -{ - JSONNode *m_root = nullptr; - int m_errorCode = 0; - -public: - JsonReply(MHttpResponse *); - ~JsonReply(); - - __forceinline int error() const { return m_errorCode; } - __forceinline JSONNode &data() const { return *m_root; } - __forceinline operator bool() const { return m_errorCode == 200; } -}; diff --git a/protocols/Weather/src/weather_utils.cpp b/protocols/Weather/src/weather_utils.cpp deleted file mode 100644 index b37b6bbe9a..0000000000 --- a/protocols/Weather/src/weather_utils.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright (C) 2012-25 Miranda NG team (https://miranda-ng.org) - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation version 2 -of the License. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#include "stdafx.h" - -JsonReply::JsonReply(MHttpResponse *pReply) -{ - if (pReply == nullptr) { - m_errorCode = 500; - return; - } - - m_errorCode = pReply->resultCode; - - m_root = json_parse(pReply->body); - if (m_root == nullptr) - m_errorCode = 500; - - delete pReply; -} - -JsonReply::~JsonReply() -{ - json_delete(m_root); -} diff --git a/protocols/Weather/weather.vcxproj b/protocols/Weather/weather.vcxproj index 8b18056c59..56a2b52edd 100644 --- a/protocols/Weather/weather.vcxproj +++ b/protocols/Weather/weather.vcxproj @@ -41,7 +41,6 @@ - diff --git a/protocols/Weather/weather.vcxproj.filters b/protocols/Weather/weather.vcxproj.filters index 50c35cce0e..92a72a4653 100644 --- a/protocols/Weather/weather.vcxproj.filters +++ b/protocols/Weather/weather.vcxproj.filters @@ -41,9 +41,6 @@ Source Files - - Source Files - diff --git a/src/mir_app/src/MHttpResponse.cpp b/src/mir_app/src/MHttpResponse.cpp index ccfd81e34c..4a27d794df 100644 --- a/src/mir_app/src/MHttpResponse.cpp +++ b/src/mir_app/src/MHttpResponse.cpp @@ -33,6 +33,33 @@ MHttpResponse::~MHttpResponse() mir_free(szResultDescr); } +///////////////////////////////////////////////////////////////////////////////////////// + +JsonReply::JsonReply(MHttpResponse *pReply) +{ + if (pReply == nullptr) { + m_errorCode = 500; + return; + } + + m_errorCode = pReply->resultCode; + if (m_errorCode != 200) + return; + + m_root = json_parse(pReply->body); + if (m_root == nullptr) { + m_errorCode = 500; + return; + } + + m_errorCode = (*m_root)["status"]["code"].as_int(); +} + +JsonReply::~JsonReply() +{ + json_delete(m_root); +} + ///////////////////////////////////////////////////////////////////////////////////////// // MHttpResponse helpers for Pascal // declared only in m_netlib.inc diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index 1dd24ecab3..045183f86f 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -998,3 +998,8 @@ Proto_CanDeleteHistory @1118 NONAME ?getConn@MWebSocket@@QBEPAXXZ @1129 NONAME ?Chat_GetStatus@@YGPAUSTATUSINFO@@PAUSESSION_INFO@@PBUUSERINFO@@@Z @1130 NONAME ?SetBirthday@Contact@@YGXIHHH@Z @1131 NONAME +??0JsonReply@@QAE@PAUMHttpResponse@@@Z @1132 NONAME +??1JsonReply@@QAE@XZ @1133 NONAME +?data@JsonReply@@QBEAAVJSONNode@@XZ @1134 NONAME +?error@JsonReply@@QBEHXZ @1135 NONAME +??BJsonReply@@QBE_NXZ @1136 NONAME diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index 5e7b681246..097f6a94fa 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -998,3 +998,8 @@ Proto_CanDeleteHistory @1118 NONAME ?getConn@MWebSocket@@QEBAPEAXXZ @1129 NONAME ?Chat_GetStatus@@YAPEAUSTATUSINFO@@PEAUSESSION_INFO@@PEBUUSERINFO@@@Z @1130 NONAME ?SetBirthday@Contact@@YAXIHHH@Z @1131 NONAME +??0JsonReply@@QEAA@PEAUMHttpResponse@@@Z @1132 NONAME +??1JsonReply@@QEAA@XZ @1133 NONAME +?data@JsonReply@@QEBAAEAVJSONNode@@XZ @1134 NONAME +?error@JsonReply@@QEBAHXZ @1135 NONAME +??BJsonReply@@QEBA_NXZ @1136 NONAME -- cgit v1.2.3