private void InitializeWith(VKRequestParameters parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            _parameters = parameters;
        }
        private void trackRequest_TextChanged(object sender, TextChangedEventArgs e)
        {
            var requestPar = new VKRequestParameters("audio.search", "q", trackRequest.Text);
            Action<VKBackendResult<VKList<VKAudio>>> callback = (result) =>
            {
                trackView.ItemsSource = result?.Data?.items; // оператор безопасной навигации
            };

            VKRequest.Dispatch<VKList<VKAudio>>(requestPar, callback);
        }
        public static VKRequest Dispatch <T>(VKRequestParameters parameters,
                                             Action <VKBackendResult <T> > callback,
                                             Func <string, T> customDeserializationFunc = null)
        {
            var request = new VKRequest(parameters);

            request.Dispatch <T>(callback, customDeserializationFunc);

            return(request);
        }
 public void friends_get(string par)
 {
     string[] options = JsonHelper.Deserialize<string[]>(par);
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("friends.get", "user_id", options[0], "order", options[1], "count", options[2], "offset", options[3], "fields", options[4], "name_case", options[5]);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res.ResultString), options.Last<string>());
                         } else {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, res.ResultString), options.Last<string>());
                         }
                     });
 }
        public void callApiMethod(string par)
        {
            string[] options = JsonHelper.Deserialize<string[]>(par);
            VKRequestParameters vkrp = null;
            try {
                JObject p = JObject.Parse(options[1]);
                var dict = p.ToObject<Dictionary<string, string>>();
                vkrp = new VKRequestParameters(options[0], dict);
            } catch (Exception e) {

            }
            if(vkrp != null)
            VKRequest.Dispatch<object>(
                            vkrp,
                            (res) => {
                                if (res.ResultCode == VKResultCode.Succeeded) {
                                    DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res.ResultString), options.Last<string>());
                                } else {
                                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, res.ResultString), options.Last<string>());
                                }
                            });
        }
 public void friends_getRequests(string offset, string count, string extended, string needs_mutual, string _out, string sort, string suggested, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("friends.getRequests", "offset", offset, "count", count, "extended", extended, "needs_mutual", needs_mutual, "out", _out, "sort", sort, "suggested", suggested);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public void photos_getWallUploadServer(string par)
 {
     string[] options = JsonHelper.Deserialize<string[]>(par);
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("photos.getWallUploadServer", "group_id", options[0]);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res.ResultString), options.Last<string>());
                         } else {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, res.ResultString), options.Last<string>());
                         }
                     });
 }
 public void users_isAppUser(string par)
 {
     string[] options = JsonHelper.Deserialize<string[]>(par);
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("users.isAppUser", "user_id", options[0]);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res.ResultString), options.Last<string>());
                         } else {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, res.ResultString), options.Last<string>());
                         }
                     },
                     (json) => new Object());
 }
 public void friends_getRecent(string par)
 {
     string[] options = JsonHelper.Deserialize<string[]>(par);
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("friends.getRecent", "count", options[0]);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res.ResultString), options.Last<string>());
                         } else {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, res.ResultString), options.Last<string>());
                         }
                     });
 }
 public void friends_getRequests(string par)
 {
     string[] options = JsonHelper.Deserialize<string[]>(par);
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("friends.getRequests", "offset", options[0], "count", options[1], "extended", options[2], "needs_mutual", options[3], "out", options[4], "sort", options[5], "suggested", options[6]);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res.ResultString), options.Last<string>());
                         } else {
                             DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, res.ResultString), options.Last<string>());
                         }
                     });
 }
 public void users_search(string query_or_params, int cbid) {
     VKRequestParameters vkrp;
     try {
         JObject p = JObject.Parse(query_or_params);
         var dict = p.ToObject<Dictionary<string, string>>();
         vkrp = new VKRequestParameters("users.search", dict);
     } catch (Exception e) {
         vkrp = new VKRequestParameters("users.search", "q", query_or_params);
     }
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public void users_isAppUser(string user_id, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("users.isAppUser", "user_id", user_id);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     },
                     (json) => new Object());
 }
 public void users_getSubscriptions(string user_id, string extended, string offset, string count, string fields, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("users.getSubscriptions", "user_id", user_id, "extended", extended, "offset", offset, "count", count, "fields", fields);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public VKRequest(string methodName, params string[] parameters)
 {
     var vkParameters = new VKRequestParameters(methodName, parameters);
 }
 public void friends_getRecent(string count, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("friends.getRecent", "count", count);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public void friends_getMutual(string source_uid, string target_uid, string order, string count, string offset, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("friends.getMutual", "source_uid", source_uid, "target_uid", target_uid, "order", order, "count", count, "offset", offset);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public void friends_getOnline(string user_id, string order, string count, string offset, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("friends.getOnline", "user_id", user_id, "order", order, "count", count, "offset", offset);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public void photos_getWallUploadServer(string group_id, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("photos.getWallUploadServer", "group_id", group_id);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public void users_getFollowers(string user_id, string offset, string count, string fields, string name_case, int cbid) {
     VKRequestParameters vkrp;
     vkrp = new VKRequestParameters("users.getFollowers", "user_id", user_id, "offset", offset, "count", count, "fields", fields, "name_case", name_case);
     VKRequest.Dispatch<object>(
                     vkrp,
                     (res) => {
                         if (res.ResultCode == VKResultCode.Succeeded) {
                             sendResult(cbid, res.ResultString);
                         } else {
                             sendResult(cbid, "", res.ResultString);
                         }
                     });
 }
 public VKRequest(VKRequestParameters parameters)
 {
     InitializeWith(parameters);
 }
        public void callApiMethod(string method, string args, int cbid) {
            VKRequestParameters vkrp = null;
            try {
                JObject p = JObject.Parse(args);
                var dict = p.ToObject<Dictionary<string, string>>();
                vkrp = new VKRequestParameters(method, dict);
            } catch (Exception e) {

            }
            if (vkrp != null)
                VKRequest.Dispatch<object>(
                                vkrp,
                                (res) => {
                                    if (res.ResultCode == VKResultCode.Succeeded) {
                                        sendResult(cbid, res.ResultString);
                                    } else {
                                        sendResult(cbid, "", res.ResultString);
                                    }
                                });
            else
                sendResult(cbid, "", "Invalid parameters: " + method + " " + args);
        }
        private void UploadPhoto(Stream photoStream,
            string getServerMethodName,
            Dictionary<string, string> parameters,
            string saveMethodName,
            bool saveReturnsList,
            Action<VKBackendResult<VKPhoto>> callback,
            Action<double> progressCallback)
        {

            var vkParams = new VKRequestParameters(getServerMethodName,
               parameters);

            var getServerRequest = new VKRequest(vkParams);

            getServerRequest.Dispatch<VKUploadServerAddress>(
               (res) =>
               {
                   if (res.ResultCode == VKResultCode.Succeeded)
                   {
                       var uploadUrl = res.Data.upload_url;

                       VKHttpRequestHelper.Upload(
                            uploadUrl,
                            photoStream,
                            "file1",
                            "image",
                            (uploadRes) =>
                            {
                                if (uploadRes.IsSucceeded)
                                {
                                    var serverPhotoHashJson = uploadRes.Data;

                                    var uploadData = JsonConvert.DeserializeObject<VKUploadResponseData>(serverPhotoHashJson);
                                 
                                    if (!string.IsNullOrWhiteSpace(uploadData.server))
                                    {
                                        parameters["server"] = uploadData.server;
                                    }
                                    if (!string.IsNullOrWhiteSpace(uploadData.photos_list))
                                    {
                                        parameters["photos_list"] = uploadData.photos_list;
                                    }
                                    if (!string.IsNullOrWhiteSpace(uploadData.hash))
                                    {
                                        parameters["hash"] = uploadData.hash;
                                    }
                                    if (!string.IsNullOrWhiteSpace(uploadData.photo))
                                    {
                                        parameters["photo"] = uploadData.photo;
                                    }                                    

                                    var saveWallPhotoVKParams = new VKRequestParameters(saveMethodName,
                                        parameters);

                                    var saveWallPhotoRequest = new VKRequest(saveWallPhotoVKParams);

                                    saveWallPhotoRequest.Dispatch(
                                        callback,
                                        (jsonStr) =>
                                        {
                                            if (saveReturnsList)
                                            {
                                                var resp = JsonConvert.DeserializeObject<GenericRoot<List<VKPhoto>>>(jsonStr);

                                                return resp.response.First();
                                            }
                                            else
                                            {
                                                var resp = JsonConvert.DeserializeObject<GenericRoot<VKPhoto>>(jsonStr);

                                                return resp.response;
                                            }
                                        });

                                }
                                else
                                {
                                    callback(new VKBackendResult<VKPhoto> { ResultCode = VKResultCode.UnknownError });
                                }
                            },
                            progressCallback,
                            "Image.jpg");
                   }
                   else
                   {
                       callback(new VKBackendResult<VKPhoto> { ResultCode = res.ResultCode, Error = res.Error });
                   }
               });
        }
        private void UploadPhoto(Stream photoStream,
                                 string getServerMethodName,
                                 Dictionary <string, string> parameters,
                                 string saveMethodName,
                                 bool saveReturnsList,
                                 Action <VKBackendResult <VKPhoto> > callback,
                                 Action <double> progressCallback)
        {
            var vkParams = new VKRequestParameters(getServerMethodName,
                                                   parameters);

            var getServerRequest = new VKRequest(vkParams);

            getServerRequest.Dispatch <VKUploadServerAddress>(
                (res) =>
            {
                if (res.ResultCode == VKResultCode.Succeeded)
                {
                    var uploadUrl = res.Data.upload_url;

                    VKHttpRequestHelper.Upload(
                        uploadUrl,
                        photoStream,
                        "file1",
                        "image",
                        (uploadRes) =>
                    {
                        if (uploadRes.IsSucceeded)
                        {
                            var serverPhotoHashJson = uploadRes.Data;

                            var uploadData = JsonConvert.DeserializeObject <VKUploadResponseData>(serverPhotoHashJson);

                            if (!string.IsNullOrWhiteSpace(uploadData.server))
                            {
                                parameters["server"] = uploadData.server;
                            }
                            if (!string.IsNullOrWhiteSpace(uploadData.photos_list))
                            {
                                parameters["photos_list"] = uploadData.photos_list;
                            }
                            if (!string.IsNullOrWhiteSpace(uploadData.hash))
                            {
                                parameters["hash"] = uploadData.hash;
                            }
                            if (!string.IsNullOrWhiteSpace(uploadData.photo))
                            {
                                parameters["photo"] = uploadData.photo;
                            }

                            var saveWallPhotoVKParams = new VKRequestParameters(saveMethodName,
                                                                                parameters);

                            var saveWallPhotoRequest = new VKRequest(saveWallPhotoVKParams);

                            saveWallPhotoRequest.Dispatch(
                                callback,
                                (jsonStr) =>
                            {
                                if (saveReturnsList)
                                {
                                    var resp = JsonConvert.DeserializeObject <GenericRoot <List <VKPhoto> > >(jsonStr);

                                    return(resp.response.First());
                                }
                                else
                                {
                                    var resp = JsonConvert.DeserializeObject <GenericRoot <VKPhoto> >(jsonStr);

                                    return(resp.response);
                                }
                            });
                        }
                        else
                        {
                            callback(new VKBackendResult <VKPhoto> {
                                ResultCode = VKResultCode.UnknownError
                            });
                        }
                    },
                        progressCallback,
                        "Image.jpg");
                }
                else
                {
                    callback(new VKBackendResult <VKPhoto> {
                        ResultCode = res.ResultCode, Error = res.Error
                    });
                }
            });
        }