Ejemplo n.º 1
0
        private string ProcessRequest(HttpRequestBuilder requestBuilder, SabnzbdSettings settings)
        {
            var httpRequest = requestBuilder.Build();

            HttpResponse response;

            _logger.Debug("Url: {0}", httpRequest.Url);

            try
            {
                response = _httpClient.Execute(httpRequest);
            }
            catch (HttpException ex)
            {
                throw new DownloadClientException("Unable to connect to SABnzbd, please check your settings", ex);
            }
            catch (WebException ex)
            {
                throw new DownloadClientException("Unable to connect to SABnzbd, please check your settings", ex);
            }

            CheckForError(response);

            return(response.Content);
        }
Ejemplo n.º 2
0
        public void RetryDownload(string id, SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = String.Format("mode=retry&value={0}", id);

            ProcessRequest(request, action, settings);
        }
Ejemplo n.º 3
0
        private string ProcessRequest(HttpRequestBuilder requestBuilder, SabnzbdSettings settings)
        {
            var httpRequest = requestBuilder.Build();

            HttpResponse response;

            _logger.Debug("Url: {0}", httpRequest.Url);

            try
            {
                response = _httpClient.Execute(httpRequest);
            }
            catch (HttpException ex)
            {
                throw new DownloadClientException("Unable to connect to SABnzbd, {0}", ex, ex.Message);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.TrustFailure)
                {
                    throw new DownloadClientUnavailableException("Unable to connect to SABnzbd, certificate validation failed.", ex);
                }

                throw new DownloadClientUnavailableException("Unable to connect to SABnzbd, {0}", ex, ex.Message);
            }

            CheckForError(response);

            return(response.Content);
        }
Ejemplo n.º 4
0
        private HttpRequestBuilder BuildRequest(string mode, SabnzbdSettings settings)
        {
            var baseUrl = string.Format(@"{0}://{1}:{2}/api",
                                        settings.UseSsl ? "https" : "http",
                                        settings.Host,
                                        settings.Port);

            var requestBuilder = new HttpRequestBuilder(baseUrl)
                                 .Accept(HttpAccept.Json)
                                 .AddQueryParam("mode", mode);

            requestBuilder.LogResponseContent = true;

            if (settings.ApiKey.IsNotNullOrWhiteSpace())
            {
                requestBuilder.AddSuffixQueryParam("apikey", settings.ApiKey);
            }
            else
            {
                requestBuilder.AddSuffixQueryParam("ma_username", settings.Username);
                requestBuilder.AddSuffixQueryParam("ma_password", settings.Password);
            }
            requestBuilder.AddSuffixQueryParam("output", "json");

            return(requestBuilder);
        }
Ejemplo n.º 5
0
        public void RemoveFrom(string source, string id, SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = String.Format("mode={0}&name=delete&del_files=1&value={1}", source, id);

            ProcessRequest(request, action, settings);
        }
Ejemplo n.º 6
0
        public SabnzbdConfig GetConfig(SabnzbdSettings settings)
        {
            var request = BuildRequest("get_config", settings);

            var response = Json.Deserialize <SabnzbdConfigResponse>(ProcessRequest(request, settings));

            return(response.Config);
        }
Ejemplo n.º 7
0
        public string GetBaseUrl(SabnzbdSettings settings, string relativePath = null)
        {
            var baseUrl = HttpRequestBuilder.BuildBaseUrl(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase);

            baseUrl = HttpUri.CombinePath(baseUrl, relativePath);

            return(baseUrl);
        }
Ejemplo n.º 8
0
        public SabnzbdConfig GetConfig(SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = "mode=get_config";

            var response = Json.Deserialize <SabnzbdConfigResponse>(ProcessRequest(request, action, settings));

            return(response.Config);
        }
Ejemplo n.º 9
0
        public SabnzbdCategoryResponse GetCategories(SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = "mode=get_cats";

            var response = Json.Deserialize <SabnzbdCategoryResponse>(ProcessRequest(request, action, settings));

            return(response);
        }
Ejemplo n.º 10
0
        public SabnzbdQueue GetQueue(int start, int limit, SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = String.Format("mode=queue&start={0}&limit={1}", start, limit);

            var response = ProcessRequest(request, action, settings);

            return(Json.Deserialize <SabnzbdQueue>(JObject.Parse(response).SelectToken("queue").ToString()));
        }
Ejemplo n.º 11
0
        public void RemoveFrom(string source, string id, bool deleteData, SabnzbdSettings settings)
        {
            var request = BuildRequest(source, settings);

            request.AddQueryParam("name", "delete");
            request.AddQueryParam("del_files", deleteData ? 1 : 0);
            request.AddQueryParam("value", id);

            ProcessRequest(request, settings);
        }
Ejemplo n.º 12
0
        public SabnzbdFullStatus GetFullStatus(SabnzbdSettings settings)
        {
            var request = BuildRequest("fullstatus", settings);

            request.AddQueryParam("skip_dashboard", "1");

            var response = Json.Deserialize <SabnzbdFullStatusResponse>(ProcessRequest(request, settings));

            return(response.Status);
        }
Ejemplo n.º 13
0
        public SabnzbdQueue GetQueue(int start, int limit, SabnzbdSettings settings)
        {
            var request = BuildRequest("queue", settings);

            request.AddQueryParam("start", start);
            request.AddQueryParam("limit", limit);

            var response = ProcessRequest(request, settings);

            return(Json.Deserialize <SabnzbdQueue>(JObject.Parse(response).SelectToken("queue").ToString()));
        }
