/// <summary> /// Delete Account Worker /// </summary> /// <returns></returns> public async void DeleteAccountWorkers(AccountWorkers accountWorkers) { string apiURL = APIConstants.APIURL + APIEndpoints.DeletetAccountWorkers; try { // Serialize our concrete class into a JSON String var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(accountWorkers)); using (var httpClient = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage { Content = new StringContent(stringPayload, Encoding.UTF8, "application/json"), Method = HttpMethod.Delete, RequestUri = new Uri(apiURL) }; await httpClient.SendAsync(request); } } catch (Exception e) { logger.Error(e, $"Unable to delete account workers from {apiURL}"); } }
// Remove worker from list private void RemoveAccountWorkers(object param) { try { if (param != null) { AccountWorkers accountWorkersRemove = (AccountWorkers)param; // Delete worker from account using API AccountWorkersAPI accountWorkersAPI = new AccountWorkersAPI(); accountWorkersAPI.DeleteAccountWorkers(accountWorkersRemove); // Remove worker from local list of workers AccountWorkersList.Remove(accountWorkersRemove); // Notify UI of change OnPropertyChanged("AccountWorkersList"); // Update worker list on main window _mainWindowViewModel.GetAccountWorkerList(); // Notify success ShowSuccess(string.Format("Worker {0} successfully removed", accountWorkersRemove.WorkerName)); } } catch (Exception e) { throw new ApplicationException(string.Format("Error removing worker from account"), e); } }
/// <summary> /// Post Account Worker /// </summary> /// <returns></returns> public async void PostAccountWorkers(AccountWorkers accountWorkers) { string apiURL = APIConstants.APIURL + APIEndpoints.PostAccountWorkers; try { // Serialize our concrete class into a JSON String var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(accountWorkers)); // Wrap our JSON inside a StringContent which then can be used by the HttpClient class var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json"); using (var httpClient = new HttpClient()) { // Do the actual request and await the response var httpResponse = await httpClient.PostAsync(apiURL, httpContent); // If the response contains content we want to read it! if (httpResponse.Content != null) { var responseContent = await httpResponse.Content.ReadAsStringAsync(); } } } catch (Exception e) { logger.Error(e, $"Unable to post account workers from {apiURL}"); } }
/// <summary> /// Insert a new worker for account /// </summary> private void InsertAccountWorkers() { try { if (Application.Current.Properties["AccountID"] != null && WorkerSettings.WorkerName != null) { // Reload Account Work List InitAccountWorkers(); // Create new worker object AccountWorkers accountWorker = new AccountWorkers(); accountWorker.AccountGuid = (Guid)Application.Current.Properties["AccountID"]; accountWorker.WorkerName = WorkerSettings.WorkerName; // Insert new worker for account into API AccountWorkersAPI accountWorkersAPI = new AccountWorkersAPI(); accountWorkersAPI.PostAccountWorkers(accountWorker); // Insert worker into local list if it doesnt already exist List <AccountWorkers> AccountWorkersListMatches = AccountWorkersList.Where(x => x.AccountGuid == accountWorker.AccountGuid && x.WorkerName == accountWorker.WorkerName).ToList(); if (AccountWorkersListMatches.Count == 0) { // Add new worker to local list AccountWorkersList.Add(accountWorker); } // Update worker list on main window _mainWindowViewModel.GetAccountWorkerList(); // Set Label on Main Window _mainWindowViewModel.LabelMainTitle = string.Format("BITPOOL MINER - {0}", WorkerSettings.WorkerName); // Notify UI of change OnPropertyChanged("AccountWorkersList"); } } catch (Exception e) { throw new ApplicationException(string.Format("Error inserting worker to account"), e); } }