public ValidationFailure Test(JoinSettings settings) { const string title = "Test Notification"; const string body = "This is a test message from Sonarr."; try { SendNotification(title, body, settings); return null; } catch(JoinInvalidDeviceException ex) { _logger.Error(ex, "Unable to send test Join message. Invalid Device IDs supplied."); return new ValidationFailure("DeviceIds", "Device IDs appear invalid."); } catch (JoinException ex) { _logger.Error(ex, "Unable to send test Join message."); return new ValidationFailure("ApiKey", ex.Message); } catch(RestException ex) { _logger.Error(ex, "Unable to send test Join message. Server connection failed."); return new ValidationFailure("ApiKey", "Unable to connect to Join API. Please try again later."); } catch (Exception ex) { _logger.Error(ex, "Unable to send test Join message. Unknown error."); return new ValidationFailure("ApiKey", ex.Message); } }
public void SendNotification(string title, string message, JoinSettings settings) { var request = new RestRequest(Method.GET); try { SendNotification(title, message, request, settings); } catch (JoinException ex) { _logger.Error(ex, "Unable to send Join message."); throw ex; } }
private void SendNotification(string title, string message, RestRequest request, JoinSettings settings) { var client = RestClientFactory.BuildClient(URL); if (!string.IsNullOrEmpty(settings.DeviceIds)) { request.AddParameter("deviceIds", settings.DeviceIds); } else { request.AddParameter("deviceId", "group.all"); } request.AddParameter("apikey", settings.ApiKey); request.AddParameter("title", title); request.AddParameter("text", message); request.AddParameter("icon", "https://cdn.rawgit.com/Sonarr/Sonarr/develop/Logo/256.png"); // Use the Sonarr logo. var response = client.ExecuteAndValidate(request); var res = Json.Deserialize<JoinResponseModel>(response.Content); if (res.success) return; if (res.userAuthError) { throw new JoinAuthException("Authentication failed."); } if (res.errorMessage != null) { // Unfortunately hard coding this string here is the only way to determine that there aren't any devices to send to. // There isn't an enum or flag contained in the response that can be used instead. if (res.errorMessage.Equals("No devices to send to")) { throw new JoinInvalidDeviceException(res.errorMessage); } // Oddly enough, rather than give us an "Invalid API key", the Join API seems to assume the key is valid, // but fails when doing a device lookup associated with that key. // In our case we are using "deviceIds" rather than "deviceId" so when the singular form error shows up // we know the API key was the fault. else if (res.errorMessage.Equals("No device to send message to")) { throw new JoinAuthException("Authentication failed."); } throw new JoinException(res.errorMessage); } throw new JoinException("Unknown error. Join message failed to send."); }
private void trvExtensions_AfterSelect(object sender, TreeViewEventArgs e) { if (_edSvc.IsNew) { return; } var ext = e.Node.Tag as IFeatureSourceExtension; var join = e.Node.Tag as IAttributeRelation; var calc = e.Node.Tag as ICalculatedProperty; if (ext != null) { var ctl = new ExtendedClassSettings(_fs, GetAllClassNames(), ext); ctl.Dock = DockStyle.Fill; //If editing to something valid, update the toolbar ctl.ResourceChanged += (s, evt) => { btnNewJoin.Enabled = btnNewCalculation.Enabled = IsValidExtension(ext); }; splitContainer1.Panel2.Controls.Clear(); splitContainer1.Panel2.Controls.Add(ctl); btnDelete.Enabled = true; btnNewCalculation.Enabled = btnNewJoin.Enabled = IsValidExtension(ext); } else if (join != null) { ext = e.Node.Parent.Tag as IFeatureSourceExtension; if (ext != null) { if (ext.FeatureClass != null) { //NOTE: The feature source id here may be session based, but this is still okay //as we're only giving context (the primary class to join on) for the secondary join UI. //This feature source id is never written into the actual document var ctl = new JoinSettings(_fs.ResourceID, ext.FeatureClass, join); ctl.Bind(_edSvc); ctl.Dock = DockStyle.Fill; splitContainer1.Panel2.Controls.Clear(); splitContainer1.Panel2.Controls.Add(ctl); btnDelete.Enabled = true; } } } else if (calc != null) { ext = e.Node.Parent.Tag as IFeatureSourceExtension; if (ext != null) { ClassDefinition cls = _edSvc.CurrentConnection.FeatureService.GetClassDefinition(_fs.ResourceID, ext.FeatureClass); //TODO: Cache? if (cls != null) { var ctl = new CalculationSettings(_edSvc, cls, _fs, calc); ctl.Dock = DockStyle.Fill; splitContainer1.Panel2.Controls.Clear(); splitContainer1.Panel2.Controls.Add(ctl); btnDelete.Enabled = true; } } } else { splitContainer1.Panel2.Controls.Clear(); } }