Beispiel #1
0
        /// <summary>
        /// Sends an HTTP Request to the backend and parse the response (expected as a list of objects)
        /// </summary>
        /// <param name="action">action to query (e.g. "Snippets/Get")</param>
        /// <param name="data">
        ///     if the request is a GET, the parameters to be put in the querystring (e.g. "snippetID=100"),
        ///     otherwise data to be put in the post (e.g. "content=...")
        /// </param>
        /// <param name="isPost">whether this request is a GET(false) or a POST(true)</param>
        /// <param name="requiresLogin">false if this request calls a public web service</param>
        /// <returns>null if any error occurred; the result of the invocation otherwise</returns>
        protected S2CResListObj <string> SendReqListObj(string action, string data, bool isPost, bool requiresLogin = true)
        {
            string response = PrepareAndSendReq(action, data, isPost, requiresLogin);

            if (string.IsNullOrEmpty(response))
            {
                ErrorCodes errCode = ErrorCodes.COMMUNICATION_ERROR;
                if (WebConnector.Current.IsTimeout)
                {
                    errCode = ErrorCodes.TIMEOUT;
                }
                SetLastError(log, errCode, S2CRes <string> .GetErrorMsg(errCode));
                return(new S2CResListObj <string>(0.0, errCode, null, 0));
            }

            S2CResListObj <string> resp = S2CSerializer.DeserializeObjList(response, m_serialFormat);

            if (!CheckResp <string>(resp))
            {
                PrintRespError <string>(resp);

                //if the problem is related to user not logged in, reset login status and retry another time:
                if (requiresLogin && (resp != null) && (resp.Status == ErrorCodes.NOT_LOGGED_IN))
                {
                    WebConnector.Current.ResetLoginStatus(); //reset login status

                    //retry the WS call:
                    response = PrepareAndSendReq(action, data, isPost, requiresLogin);
                    resp     = S2CSerializer.DeserializeObjList(response, m_serialFormat);
                }
            }

            return(resp);
        }
Beispiel #2
0
 /// <summary>
 /// Prints the info regarding the given erroneous response
 /// </summary>
 /// <param name="resp">response to print</param>
 /// <returns></returns>
 protected void PrintRespError <T>(S2CRes <T> resp)
 {
     if ((resp == null) || !(resp is S2CResObj <object>))
     {
         SetLastError(log, (resp == null ? ErrorCodes.FAIL : resp.Status), "Generic Error");
         log.Warn("null response");
     }
     else
     {
         S2CResObj <T> errMessage = (S2CResObj <T>)resp;
         SetLastError(log, resp.Status, errMessage.Data.PrintNull());
         log.ErrorFormat("Error receiving the response: status={0}; messsage={1}", resp.Status, LastErrorMsg);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Try to parse the given response that is expected to return a long result
        /// </summary>
        /// <returns></returns>
        protected long ParseLongResponse <T>(S2CRes <T> resp)
        {
            if (!CheckResp <T>(resp))
            {
                PrintRespError <T>(resp);
                return(-1);
            }

            long          result = -1;
            S2CResObj <T> value  = (S2CResObj <T>)resp;

            if (!long.TryParse(value.Data.ToString(), out result))
            {
                PrintRespError <T>(resp);
                return(-1);
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Try to parse the given response that is expected to return a boolean result
        /// </summary>
        /// <returns></returns>
        protected bool ParseBoolResponse <T>(S2CRes <T> resp)
        {
            if (!CheckResp <T>(resp))
            {
                PrintRespError <T>(resp);
                return(false);
            }

            bool          result = false;
            S2CResObj <T> value  = (S2CResObj <T>)resp;

            if (!bool.TryParse(value.Data.ToString(), out result))
            {
                PrintRespError <T>(resp);
                return(false);
            }

            return(result);
        }
Beispiel #5
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////


        /// <summary>
        /// Checks that the given response is valid and can be safely parsed
        /// </summary>
        /// <param name="resp">response to validate</param>
        /// <returns></returns>
        protected bool CheckResp <T>(S2CRes <T> resp)
        {
            return((resp != null) && (resp.Status == ErrorCodes.OK));  // && (resp.Data != null));
        }