private async void ValidateAPIKeyButton_Click(object sender, RoutedEventArgs e) { resetForm(); // Check Input if (string.IsNullOrWhiteSpace(APIKeyTextBox.Text)) { MessageBox.Show("Please enter an API Key!"); return; } try { Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; // API object for this request var api = new Clockify(_client); // Get User _user = await api.GetGeneric <User>(APIKeyTextBox.Text, "user"); if (_user == null) { throw new ApplicationException("Failed to get the user for an unknown reason."); } UsernameTextBox.Text = _user.Email; // Save the API Key for next time this settings dialog is opened Properties.Settings.Default.LastConfiguredAPIKey = APIKeyTextBox.Text; Properties.Settings.Default.Save(); // Get Workspaces _workspaces = await api.GetGeneric <List <Workspace> >(APIKeyTextBox.Text, "workspaces"); if (_workspaces == null) { throw new ApplicationException("Failed to get the workspaces for an unknown reason."); } WorkspaceComboBox.Items.Clear(); WorkspaceComboBox.ItemsSource = _workspaces; WorkspaceComboBox.DisplayMemberPath = "Name"; WorkspaceComboBox.Items.Refresh(); WorkspaceComboBox.IsEnabled = true; WorkspaceComboBox.IsReadOnly = false; SelectWorkspaceButton.IsEnabled = true; } catch (Exception ex) { Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow; MessageBox.Show("ERROR: " + ex.Message); return; } finally { Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow; } }
private async void ClockOutWindow_ContentRendered(object sender, EventArgs e) { try { Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; this.IsEnabled = false; var api = new Clockify(_client); _user = await api.GetGeneric <User>(_opts.APIKey, "user"); if (_user == null) { throw new ApplicationException("Failed to get the user for an unknown reason."); } UsernameTextBox.Text = _user.Email; _workspaces = await api.GetGeneric <List <Workspace> >(_opts.APIKey, "workspaces"); if (_workspaces == null) { throw new ApplicationException("Failed to get the workspaces for an unknown reason"); } var workspace = _workspaces.FirstOrDefault(x => x.Id == _opts.Workspace); if (workspace == null) { throw new ApplicationException("You do not have access to a workspace with that ID"); } WorkspaceTextBox.Text = workspace.Name; var timeEntries = await api.GetGeneric <List <TimeEntry> >(_opts.APIKey, "workspaces/" + _opts.Workspace + "/user/" + _user.Id + "/time-entries?in-progress=true"); if (timeEntries == null) { throw new ApplicationException("Failed to get the time entries for an unknown reason"); } if (timeEntries.Count == 0) { throw new ApplicationException("You are not clocked in right now!"); } else if (timeEntries.Count > 1) { throw new ApplicationException("You are clocked in multiple times right now in the same workspace. I don\'t know how to handle that!"); } _timeEntry = timeEntries.First(); ClockStartedTextBox.Text = _timeEntry.TimeInterval.Start; _projects = await api.GetGeneric <List <Project> >(_opts.APIKey, "workspaces/" + _opts.Workspace + "/projects?archived=false"); if (_projects == null) { throw new ApplicationException("Failed to get the projects for an unknown reason"); } _filteredProjects = new List <KeyValuePair <string, Project> >(); foreach (var p in _projects) { _filteredProjects.Add(new KeyValuePair <string, Project>(p.Name + " - " + p.ClientName, p)); } _filteredProjects = _filteredProjects.OrderBy(x => x.Value.ClientName).ThenBy(x => x.Value.Name).ToList(); ProjectResultsListBox.ItemsSource = _filteredProjects; ProjectResultsListBox.DisplayMemberPath = "Key"; ProjectResultsListBox.Items.Refresh(); _tags = await api.GetGeneric <List <Tag> >(_opts.APIKey, "workspaces/" + _opts.Workspace + "/tags?archived=false"); if (_tags == null) { throw new ApplicationException("Failed to get the tags for any unknown reason"); } _tags = _tags.OrderBy(x => x.Name).ToList(); TagComboBox.ItemsSource = _tags; TagComboBox.DisplayMemberPath = "Name"; TagComboBox.Items.Refresh(); } catch (Exception ex) { Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow; MessageBox.Show("ERROR: " + ex.Message); this.Close(); } finally { Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow; this.IsEnabled = true; DescriptionTextBox.Focus(); } }