Example #1
0
        public List <NzbVortexGroup> GetGroups(NzbVortexSettings settings)
        {
            var request  = BuildRequest("group", Method.GET, true, settings);
            var response = ProcessRequest <NzbVortexGroupResponse>(request, settings);

            return(response.Groups);
        }
Example #2
0
        public List <NzbVortexGroup> GetGroups(NzbVortexSettings settings)
        {
            var request  = BuildRequest(settings).Resource("group");
            var response = ProcessRequest <NzbVortexGroupResponse>(request, true, settings);

            return(response.Groups);
        }
Example #3
0
        public NzbVortexApiVersionResponse GetApiVersion(NzbVortexSettings settings)
        {
            var request  = BuildRequest("app/apilevel", Method.GET, false, settings);
            var response = ProcessRequest <NzbVortexApiVersionResponse>(request, settings);

            return(response);
        }
Example #4
0
        public NzbVortexFiles GetFiles(int id, NzbVortexSettings settings)
        {
            var request  = BuildRequest(string.Format("file/{0}", id), Method.GET, true, settings);
            var response = ProcessRequest <NzbVortexFiles>(request, settings);

            return(response);
        }
Example #5
0
        public List <NzbVortexFile> GetFiles(int id, NzbVortexSettings settings)
        {
            var requestBuilder = BuildRequest(settings).Resource(string.Format("file/{0}", id));

            var response = ProcessRequest <NzbVortexFilesResponse>(requestBuilder, true, settings);

            return(response.Files);
        }
Example #6
0
        private HttpRequestBuilder BuildRequest(NzbVortexSettings settings)
        {
            var requestBuilder = new HttpRequestBuilder(true, settings.Host, settings.Port, "api");

            requestBuilder.LogResponseContent = true;

            return(requestBuilder);
        }
Example #7
0
        public NzbVortexApiVersionResponse GetApiVersion(NzbVortexSettings settings)
        {
            var requestBuilder = BuildRequest(settings).Resource("app/apilevel");

            var response = ProcessRequest <NzbVortexApiVersionResponse>(requestBuilder, false, settings);

            return(response);
        }
Example #8
0
        public void Remove(int id, bool deleteData, NzbVortexSettings settings)
        {
            var request = BuildRequest(string.Format("nzb/{0}/cancel", id), Method.GET, true, settings);

            if (deleteData)
            {
                request.Resource += "Delete";
            }

            ProcessRequest(request, settings);
        }
Example #9
0
        private HttpRequestBuilder BuildRequest(NzbVortexSettings settings)
        {
            var baseUrl = HttpRequestBuilder.BuildBaseUrl(true, settings.Host, settings.Port, settings.UrlBase);

            baseUrl = HttpUri.CombinePath(baseUrl, "api");
            var requestBuilder = new HttpRequestBuilder(baseUrl);

            requestBuilder.LogResponseContent = true;

            return(requestBuilder);
        }
Example #10
0
        private string GetSessionId(bool force, NzbVortexSettings settings)
        {
            var authCacheKey = string.Format("{0}_{1}_{2}", settings.Host, settings.Port, settings.ApiKey);

            if (force)
            {
                _authCache.Remove(authCacheKey);
            }

            var sessionId = _authCache.Get(authCacheKey, () => Authenticate(settings));

            return(sessionId);
        }
Example #11
0
        public NzbVortexQueue GetQueue(int doneLimit, NzbVortexSettings settings)
        {
            var request = BuildRequest("nzb", Method.GET, true, settings);

            if (settings.TvCategory.IsNotNullOrWhiteSpace())
            {
                request.AddQueryParameter("groupName", settings.TvCategory);
            }

            request.AddQueryParameter("limitDone", doneLimit.ToString());

            var response = ProcessRequest <NzbVortexQueue>(request, settings);

            return(response);
        }
Example #12
0
        public List <NzbVortexQueueItem> GetQueue(int doneLimit, NzbVortexSettings settings)
        {
            var requestBuilder = BuildRequest(settings).Resource("nzb");

            if (settings.Category.IsNotNullOrWhiteSpace())
            {
                requestBuilder.AddQueryParam("groupName", settings.Category);
            }

            requestBuilder.AddQueryParam("limitDone", doneLimit.ToString());

            var response = ProcessRequest <NzbVortexQueueResponse>(requestBuilder, true, settings);

            return(response.Items);
        }
Example #13
0
        private string ProcessRequest(IRestRequest request, NzbVortexSettings settings)
        {
            var client = BuildClient(settings);

            try
            {
                return(ProcessRequest(client, request).Content);
            }
            catch (NzbVortexNotLoggedInException ex)
            {
                _logger.Warn("Not logged in response received, reauthenticating and retrying");
                request.AddQueryParameter("sessionid", GetSessionId(true, settings));

                return(ProcessRequest(client, request).Content);
            }
        }
Example #14
0
        public string DownloadNzb(byte[] nzbData, string filename, int priority, NzbVortexSettings settings)
        {
            var request = BuildRequest("/nzb/add", Method.POST, true, settings);

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

            if (settings.TvCategory.IsNotNullOrWhiteSpace())
            {
                request.AddQueryParameter("groupname", settings.TvCategory);
            }

            var response = ProcessRequest <NzbVortexAddResponse>(request, settings);

            return(response.Id);
        }
Example #15
0
        public string DownloadNzb(byte[] nzbData, string filename, int priority, NzbVortexSettings settings)
        {
            var requestBuilder = BuildRequest(settings).Resource("nzb/add")
                                 .Post()
                                 .AddQueryParam("priority", priority.ToString());

            if (settings.MusicCategory.IsNotNullOrWhiteSpace())
            {
                requestBuilder.AddQueryParam("groupname", settings.MusicCategory);
            }

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

            var response = ProcessRequest <NzbVortexAddResponse>(requestBuilder, true, settings);

            return(response.Id);
        }
