refactor: dedupe request code
authorKyle Fuller <redacted>
Wed, 6 Jan 2021 21:17:51 +0000 (21:17 +0000)
committerKyle Fuller <redacted>
Wed, 6 Jan 2021 21:17:51 +0000 (21:17 +0000)
palaver.cpp

index 7a88d9d0c5691a503902ab45034869f896fcb9c5..a0c22e3d33230fa1f9b572e867803823fc2d4ea1 100644 (file)
@@ -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;
git clone https://git.99rst.org/PROJECT