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; }
/// <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); }
/// <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); }
/// <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); }
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; }