public ValidationFailure Test(PushBulletSettings settings) { try { const string title = "Sonarr - Test Notification"; const string body = "This is a test message from Sonarr"; SendNotification(title, body, settings.ApiKey, settings.DeviceId); } catch (RestException ex) { if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) { _logger.ErrorException("API Key is invalid: " + ex.Message, ex); return new ValidationFailure("ApiKey", "API Key is invalid"); } _logger.ErrorException("Unable to send test message: " + ex.Message, ex); return new ValidationFailure("ApiKey", "Unable to send test message"); } catch (Exception ex) { _logger.ErrorException("Unable to send test message: " + ex.Message, ex); return new ValidationFailure("", "Unable to send test message"); } return null; }
public async Task <IActionResult> OnPost() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Error: Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (Input.Enable) { if (Input.Enable && Input.ApiKey.IsNullOrWhiteSpace()) { ModelState.AddModelError("ApiKey", "Api Key is required."); } if (Input.Enable && Input.EncryptionKey.IsNullOrWhiteSpace()) { ModelState.AddModelError("EncryptionKey", "Encryption Key is required."); } if (!ModelState.IsValid) { StatusMessage = "Error: Please review the errors below."; return(Page()); } var encryptedApiKey = Input.HiddenEncryptedApiKey; var encryptedEncryptionKey = Input.HiddenEncryptedEncryptionKey; if (Input.HiddenEncryptedApiKey != Input.ApiKey) { encryptedApiKey = _cipherService.Encrypt(Input.ApiKey); } if (Input.HiddenEncryptedEncryptionKey != Input.EncryptionKey) { encryptedEncryptionKey = _cipherService.Encrypt(Input.EncryptionKey); } var pushBulletSettings = new PushBulletSettings() { ApiKey = encryptedApiKey, EncryptionKey = encryptedEncryptionKey }; var pushBulletSettingsString = JsonConvert.SerializeObject(pushBulletSettings); var success = await _pushBulletAppService.AddOrUpdatePushBulletSettings(user.Id, pushBulletSettingsString); StatusMessage = success ? "Your PushBullet Settings have been encrypted and updated!" : "Error: Unable to save your settings at this time."; } else { var success = await _pushBulletAppService.RemovePushBulletService(user.Id); StatusMessage = success ? "Your PushBullet integration has been removed." : "Error: Unable to remove PushBullet integration at this time."; } return(RedirectToPage()); }
public void SendNotification(string title, string message, PushBulletSettings settings) { var error = false; if (settings.ChannelTags.Any()) { foreach (var channelTag in settings.ChannelTags) { var request = BuildChannelRequest(channelTag); try { SendNotification(title, message, request, settings); } catch (PushBulletException ex) { _logger.Error(ex, "Unable to send test message to: " + channelTag); error = true; } } } else { if (settings.DeviceIds.Any()) { foreach (var deviceId in settings.DeviceIds) { var request = BuildDeviceRequest(deviceId); try { SendNotification(title, message, request, settings); } catch (PushBulletException ex) { _logger.Error(ex, "Unable to send test message to: " + deviceId); error = true; } } } else { var request = BuildDeviceRequest(null); try { SendNotification(title, message, request, settings); } catch (PushBulletException ex) { _logger.Error(ex, "Unable to send test message to all devices"); error = true; } } } if (error) { throw new PushBulletException("Unable to send PushBullet notifications to all channels or devices"); } }
private void SendNotification(string title, string message, RestRequest request, PushBulletSettings settings) { try { var client = RestClientFactory.BuildClient(URL); request.AddParameter("type", "note"); request.AddParameter("title", title); request.AddParameter("body", message); if (settings.SenderId.IsNotNullOrWhiteSpace()) { request.AddParameter("source_device_iden", settings.SenderId); } client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty); client.ExecuteAndValidate(request); } catch (RestException ex) { if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) { _logger.Error(ex, "API Key is invalid: " + ex.Message); throw; } throw new PushBulletException("Unable to send text message: {0}", ex, ex.Message); } }