From: Kyle Fuller Date: Wed, 6 Jan 2021 21:17:51 +0000 (+0000) Subject: refactor: dedupe request code X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=ad763ed4f0244bff0814655ae21d55f6beb6661d;p=znc-palaver.git refactor: dedupe request code --- diff --git a/palaver.cpp b/palaver.cpp index 7a88d9d..a0c22e3 100644 --- a/palaver.cpp +++ b/palaver.cpp @@ -727,14 +727,6 @@ public: MCString mcsHeaders; - CString token = GetPushToken(); - if (token.empty()) { - token = GetIdentifier(); - } - - mcsHeaders["Authorization"] = CString("Bearer " + token); - mcsHeaders["Content-Type"] = "application/json"; - CString sJSON = "{"; sJSON += "\"badge\": " + CString(m_uiBadge); @@ -757,26 +749,28 @@ public: } sJSON += "}"; - PLVHTTPSocket *pSocket = new PLVHTTPNotificationSocket(&module, token, "POST", GetPushEndpoint(), mcsHeaders, sJSON); + SendNotificationRequest(module, sJSON); + } + + void SendNotificationRequest(CModule& module, CString sJSONBody) { + MCString mcsHeaders; + + CString token = GetPushToken(); + if (token.empty()) { + token = GetIdentifier(); + } + + mcsHeaders["Authorization"] = CString("Bearer " + token); + mcsHeaders["Content-Type"] = "application/json"; + PLVHTTPSocket *pSocket = new PLVHTTPNotificationSocket(&module, token, "POST", GetPushEndpoint(), mcsHeaders, sJSONBody); module.AddSocket(pSocket); } void ClearBadges(CModule& module, bool bInformAPI) { if (m_uiBadge != 0) { if (bInformAPI) { - MCString mcsHeaders; - - CString token = GetPushToken(); - if (token.empty()) { - token = GetIdentifier(); - } - mcsHeaders["Authorization"] = CString("Bearer " + token); - mcsHeaders["Content-Type"] = "application/json"; - CString sJSON = "{\"badge\": 0}"; - - PLVHTTPSocket *pSocket = new PLVHTTPNotificationSocket(&module, token, "POST", GetPushEndpoint(), mcsHeaders, sJSON); - module.AddSocket(pSocket); + SendNotificationRequest(module, sJSON); } m_uiBadge = 0;