コード例 #1
0
        /// <summary>
        /// Perform QuantConnect authentication
        /// </summary>
        /// <param name="serviceProvider">Visual Studio services provider</param>
        /// <param name="explicitLogin">User explicitly clicked Log In button</param>
        /// <param name="showLoginDialog">If true LoginDialog will be shown, careful it blocks UI</param>
        /// <returns>true if user logged into QuantConnect, false otherwise</returns>
        public async Task <bool> Login(IServiceProvider serviceProvider, bool explicitLogin, bool showLoginDialog = true)
        {
            VSActivityLog.Info("Logging in");

            var authorizationManager = AuthorizationManager.GetInstance();

            if (authorizationManager.IsLoggedIn())
            {
                VSActivityLog.Info("Already logged in");
                return(true);
            }

            var previousCredentials = CredentialsManager.GetLastCredential();

            if (!explicitLogin && await LoggedInWithLastStorredPassword(previousCredentials))
            {
                VSActivityLog.Info("Logged in with previously storred credentials");
                return(true);
            }

            if (showLoginDialog)
            {
                return(LoginWithDialog(serviceProvider, previousCredentials));
            }
            VsUtils.DisplayInStatusBar(serviceProvider, "Please login to QuantConnect");
            return(false);
        }
コード例 #2
0
        private void Login_Click(object sender, RoutedEventArgs e)
        {
            VSActivityLog.Info("Login button clicked");
            try
            {
                // Disable textbox passwordBox and login button
                userIdBox.IsReadOnly     = true;
                accessTokenBox.IsEnabled = false;
                logInButton.IsEnabled    = false;

                var userId      = userIdBox.Text;
                var accessToken = accessTokenBox.Password;
                var credentials = new Credentials(userId, accessToken);
                if (_authorizationManager.Login(credentials))
                {
                    VSActivityLog.Info("Logged in successfully");
                    VsUtils.DisplayInStatusBar(_serviceProvider, "Logged into QuantConnect");
                    _credentials = new Credentials(userId, accessToken);
                    Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                VsUtils.ShowMessageBox(_serviceProvider, "QuantConnect Login Exception", ex.ToString());
                VSActivityLog.Error(ex.ToString());
            }
            VsUtils.DisplayInStatusBar(_serviceProvider, "Failed to login");
            userIdBox.BorderBrush      = Brushes.Red;
            accessTokenBox.BorderBrush = Brushes.Red;
            // Re enable button and textbox
            userIdBox.IsReadOnly     = false;
            accessTokenBox.IsEnabled = true;
            logInButton.IsEnabled    = true;
        }
コード例 #3
0
 /// <summary>
 /// Get an authenticated API instance.
 /// </summary>
 /// <returns>Authenticated API instance</returns>
 /// <exception cref="InvalidOperationException">API is not authenticated</exception>
 public Api.Api GetApi()
 {
     if (_api == null)
     {
         VSActivityLog.Error("Accessing API without logging in first");
         throw new InvalidOperationException("Accessing API without logging in first");
     }
     return(_api);
 }
コード例 #4
0
ファイル: LogInDialog.xaml.cs プロジェクト: pettyfun/Lean
        /// <summary>
        /// Create LoginDialog
        /// </summary>
        /// <param name="authorizationManager">Authorization manager</param>
        /// <param name="previousCredentials">User previous credentials</param>
        public LoginDialog(AuthorizationManager authorizationManager, Credentials?previousCredentials)
        {
            VSActivityLog.Info("Created login dialog");
            InitializeComponent();
            _authorizationManager = authorizationManager;

            DisplayPreviousCredentials(previousCredentials);
            _userIdNormalBrush      = userIdBox.BorderBrush;
            _accessTokenNormalBrush = userIdBox.BorderBrush;
        }
コード例 #5
0
        /// <summary>
        /// Uploads a list of files to a specific project at QuantConnect
        /// </summary>
        /// <param name="selectedProjectId">Target project Id</param>
        /// <param name="files">List of files to upload</param>
        /// <returns>Returns false if any file failed to be uploaded</returns>
        private bool UploadFilesToServer(int selectedProjectId, IEnumerable <SelectedItem> files)
        {
            VsUtils.DisplayInStatusBar(_serviceProvider, "Uploading files to server...");
            var api = AuthorizationManager.GetInstance().GetApi();
            // Counters to keep track of files uploaded or not
            var filesUploaded    = 0;
            var filesNotUploaded = 0;

            foreach (var file in files)
            {
                api.DeleteProjectFile(selectedProjectId, file.FileName);
                try
                {
                    var fileContent = File.ReadAllText(file.FilePath);
                    var response    = api.AddProjectFile(selectedProjectId, file.FileName, fileContent);
                    if (response.Success)
                    {
                        filesUploaded++;
                    }
                    else
                    {
                        VSActivityLog.Error("Failed to add project file " + file.FileName);
                        filesNotUploaded++;
                    }
                }
                catch (Exception exception)
                {
                    VSActivityLog.Error("Exception adding project file " + file.FileName + ". Exception " + exception);
                    filesNotUploaded++;
                }
            }
            // Update Status bar accordingly based on counters
            var message = "Files upload complete";

            message += (filesUploaded != 0) ? ". Uploaded " + filesUploaded : "";
            message += (filesNotUploaded != 0) ? ". Failed to upload " + filesNotUploaded : "";
            VsUtils.DisplayInStatusBar(_serviceProvider, message);

            // Return false if any file failed to be uploaded
            var result = filesNotUploaded == 0;

            if (!result)
            {
                VsUtils.ShowErrorMessageBox(_serviceProvider, "Upload Files Failed", message);
            }
            return(result);
        }
コード例 #6
0
 /// <summary>
 /// Authenticate API
 /// </summary>
 /// <param name="credentials">User id and access token to authenticate the API</param>
 /// <returns>true if successfully authenticated API, false otherwise</returns>
 public bool Login(Credentials credentials)
 {
     VSActivityLog.Info("Authenticating QuantConnect API");
     try
     {
         var api = new Api.Api();
         api.Initialize(int.Parse(credentials.UserId), credentials.AccessToken, Globals.DataFolder);
         if (api.Connected)
         {
             _api = api;
             return(true);
         }
     }
     catch (FormatException)
     {
         VSActivityLog.Error("User id is not a valid number");
     }
     return(false);
 }
コード例 #7
0
        private bool LoginWithDialog(IServiceProvider serviceProvider, Credentials?previousCredentials)
        {
            var logInDialog = new LoginDialog(AuthorizationManager.GetInstance(), previousCredentials, serviceProvider);

            VsUtils.DisplayDialogWindow(logInDialog);

            var credentials = logInDialog.GetCredentials();

            if (credentials.HasValue)
            {
                VSActivityLog.Info("Storing credentials");
                CredentialsManager.StoreCredentials(credentials.Value);
                return(true);
            }
            else
            {
                VSActivityLog.Info("Login cancelled");
                return(false);
            }
        }
コード例 #8
0
ファイル: LogInDialog.xaml.cs プロジェクト: pettyfun/Lean
        private void Login_Click(object sender, RoutedEventArgs e)
        {
            VSActivityLog.Info("Log in button clicked");
            logInButton.IsEnabled = false;
            var userId      = userIdBox.Text;
            var accessToken = accessTokenBox.Password;
            var credentials = new Credentials(userId, accessToken);

            if (_authorizationManager.Login(credentials))
            {
                VSActivityLog.Info("Logged in successfully");
                _credentials = new Credentials(userId, accessToken);
                Close();
            }
            else
            {
                VSActivityLog.Error("Failed to login");
                userIdBox.BorderBrush      = Brushes.Red;
                accessTokenBox.BorderBrush = Brushes.Red;
            }
            logInButton.IsEnabled = true;
        }
コード例 #9
0
        /// <summary>
        /// Perform QuantConnect authentication
        /// </summary>
        /// <param name="serviceProvider">Visual Studio services provider</param>
        /// <param name="explicitLogin">User explicitly clicked Log In button</param>
        /// <returns>true if user logged into QuantConnect, false otherwise</returns>
        public bool Login(IServiceProvider serviceProvider, bool explicitLogin)
        {
            VSActivityLog.Info("Logging in");

            var authorizationManager = AuthorizationManager.GetInstance();

            if (authorizationManager.IsLoggedIn())
            {
                VSActivityLog.Info("Already logged in");
                return(true);
            }

            var previousCredentials = CredentialsManager.GetLastCredential();

            if (!explicitLogin && LoggedInWithLastStorredPassword(previousCredentials))
            {
                VSActivityLog.Info("Logged in with previously storred credentials");
                return(true);
            }

            return(LoginWithDialog(serviceProvider, previousCredentials));
        }
コード例 #10
0
        /// <summary>
        /// Authenticate API
        /// </summary>
        /// <param name="credentials">User id and access token to authenticate the API</param>
        /// <returns>true if successfully authenticated API, false otherwise</returns>
        public async System.Threading.Tasks.Task <bool> Login(Credentials credentials)
        {
            VSActivityLog.Info("Authenticating QuantConnect API");
            try
            {
                var api = new Api.Api();
                api.Initialize(int.Parse(credentials.UserId), credentials.AccessToken, Globals.DataFolder);
                var apiConnected = await System.Threading.Tasks.Task.Run(() => api.Connected);

                if (apiConnected)
                {
                    _api = api;
                    return(true);
                }
            }
            catch (Exception exception)
            {
                VsUtils.ShowErrorMessageBox(ServiceProvider.GlobalProvider,
                                            "QuantConnect Exception", exception.ToString());
            }
            return(false);
        }