/// <summary>
        /// Tries to log the user in with the given credentials.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The <see cref="PasswordBox"/> containing the password.</param>
        public async Task Login(string username, PasswordBox password)
        {
            try
            {
                EnableControls = false;
                var response = await _lastAuth.GetSessionTokenAsync(username, password.Password);

                if (response.Success && _lastAuth.Authenticated)
                {
                    _messageBoxService.ShowDialog("Successfully logged in and authenticated!");
                    TryClose(true);
                }
                else
                {
                    _messageBoxService.ShowDialog("Failed to log in or authenticate!");
                }
            }
            catch (Exception ex)
            {
                _messageBoxService.ShowDialog("Fatal error while trying to log in: " + ex.Message);
            }
            finally
            {
                EnableControls = true;
            }
        }
        private async Task ExecuteSelectedCommand()
        {
            if (ExecutingCommand)
            {
                return;
            }

            ExecutingCommand = true;

            try
            {
                // build up the command<response<lastobject>>
                var responseType = SelectedResponseType.MakeGenericType(SelectedLastObjectType);
                var genericType  = SelectedBaseCommandType.MakeGenericType(responseType);

                if (!_lastAuth.Authenticated)
                {
                    await _lastAuth.GetSessionTokenAsync("tehrikkit", "#facedusk17a");
                }

                var instance = Activator.CreateInstance(genericType, _lastAuth);

                var parameters = CommandParameters
                                 .Where(pair => !string.IsNullOrWhiteSpace(pair.Key) && !string.IsNullOrWhiteSpace(pair.Value))
                                 .ToDictionary(pair => pair.Key, pair => pair.Value);

                var methodProperty = genericType.GetProperty("Method", BindingFlags.Public | BindingFlags.Instance);
                methodProperty.SetValue(instance, CommandMethodName);

                if (SelectedResponseType == typeof(PageResponse <>))
                {
                    var pageProperty = genericType.GetProperty("Page", BindingFlags.Public | BindingFlags.Instance);
                    pageProperty.SetValue(instance, int.Parse(CommandPageNumber));

                    var countProperty = genericType.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
                    countProperty.SetValue(instance, int.Parse(CommandItemCount));
                }

                var parametersProperty = genericType.GetProperty("Parameters",
                                                                 BindingFlags.Public | BindingFlags.Instance);
                parametersProperty.SetValue(instance, parameters);

                // execute
                var executeMethod = genericType.GetMethods().First(m => m.Name == "ExecuteAsync");
                await(dynamic) executeMethod.Invoke(instance, null);

                // cast so we can get the Json response
                var dummyCommand = (IDummyCommand)instance;
                var jo           = dummyCommand.Response;

                var formattedJson = jo.ToString(Formatting.Indented);

                // writeout to file
                var filename = string.Format("syro-{0}-{1}.json", jo.Properties().First().Name,
                                             DateTime.Now.ToString("yyMMdd-HHmmss"));
                var tempDirPath = Path.GetFullPath(SolutionDir + "tmp/");

                if (!Directory.Exists(tempDirPath))
                {
                    Directory.CreateDirectory(tempDirPath);
                }
                var path = Path.GetFullPath(tempDirPath + filename);

                // write to output directory and launch
                using (var fs = new FileStream(path, FileMode.Create))
                {
                    using (var sw = new StreamWriter(fs))
                    {
                        sw.Write(formattedJson);
                    }
                }
                Process.Start(path);

                CommandResult = formattedJson;
            }
            finally
            {
                ExecutingCommand = false;
            }
        }
Exemple #3
0
        private async Task ExecuteSelectedCommand()
        {
            if (ExecutingCommand)
            {
                return;
            }

            ExecutingCommand = true;

            try
            {
                // build up the command<response<lastobject>>
                var responseType = _state.SelectedResponseType.MakeGenericType(_state.SelectedLastObjectType);
                var genericType  = _state.SelectedBaseCommandType.MakeGenericType(responseType);

                if ((_lastAuth.UserSession == null || _lastAuth.UserSession.Username != _state.LastUsername) &&
                    _state.SelectedBaseCommandType == typeof(DummyPostAsyncCommand <>))
                {
                    await _lastAuth.GetSessionTokenAsync(_state.LastUsername, _state.LastPassword);
                }

                var instance = Activator.CreateInstance(genericType, _lastAuth);

                var parameters = CommandParameters
                                 .Where(pair => !string.IsNullOrWhiteSpace(pair.Key) && !string.IsNullOrWhiteSpace(pair.Value))
                                 .ToDictionary(pair => pair.Key, pair => pair.Value);

                var methodProperty = genericType.GetProperty("Method", BindingFlags.Public | BindingFlags.Instance);
                methodProperty.SetValue(instance, _state.CommandMethodName);

                if (_state.SelectedResponseType == typeof(PageResponse <>) ||
                    _state.CommandMethodName.EndsWith("s"))    // yolo
                {
                    var pageProperty = genericType.GetProperty("Page", BindingFlags.Public | BindingFlags.Instance);
                    pageProperty.SetValue(instance, int.Parse(_state.CommandPageNumber));

                    var countProperty = genericType.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
                    countProperty.SetValue(instance, int.Parse(_state.CommandItemCount));
                }

                var httpClientProperty = genericType.GetProperty("HttpClient", BindingFlags.Public | BindingFlags.Instance);
                httpClientProperty.SetValue(instance, new HttpClient());

                var parametersProperty = genericType.GetProperty("Parameters",
                                                                 BindingFlags.Public | BindingFlags.Instance);
                parametersProperty.SetValue(instance, parameters);

                // execute
                var executeMethod = genericType.GetMethods().First(m => m.Name == "ExecuteAsync");
                await(dynamic) executeMethod.Invoke(instance, null);

                // cast so we can get the Json response
                var dummyCommand = (IDummyCommand)instance;
                var jo           = dummyCommand.Response;

                var formattedJson = jo.ToString(Formatting.Indented);

                // writeout to file
                var filename = string.Format("syro-{0}-{1}.json", _state.CommandMethodName.Replace(".", "-"),
                                             DateTime.Now.ToString("yyMMdd-HHmmss"));

                SaveToTempAndLoad(filename, formattedJson);

                CommandResult = formattedJson;
            }
            catch (Exception e)
            {
                var filename = String.Format("syro-exception-{0}.txt", DateTime.Now.ToString("yyMMdd-HHmmss"));
                SaveToTempAndLoad(filename, e.ToString());
            }
            finally
            {
                ExecutingCommand = false;
            }
        }