Ejemplo n.º 14
0
        public string ProcessRequest(IRestRequest restRequest, string action, SabnzbdSettings settings)
        {
            var client   = BuildClient(action, settings);
            var response = client.Execute(restRequest);

            _logger.Trace("Response: {0}", response.Content);

            CheckForError(response);

            return(response.Content);
        }
Ejemplo n.º 15
0
        public string GetVersion(SabnzbdSettings settings)
        {
            var request = BuildRequest("version", settings);

            SabnzbdVersionResponse response;

            if (!Json.TryDeserialize <SabnzbdVersionResponse>(ProcessRequest(request, settings), out response))
            {
                response = new SabnzbdVersionResponse();
            }

            return(response.Version);
        }
Ejemplo n.º 16
0
        public SabnzbdHistory GetHistory(int start, int limit, string category, SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = string.Format("mode=history&start={0}&limit={1}", start, limit);

            if (category.IsNotNullOrWhiteSpace())
            {
                action += "&category=" + category;
            }

            var response = ProcessRequest(request, action, settings);

            return(Json.Deserialize <SabnzbdHistory>(JObject.Parse(response).SelectToken("history").ToString()));
        }
Ejemplo n.º 17
0
        public SabnzbdVersionResponse GetVersion(SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = "mode=version";

            SabnzbdVersionResponse response;

            if (!Json.TryDeserialize <SabnzbdVersionResponse>(ProcessRequest(request, action, settings), out response))
            {
                response = new SabnzbdVersionResponse();
            }

            return(response);
        }
Ejemplo n.º 18
0
        public string RetryDownload(string id, SabnzbdSettings settings)
        {
            var request = new RestRequest();
            var action  = String.Format("mode=retry&value={0}", id);

            SabnzbdRetryResponse response;

            if (!Json.TryDeserialize <SabnzbdRetryResponse>(ProcessRequest(request, action, settings), out response))
            {
                response        = new SabnzbdRetryResponse();
                response.Status = true;
            }

            return(response.Id);
        }
Ejemplo n.º 19
0
        public SabnzbdHistory GetHistory(int start, int limit, string category, SabnzbdSettings settings)
        {
            var request = BuildRequest("history", settings);

            request.AddQueryParam("start", start);
            request.AddQueryParam("limit", limit);

            if (category.IsNotNullOrWhiteSpace())
            {
                request.AddQueryParam("category", category);
            }

            var response = ProcessRequest(request, settings);

            return(Json.Deserialize <SabnzbdHistory>(JObject.Parse(response).SelectToken("history").ToString()));
        }
Ejemplo n.º 20
0
        public string RetryDownload(string id, SabnzbdSettings settings)
        {
            var request = BuildRequest("retry", settings);

            request.AddQueryParam("value", id);

            SabnzbdRetryResponse response;

            if (!Json.TryDeserialize <SabnzbdRetryResponse>(ProcessRequest(request, settings), out response))
            {
                response        = new SabnzbdRetryResponse();
                response.Status = true;
            }

            return(response.Id);
        }
Ejemplo n.º 21
0
        private IRestClient BuildClient(string action, SabnzbdSettings settings)
        {
            var protocol = settings.UseSsl ? "https" : "http";

            var authentication = settings.ApiKey.IsNullOrWhiteSpace() ?
                                 String.Format("ma_username={0}&ma_password={1}", settings.Username, Uri.EscapeDataString(settings.Password)) :
                                 String.Format("apikey={0}", settings.ApiKey);

            var url = String.Format(@"{0}://{1}:{2}/api?{3}&{4}&output=json",
                                    protocol,
                                    settings.Host,
                                    settings.Port,
                                    action,
                                    authentication);

            _logger.Debug("Url: " + url);

            return(RestClientFactory.BuildClient(url));
        }
Ejemplo n.º 22
0
        public SabnzbdAddResponse DownloadNzbByUrl(string url, string category, int priority, SabnzbdSettings settings)
        {
            var request = BuildRequest("addurl", settings).Post();

            request.AddQueryParam("name", url);
            request.AddQueryParam("cat", category);
            request.AddQueryParam("priority", priority);

            SabnzbdAddResponse response;

            if (!Json.TryDeserialize <SabnzbdAddResponse>(ProcessRequest(request, settings), out response))
            {
                response        = new SabnzbdAddResponse();
                response.Status = true;
            }

            return(response);
        }
Ejemplo n.º 23
0
        public SabnzbdAddResponse DownloadNzb(Byte[] nzbData, string filename, string category, int priority, SabnzbdSettings settings)
        {
            var request = new RestRequest(Method.POST);
            var action  = String.Format("mode=addfile&cat={0}&priority={1}", Uri.EscapeDataString(category), priority);

            request.AddFile("name", nzbData, filename, "application/x-nzb");

            SabnzbdAddResponse response;

            if (!Json.TryDeserialize <SabnzbdAddResponse>(ProcessRequest(request, action, settings), out response))
            {
                response        = new SabnzbdAddResponse();
                response.Status = true;
            }

            return(response);
        }
Ejemplo n.º 24
0
        public SabnzbdAddResponse DownloadNzb(byte[] nzbData, string filename, string category, int priority, SabnzbdSettings settings)
        {
            var request = BuildRequest("addfile", settings).Post();

            request.AddQueryParam("cat", category);
            request.AddQueryParam("priority", priority);

            request.AddFormUpload("name", filename, nzbData, "application/x-nzb");

            SabnzbdAddResponse response;

            if (!Json.TryDeserialize <SabnzbdAddResponse>(ProcessRequest(request, settings), out response))
            {
                response        = new SabnzbdAddResponse();
                response.Status = true;
            }

            return(response);
        }