private static VKRequestsDispatcher.ResultData GetResultFromJson(string jsonStr)
 {
     VKRequestsDispatcher.ResultData resultData = new VKRequestsDispatcher.ResultData()
     {
         ResultCode = ResultCode.Succeeded
     };
     try
     {
         if (jsonStr.StartsWith(VKRequestsDispatcher._errorPrefixGeneral))
         {
             VKRequestsDispatcher.RootObjectError rootObjectError = JsonConvert.DeserializeObject <VKRequestsDispatcher.RootObjectError>(jsonStr);
             resultData.error        = rootObjectError.error;
             resultData.ResultCode   = (ResultCode)rootObjectError.error.error_code;
             resultData.captcha_sid  = rootObjectError.error.captcha_sid;
             resultData.captcha_img  = rootObjectError.error.captcha_img;
             resultData.redirect_uri = rootObjectError.error.redirect_uri;
         }
         else if (jsonStr.StartsWith(VKRequestsDispatcher._errorPrefixFailed))
         {
             resultData.ResultCode = ResultCode.NewLongPollServerRequested;
         }
         else if (jsonStr.StartsWith(VKRequestsDispatcher._errorPrefixInvalid))
         {
             resultData.ResultCode = ResultCode.WrongUsernameOrPassword;
         }
         else if (jsonStr.StartsWith(VKRequestsDispatcher._errorPrefixNeedValidationAuth))
         {
             resultData.ResultCode = ResultCode.ValidationRequired;
             VKRequestsDispatcher.ErrorAuthentification authentification = JsonConvert.DeserializeObject <VKRequestsDispatcher.ErrorAuthentification>(jsonStr);
             resultData.redirect_uri    = authentification.redirect_uri;
             resultData.validation_type = authentification.validation_type;
             resultData.validation_sid  = authentification.validation_sid;
             resultData.phone_mask      = authentification.phone_mask;
         }
         else if (jsonStr.StartsWith(VKRequestsDispatcher._errorPrefixInvalidRequest))
         {
             resultData.ResultCode = ResultCode.Unauthorized;
         }
         else if (jsonStr.StartsWith(VKRequestsDispatcher._errorPrefixCaptchaNeeded))
         {
             VKRequestsDispatcher.ErrorCaptcha errorCaptcha = JsonConvert.DeserializeObject <VKRequestsDispatcher.ErrorCaptcha>(jsonStr);
             resultData.ResultCode  = ResultCode.CaptchaRequired;
             resultData.captcha_img = errorCaptcha.captcha_img;
             resultData.captcha_sid = errorCaptcha.captcha_sid;
         }
         else if (jsonStr.Contains("execute_errors"))
         {
             JToken jtoken = JObject.Parse(jsonStr)["execute_errors"];
             if (jtoken != null)
             {
                 List <VKRequestsDispatcher.Error> errorList = JsonConvert.DeserializeObject <List <VKRequestsDispatcher.Error> >(jtoken.ToString());
                 if (errorList != null)
                 {
                     if (errorList.Count > 0)
                     {
                         return(VKRequestsDispatcher.GetResultFromJson(string.Format("{{\"error\":{0}}}", JsonConvert.SerializeObject(errorList[0]))));
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         resultData.ResultCode = ResultCode.UnknownError;
         Logger.Instance.Error("Failed to parse error code, jsonStr =" + jsonStr, ex);
     }
     return(resultData);
 }
 private static VKRequestsDispatcher.ResultData GetResultFromJson(JObject jObject)
 {
     VKRequestsDispatcher.ResultData resultData = new VKRequestsDispatcher.ResultData()
     {
         ResultCode = ResultCode.Succeeded
     };
     try
     {
         JToken jtoken1 = jObject["error"];
         if (jtoken1 != null)
         {
             VKRequestsDispatcher.Error error;
             if (jtoken1.HasValues)
             {
                 JToken jtoken2 = jtoken1["error_code"];
                 if (jtoken2 != null)
                 {
                     int num = (int)jtoken2;
                     resultData.ResultCode = (ResultCode)num;
                 }
                 error = jtoken1.ToObject <VKRequestsDispatcher.Error>();
             }
             else
             {
                 string str = jtoken1.ToString();
                 if (!(str == "need_validation"))
                 {
                     if (!(str == "need_captcha"))
                     {
                         if (!(str == "invalid_client"))
                         {
                             if (!(str == "invalid_request"))
                             {
                                 if (str == "need_confirmation")
                                 {
                                     resultData.ResultCode = ResultCode.ConfirmationRequired;
                                 }
                             }
                             else
                             {
                                 resultData.ResultCode = ResultCode.Unauthorized;
                             }
                         }
                         else
                         {
                             resultData.ResultCode = ResultCode.WrongUsernameOrPassword;
                         }
                     }
                     else
                     {
                         resultData.ResultCode = ResultCode.CaptchaRequired;
                     }
                 }
                 else
                 {
                     resultData.ResultCode = ResultCode.ValidationRequired;
                 }
                 error = jObject.ToObject <VKRequestsDispatcher.Error>();
             }
             resultData.error             = error;
             resultData.captcha_sid       = error.captcha_sid;
             resultData.captcha_img       = error.captcha_img;
             resultData.redirect_uri      = error.redirect_uri;
             resultData.validation_type   = error.validation_type;
             resultData.validation_sid    = error.validation_sid;
             resultData.phone_mask        = error.phone_mask;
             resultData.confirmation_text = error.confirmation_text;
             return(resultData);
         }
         if (jObject["failed"] != null)
         {
             resultData.ResultCode = ResultCode.NewLongPollServerRequested;
             return(resultData);
         }
         JToken jtoken3 = jObject["execute_errors"];
         if (jtoken3 != null)
         {
             List <VKRequestsDispatcher.Error> errorList = jtoken3.ToObject <List <VKRequestsDispatcher.Error> >();
             if (errorList != null)
             {
                 if (errorList.Count > 0)
                 {
                     VKRequestsDispatcher.Error error = errorList[0];
                     switch ((ResultCode)error.error_code)
                     {
                     case ResultCode.CaptchaRequired:
                     case ResultCode.ValidationRequired:
                         return(VKRequestsDispatcher.GetResultFromJson(JObject.FromObject(new VKRequestsDispatcher.RootObjectError()
                         {
                             error = error
                         })));
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         resultData.ResultCode = ResultCode.UnknownError;
         Logger.Instance.Error("Failed to parse error code, jsonStr =" + jObject, ex);
     }
     return(resultData);
 }
 private static void DoDispatch <R>(string baseUrl, string methodName, Dictionary <string, string> parameters, Action <BackendResult <R, ResultCode> > callback, Func <string, R> customDeserializationFunc = null, bool lowPriority = false, bool pageDataRequest = true, CancellationToken?cancellationToken = null, Action confirmationRequiredHandler = null)
 {
     parameters["v"] = VKConstants.API_VERSION;
     if (!string.IsNullOrEmpty(VKRequestsDispatcher._autorizationData != null ? VKRequestsDispatcher._autorizationData.access_token : null))
     {
         parameters["access_token"] = VKRequestsDispatcher._autorizationData.access_token;
     }
     VKRequestsDispatcher.AddLangParameter(parameters);
     if (!string.IsNullOrEmpty(VKRequestsDispatcher._autorizationData != null ? VKRequestsDispatcher._autorizationData.secret : null))
     {
         if (parameters.ContainsKey("sig"))
         {
             parameters.Remove("sig");
         }
         string str1 = JsonWebRequest.ConvertDictionaryToQueryString(parameters, false);
         if (str1 != string.Empty)
         {
             str1 = "?" + str1;
         }
         string str2 = VKRequestsDispatcher.HashString("/method/" + methodName + str1 + VKRequestsDispatcher._autorizationData.secret);
         parameters["sig"] = str2.ToLower();
     }
     JsonWebRequest.SendHTTPRequestAsync(!VKRequestsDispatcher.USE_HTTP ? "https://" + baseUrl : "http://" + baseUrl, parameters, (Action <JsonResponseData>)(jsonResp =>
     {
         BackendResult <R, ResultCode> backendResult = new BackendResult <R, ResultCode>(ResultCode.CommunicationFailed);
         if (jsonResp.IsSucceeded)
         {
             VKRequestsDispatcher.ResultData resultFromJson = VKRequestsDispatcher.GetResultFromJson(JObject.Parse(jsonResp.JsonString));
             backendResult.ResultCode = resultFromJson.ResultCode;
             backendResult.Error      = resultFromJson.error;
             if (backendResult.ResultCode == ResultCode.UserAuthorizationFailed)
             {
                 if (!string.IsNullOrEmpty(VKRequestsDispatcher._autorizationData != null ? VKRequestsDispatcher._autorizationData.access_token : null))
                 {
                     Logger.Instance.Error("RECEIVED AUTHORIZATION FAILURE!!! JSON STR = " + jsonResp.JsonString ?? "");
                     LogoutRequestHandler.InvokeLogoutRequest();
                 }
             }
             if (backendResult.ResultCode == ResultCode.CaptchaRequired)
             {
                 CaptchaUserRequestHandler.InvokeCaptchaRequest(new CaptchaUserRequest()
                 {
                     CaptchaSid = resultFromJson.captcha_sid,
                     Url        = resultFromJson.captcha_img
                 }, (Action <CaptchaUserResponse>)(captchaResp =>
                 {
                     if (!captchaResp.IsCancelled)
                     {
                         Dictionary <string, string> parameters1 = parameters;
                         parameters1["captcha_sid"] = captchaResp.Request.CaptchaSid;
                         parameters1["captcha_key"] = captchaResp.EnteredString;
                         VKRequestsDispatcher.DoDispatch <R>(baseUrl, methodName, parameters1, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, null);
                     }
                     else
                     {
                         VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.CaptchaControlCancelled))), cancellationToken);
                     }
                 }));
             }
             else if (backendResult.ResultCode == ResultCode.ValidationRequired)
             {
                 if (resultFromJson.validation_type == "2fa_app" || resultFromJson.validation_type == "2fa_sms")
                 {
                     if (parameters.ContainsKey("force_sms") || parameters.ContainsKey("code"))
                     {
                         R r = customDeserializationFunc != null ? customDeserializationFunc("") : default(R);
                         VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.Succeeded, r))), cancellationToken);
                     }
                     else
                     {
                         CaptchaUserRequestHandler.InvokeValidation2FARequest(new Validation2FAUserRequest()
                         {
                             username       = parameters.ContainsKey("username") ? parameters["username"] : "",
                             password       = parameters.ContainsKey("password") ? parameters["password"] : "",
                             phoneMask      = resultFromJson.phone_mask,
                             validationType = resultFromJson.validation_type,
                             validationSid  = resultFromJson.validation_sid
                         }, (Action <ValidationUserResponse>)(valResp => VKRequestsDispatcher.ProcessValidationResponse <R>(valResp, baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken)));
                     }
                 }
                 else
                 {
                     CaptchaUserRequestHandler.InvokeValidationRequest(new ValidationUserRequest()
                     {
                         ValidationUri = resultFromJson.redirect_uri
                     }, (Action <ValidationUserResponse>)(valResp => VKRequestsDispatcher.ProcessValidationResponse <R>(valResp, baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken)));
                 }
             }
             else if (backendResult.ResultCode == ResultCode.ConfirmationRequired)
             {
                 if (!VKRequestsDispatcher.GetIsResponseCancelled(cancellationToken))
                 {
                     Action action = confirmationRequiredHandler;
                     if (action != null)
                     {
                         action();
                     }
                     IBackendConfirmationHandler confirmationHandler = ServiceLocator.Resolve <IBackendConfirmationHandler>();
                     if (confirmationHandler != null)
                     {
                         confirmationHandler.Confirm(resultFromJson.confirmation_text, (Action <bool>)(confirmed =>
                         {
                             if (confirmed)
                             {
                                 parameters["confirm"] = "1";
                                 VKRequestsDispatcher.DoDispatch <R>(baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, null);
                             }
                             else
                             {
                                 VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.ConfirmationCancelled))), cancellationToken);
                             }
                         }));
                     }
                 }
             }
             else if (backendResult.ResultCode == ResultCode.NotEnoughMoney)
             {
                 if (!VKRequestsDispatcher.GetIsResponseCancelled(cancellationToken))
                 {
                     IBackendNotEnoughMoneyHandler enoughMoneyHandler = ServiceLocator.Resolve <IBackendNotEnoughMoneyHandler>();
                     Action action1 = (() => VKRequestsDispatcher.DoDispatch <R>(baseUrl, methodName, parameters, callback, customDeserializationFunc, lowPriority, pageDataRequest, cancellationToken, null));
                     Action action2 = (Action)(() => VKRequestsDispatcher.InvokeCallback((Action)(() => callback(new BackendResult <R, ResultCode>(ResultCode.BalanceRefillCancelled))), cancellationToken));
                     if (enoughMoneyHandler != null)
                     {
                         Action refilledCallback  = action1;
                         Action cancelledCallback = action2;
                         enoughMoneyHandler.RequestBalanceRefill(refilledCallback, cancelledCallback);
                     }
                 }
             }
             else if (backendResult.ResultCode == ResultCode.Succeeded)
             {
                 try
                 {
                     List <ExecuteError> executeErrorList = null;
                     R r;
                     if (customDeserializationFunc != null)
                     {
                         r = customDeserializationFunc(jsonResp.JsonString);
                     }
                     else if (typeof(R) == typeof(VKClient.Common.Backend.DataObjects.ResponseWithId))
                     {
                         r = JsonConvert.DeserializeObject <R>(jsonResp.JsonString);
                     }
                     else
                     {
                         VKRequestsDispatcher.GenericRoot <R> genericRoot = JsonConvert.DeserializeObject <VKRequestsDispatcher.GenericRoot <R> >(jsonResp.JsonString);
                         r = genericRoot.response;
                         executeErrorList = genericRoot.execute_errors;
                     }
                     backendResult.ResultData    = r;
                     backendResult.ExecuteErrors = executeErrorList;
                 }
                 catch (Exception ex)
                 {
                     Logger.Instance.Error("Error during deserialization", ex);
                     backendResult.ResultCode = ResultCode.DeserializationError;
                 }
             }
         }
         if (backendResult.ResultCode == ResultCode.CaptchaRequired || backendResult.ResultCode == ResultCode.ValidationRequired || (backendResult.ResultCode == ResultCode.ConfirmationRequired || backendResult.ResultCode == ResultCode.NotEnoughMoney))
         {
             return;
         }
         VKRequestsDispatcher.InvokeCallback((Action)(() =>
         {
             Action <BackendResult <R, ResultCode> > action = callback;
             if (action == null)
             {
                 return;
             }
             BackendResult <R, ResultCode> backendResult1 = backendResult;
             action(backendResult1);
         }), cancellationToken);
     }), true, lowPriority, pageDataRequest);
 }