Пример #1
0
        private static void DoDispatch2(string baseUrl, string methodName, Dictionary <string, string> parameters)
        {
            parameters["v"] = VKConstants.API_VERSION;
            AutorizationData autorizationData1 = VKRequestsDispatcher._autorizationData;

            if (!string.IsNullOrEmpty(autorizationData1 != null ? autorizationData1.access_token : null))
            {
                parameters["access_token"] = VKRequestsDispatcher._autorizationData.access_token;
            }
            VKRequestsDispatcher.AddLangParameter(parameters);
            AutorizationData autorizationData2 = VKRequestsDispatcher._autorizationData;

            if (!string.IsNullOrEmpty(autorizationData2 != null ? autorizationData2.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 =>
            {
                if (jsonResp.IsSucceeded)
                {
                }
            }));
        }
Пример #2
0
 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);
 }