public void Execute(Func <IRestClient, IRestResponse> execute) { var client = _factory.BuildClient(); var response = execute(client); if (response.StatusCode != HttpStatusCode.OK) { throw new ApiException($"Execute failed. Status code {response.StatusCode}"); } if (response.ErrorException != null) { throw new ApiException(response.ErrorMessage, response.ErrorException); } }
private void SendNotification(string title, string message, RestRequest request, BoxcarSettings settings) { try { var client = _restClientFactory.BuildClient(URL); request.AddParameter("user_credentials", settings.Token); request.AddParameter("notification[title]", title); request.AddParameter("notification[long_message]", message); request.AddParameter("notification[source_name]", BuildInfo.AppName); request.AddParameter("notification[icon_url]", "https://raw.githubusercontent.com/Sonarr/Sonarr/7818f0c59b787312f0bcbc5c0eafc3c9dd7e5451/Logo/64.png"); client.ExecuteAndValidate(request); } catch (RestException ex) { if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) { _logger.Error(ex, "Access Token is invalid"); throw; } throw new BoxcarException("Unable to send text message: " + ex.Message, ex); } }
public void SendNotification(string title, string message, PushoverSettings settings) { var client = _restClientFactory.BuildClient(URL); var request = new RestRequest(Method.POST); request.AddParameter("token", settings.ApiKey); request.AddParameter("user", settings.UserKey); request.AddParameter("device", string.Join(",", settings.Devices)); request.AddParameter("title", title); request.AddParameter("message", message); request.AddParameter("priority", settings.Priority); if ((PushoverPriority)settings.Priority == PushoverPriority.Emergency) { request.AddParameter("retry", settings.Retry); request.AddParameter("expire", settings.Expire); } if (!settings.Sound.IsNullOrWhiteSpace()) { request.AddParameter("sound", settings.Sound); } client.ExecuteAndValidate(request); }
private void SendNotification(string title, string message, RestRequest request, JoinSettings settings) { var client = _restClientFactory.BuildClient(URL); if (settings.DeviceNames.IsNotNullOrWhiteSpace()) { request.AddParameter("deviceNames", settings.DeviceNames); } else if (settings.DeviceIds.IsNotNullOrWhiteSpace()) { 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. request.AddParameter("smallicon", "https://cdn.rawgit.com/Sonarr/Sonarr/develop/Logo/96-Outline-White.png"); // 96x96px with outline at 88x88px on a transparent background. request.AddParameter("priority", settings.Priority); 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."); }
public void SendNotification(string title, string message, GotifySettings settings) { var client = _restClientFactory.BuildClient(settings.Server); var request = new RestRequest("message", Method.POST); request.AddQueryParameter("token", settings.AppToken); request.AddParameter("title", title); request.AddParameter("message", message); request.AddParameter("priority", settings.Priority); client.ExecuteAndValidate(request); }
private IRestClient BuildClient(XbmcSettings settings) { var url = string.Format(@"http://{0}/jsonrpc", settings.Address); var client = _restClientFactory.BuildClient(url); if (!settings.Username.IsNullOrWhiteSpace()) { client.Authenticator = new HttpBasicAuthenticator(settings.Username, settings.Password); } return(client); }
public List <PushBulletDevice> GetDevices(PushBulletSettings settings) { try { var client = _restClientFactory.BuildClient(DEVICE_URL); var request = new RestRequest(Method.GET); client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty); var response = client.ExecuteAndValidate(request); return(Json.Deserialize <PushBulletDevicesResponse>(response.Content).Devices); } catch (RestException ex) { if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) { _logger.Error(ex, "Access token is invalid"); throw; } } return(new List <PushBulletDevice>()); }
public void SendNotification(string title, string message, TelegramSettings settings) { //Format text to add the title before and bold using markdown var text = $"<b>{HttpUtility.HtmlEncode(title)}</b>\n{HttpUtility.HtmlEncode(message)}"; var client = _restClientFactory.BuildClient(URL); var request = new RestRequest("bot{token}/sendmessage", Method.POST); request.AddUrlSegment("token", settings.BotToken); request.AddParameter("chat_id", settings.ChatId); request.AddParameter("parse_mode", "HTML"); request.AddParameter("text", text); client.ExecuteAndValidate(request); }