Example #16
0
        private void AuthenticateClient(HttpRequestBuilder requestBuilder, NzbVortexSettings settings, bool reauthenticate = false)
        {
            var authKey = string.Format("{0}:{1}", requestBuilder.BaseUrl, settings.ApiKey);

            var sessionId = _authSessionIdCache.Find(authKey);

            if (sessionId == null || reauthenticate)
            {
                _authSessionIdCache.Remove(authKey);

                var nonceRequest  = BuildRequest(settings).Resource("auth/nonce").Build();
                var nonceResponse = _httpClient.Execute(nonceRequest);

                var nonce = Json.Deserialize <NzbVortexAuthNonceResponse>(nonceResponse.Content).AuthNonce;

                var cnonce = Guid.NewGuid().ToString();

                var hashString = string.Format("{0}:{1}:{2}", nonce, cnonce, settings.ApiKey);
                var hash       = Convert.ToBase64String(hashString.SHA256Hash().HexToByteArray());

                var authRequest = BuildRequest(settings).Resource("auth/login")
                                  .AddQueryParam("nonce", nonce)
                                  .AddQueryParam("cnonce", cnonce)
                                  .AddQueryParam("hash", hash)
                                  .Build();
                var authResponse = _httpClient.Execute(authRequest);
                var authResult   = Json.Deserialize <NzbVortexAuthResponse>(authResponse.Content);

                if (authResult.LoginResult == NzbVortexLoginResultType.Failed)
                {
                    throw new NzbVortexAuthenticationException("Authentication failed, check your API Key");
                }

                sessionId = authResult.SessionId;

                _authSessionIdCache.Set(authKey, sessionId);
            }

            requestBuilder.AddQueryParam("sessionid", sessionId);
        }
Example #17
0
        private string Authenticate(NzbVortexSettings settings)
        {
            var nonce      = GetNonce(settings);
            var cnonce     = Guid.NewGuid().ToString();
            var hashString = string.Format("{0}:{1}:{2}", nonce, cnonce, settings.ApiKey);
            var sha256     = hashString.SHA256Hash();
            var base64     = Convert.ToBase64String(sha256.HexToByteArray());
            var request    = BuildRequest("auth/login", Method.GET, false, settings);

            request.AddQueryParameter("nonce", nonce);
            request.AddQueryParameter("cnonce", cnonce);
            request.AddQueryParameter("hash", base64);

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

            if (result.LoginResult == NzbVortexLoginResultType.Failed)
            {
                throw new NzbVortexAuthenticationException("Authentication failed, check your API Key");
            }

            return(result.SessionId);
        }
Example #18
0
        private IRestClient BuildClient(NzbVortexSettings settings)
        {
            var url = string.Format(@"https://{0}:{1}/api", settings.Host, settings.Port);

            return(RestClientFactory.BuildClient(url));
        }
Example #19
0
        public void Remove(int id, bool deleteData, NzbVortexSettings settings)
        {
            var requestBuilder = BuildRequest(settings).Resource(string.Format("nzb/{0}/{1}", id, deleteData ? "cancelDelete" : "cancel"));

            ProcessRequest <NzbVortexResponseBase>(requestBuilder, true, settings);
        }
Example #20
0
        private T ProcessRequest <T>(HttpRequestBuilder requestBuilder, bool requiresAuthentication, NzbVortexSettings settings)
            where T : NzbVortexResponseBase, new()
        {
            if (requiresAuthentication)
            {
                AuthenticateClient(requestBuilder, settings);
            }

            HttpResponse response = null;

            try
            {
                response = _httpClient.Execute(requestBuilder.Build());

                var result = Json.Deserialize <T>(response.Content);

                if (result.Result == NzbVortexResultType.NotLoggedIn)
                {
                    _logger.Debug("Not logged in response received, reauthenticating and retrying");
                    AuthenticateClient(requestBuilder, settings, true);

                    response = _httpClient.Execute(requestBuilder.Build());

                    result = Json.Deserialize <T>(response.Content);

                    if (result.Result == NzbVortexResultType.NotLoggedIn)
                    {
                        throw new DownloadClientException("Unable to connect to remain authenticated to NzbVortex");
                    }
                }

                return(result);
            }
            catch (JsonException ex)
            {
                throw new DownloadClientException("NzbVortex response could not be processed {0}: {1}", ex.Message, response.Content);
            }
            catch (HttpException ex)
            {
                throw new DownloadClientException("Unable to connect to NZBVortex, please check your settings", ex);
            }
            catch (WebException ex)
            {
                throw new DownloadClientUnavailableException("Unable to connect to NZBVortex, please check your settings", ex);
            }
        }
Example #21
0
        private IRestRequest BuildRequest(string resource, Method method, bool requiresAuthentication, NzbVortexSettings settings)
        {
            var request = new RestRequest(resource, method);

            if (requiresAuthentication)
            {
                request.AddQueryParameter("sessionid", GetSessionId(false, settings));
            }

            return(request);
        }
Example #22
0
 private T ProcessRequest <T>(IRestRequest request, NzbVortexSettings settings) where T : new()
 {
     return(Json.Deserialize <T>(ProcessRequest(request, settings)));
 }
Example #23
0
        private string GetNonce(NzbVortexSettings settings)
        {
            var request = BuildRequest("auth/nonce", Method.GET, false, settings);

            return(ProcessRequest <NzbVortexAuthNonceResponse>(request, settings).AuthNonce);
        }