GetApiUrl() public method

Получить URL для API.
public GetApiUrl ( string methodName, string>.IDictionary parameters, bool skipAuthorization = false ) : string
methodName string Название метода.
parameters string>.IDictionary Параметры.
skipAuthorization bool Пропускать ли авторизацию
return string
Example #1
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbMethodName.Text))
            {
                MessageBox.Show("Не указано имя метода!");
                return;
            }

            // get authorization values from app.config
            int appId;
            try
            {
                string appIdValue = ConfigurationManager.AppSettings["appId"];
                appId = Convert.ToInt32(appIdValue);
            }
            catch (FormatException)
            {
                MessageBox.Show("Ошибка в разборе appId из app.config", "Ошибка приложения", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string login = ConfigurationManager.AppSettings["login"];
            string password = ConfigurationManager.AppSettings["password"];

            btnRun.Enabled = btnGetTest.Enabled = false;

            // Authorize on vk server
            var api = new VkApi();
            try
            {
                api.Authorize(appId, login, password, Settings.All);
            }
            catch (VkApiException)
            {
                MessageBox.Show("Ошибка авторизации. Проверьте данные в app.config.", "Ошибка приложения", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnRun.Enabled = btnGetTest.Enabled = true;
                return;
            }

            // Combine parameters
            var parameters = new VkParameters();
            for (int i = 1; i < 11; i++)
            {
                var name = (TextBox) pnlParams.Controls.Find(string.Format("tbParamName{0}", i), false)[0];
                var value = (TextBox)pnlParams.Controls.Find(string.Format("tbParamValue{0}", i), false)[0];

                string paramName = name.Text.Trim();
                string paramValue = value.Text.Trim();

                if (string.IsNullOrEmpty(paramName)) continue;

                parameters.Add(paramName, paramValue);
            }

            string methodName = tbMethodName.Text.Trim();

            VkResponse response;
            try
            {
                response = api.Call(methodName, parameters);
            }
            catch (VkApiException ex)
            {
                MessageBox.Show(ex.InnerException != null ? ex.InnerException.Message : ex.Message, "Ошибка приложения",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnRun.Enabled = btnGetTest.Enabled = true;
                return;
            }

            tbJson.Text = response.RawJson;

            _apiUrl = api.GetApiUrl(methodName, parameters);
            llVkApiUrl.Text = _apiUrl.Length > 80 ? _apiUrl.Substring(0, 80) + "..." : _apiUrl;

            btnRun.Enabled = btnGetTest.Enabled = true;
        }
Example #2
0
        public string GenerateUnitTest(string categoryName, string accessToken)
        {
            // 1. read assembly
            // 2. find methods in particular category
            // 3. find methods with not input parameters (it's the most simple case)
            // 4. invoke a method
            // 5. construct unit-test
            // 6. return it

            Assembly dll = Assembly.LoadFrom("VkNet.dll");

            // todo refactor it later
            List<Type> classes = dll.GetTypes().Where(t => t.Name.Contains("Category") && t.Name.Contains(categoryName)).ToList();

            Type category = classes.FirstOrDefault();

            MethodInfo[] methods = category.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);

            var genMethods = new List<VkMethodGenInfo>();
            foreach (var methodInfo in methods)
            {
                var genInfo = GetMethodData(methodInfo);

                if (string.IsNullOrEmpty(genInfo.ApiMethod)) continue;

                Debug.WriteLine(genInfo.ToString());

                genMethods.Add(genInfo);

            }

            // invoke and get json and url
            var api = new VkApi();

            api.Authorize(accessToken);
            // TODO must be authorized

            var unittests = new List<UnitTestInfo>();

            var testCategory = new StringBuilder();
            foreach (var m in genMethods)
            {
                if (m.Skip) continue;

                var test = new UnitTestInfo
                {
                    Name = string.Format("{0}_", m.Name),
                    Url = Utilities.PreetyPrintApiUrl(api.GetApiUrl(m.ApiMethod, m.Params))
                };

                // TODO refactor this shit
                int index = test.Url.IndexOf("access_token", StringComparison.InvariantCulture);
                if (index != -1)
                {
                    test.Url = test.Url.Substring(0, index) + "access_token=token\";";
                }

                test.Json = Utilities.PreetyPrintJson(api.Invoke(m.ApiMethod, m.Params));

                unittests.Add(test);

                testCategory.Append(test);
            }

            File.WriteAllText(@"d:\vk.txt", testCategory.ToString());

            return string.Empty;

            //throw new NotImplementedException();
        }