Exemple #1
0
        public ValidationFailure Test(PushBulletSettings settings)
        {
            try
            {
                const string title = "Radarr - Test Notification";
                const string body  = "This is a test message from Radarr";

                SendNotification(title, body, settings);
            }
            catch (RestException ex)
            {
                if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    _logger.Error(ex, "API Key is invalid: " + ex.Message);
                    return(new ValidationFailure("ApiKey", "API Key is invalid"));
                }

                _logger.Error(ex, "Unable to send test message: " + ex.Message);
                return(new ValidationFailure("ApiKey", "Unable to send test message"));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Unable to send test message: " + ex.Message);
                return(new ValidationFailure("", "Unable to send test message"));
            }

            return(null);
        }
        public List <PushBulletDevice> GetDevices(PushBulletSettings settings)
        {
            try
            {
                var requestBuilder = new HttpRequestBuilder(DEVICE_URL);

                var request = requestBuilder.Build();

                request.Method      = HttpMethod.Get;
                request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);

                var response = _httpClient.Execute(request);

                return(Json.Deserialize <PushBulletDevicesResponse>(response.Content).Devices);
            }
            catch (HttpException ex)
            {
                if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    _logger.Error(ex, "Access token is invalid");
                    throw;
                }
            }

            return(new List <PushBulletDevice>());
        }
Exemple #3
0
        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>());
        }
Exemple #4
0
        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");
            }
        }
Exemple #5
0
        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);
            }
        }
        private void SendNotification(string title, string message, HttpRequestBuilder requestBuilder, PushBulletSettings settings)
        {
            try
            {
                requestBuilder.AddFormParameter("type", "note")
                .AddFormParameter("title", title)
                .AddFormParameter("body", message);

                if (settings.SenderId.IsNotNullOrWhiteSpace())
                {
                    requestBuilder.AddFormParameter("source_device_iden", settings.SenderId);
                }

                var request = requestBuilder.Build();

                request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);

                _httpClient.Execute(request);
            }
            catch (HttpException ex)
            {
                if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    _logger.Error(ex, "Access token is invalid");
                    throw;
                }

                throw new PushBulletException("Unable to send text message: {0}", ex, ex.Message);
            }
        }