예제 #1
0
        public void AddTorrentByUrl(string url, IEnumerable <string> tags, FloodSettings settings)
        {
            var addRequest = BuildRequest(settings).Resource("/torrents/add-urls").Post().Build();

            var body = new Dictionary <string, object>
            {
                { "urls", new List <string> {
                      url
                  } },
                { "tags", tags.ToList() }
            };

            if (settings.Destination != null)
            {
                body.Add("destination", settings.Destination);
            }

            if (settings.StartOnAdd)
            {
                body.Add("start", true);
            }

            addRequest.SetContent(body.ToJson());

            HandleRequest(addRequest, settings);
        }
예제 #2
0
        public List <string> GetTorrentContentPaths(string hash, FloodSettings settings)
        {
            var contentsRequest = BuildRequest(settings).Resource($"/torrents/{hash}/contents").Build();

            contentsRequest.Method = HttpMethod.GET;

            return(Json.Deserialize <List <TorrentContent> >(HandleRequest(contentsRequest, settings).Content).ConvertAll(content => content.Path));
        }
예제 #3
0
        public Dictionary <string, Torrent> GetTorrents(FloodSettings settings)
        {
            var getTorrentsRequest = BuildRequest(settings).Resource("/torrents").Build();

            getTorrentsRequest.Method = HttpMethod.GET;

            return(Json.Deserialize <TorrentListSummary>(HandleRequest(getTorrentsRequest, settings).Content).Torrents);
        }
예제 #4
0
        public void AuthVerify(FloodSettings settings)
        {
            var verifyRequest = BuildRequest(settings).Resource("/auth/verify").Build();

            verifyRequest.Method = HttpMethod.GET;

            HandleRequest(verifyRequest, settings);
        }
예제 #5
0
        public FloodClientSettings GetClientSettings(FloodSettings settings)
        {
            var contentsRequest = BuildRequest(settings).Resource($"/client/settings").Build();

            contentsRequest.Method = HttpMethod.GET;

            return(Json.Deserialize <FloodClientSettings>(HandleRequest(contentsRequest, settings).Content));
        }
예제 #6
0
        private HttpRequestBuilder BuildRequest(FloodSettings settings)
        {
            var requestBuilder = new HttpRequestBuilder(HttpUri.CombinePath(BuildUrl(settings), "/api"))
            {
                LogResponseContent = true,
                NetworkCredential  = new NetworkCredential(settings.Username, settings.Password)
            };

            requestBuilder.Headers.ContentType = "application/json";
            requestBuilder.SetCookies(AuthAuthenticate(requestBuilder, settings));

            return(requestBuilder);
        }
예제 #7
0
        public void DeleteTorrent(string hash, bool deleteData, FloodSettings settings)
        {
            var deleteRequest = BuildRequest(settings).Resource("/torrents/delete").Post().Build();

            var body = new Dictionary <string, object>
            {
                { "hashes", new List <string> {
                      hash
                  } },
                { "deleteData", deleteData }
            };

            deleteRequest.SetContent(body.ToJson());

            HandleRequest(deleteRequest, settings);
        }
예제 #8
0
        public void SetTorrentsTags(string hash, IEnumerable <string> tags, FloodSettings settings)
        {
            var tagsRequest = BuildRequest(settings).Resource("/torrents/tags").Build();

            tagsRequest.Method = HttpMethod.PATCH;

            var body = new Dictionary <string, object>
            {
                { "hashes", new List <string> {
                      hash
                  } },
                { "tags", tags.ToList() }
            };

            tagsRequest.SetContent(body.ToJson());

            HandleRequest(tagsRequest, settings);
        }
예제 #9
0
        private HttpResponse HandleRequest(HttpRequest request, FloodSettings settings)
        {
            try
            {
                return(_httpClient.Execute(request));
            }
            catch (HttpException ex)
            {
                if (ex.Response.StatusCode == HttpStatusCode.Forbidden ||
                    ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    _authCookieCache.Remove(BuildCachedCookieKey(settings));
                    throw new DownloadClientAuthenticationException("Failed to authenticate with Flood.");
                }

                throw new DownloadClientException("Unable to connect to Flood, please check your settings");
            }
            catch
            {
                throw new DownloadClientException("Unable to connect to Flood, please check your settings");
            }
        }
예제 #10
0
        private Dictionary <string, string> AuthAuthenticate(HttpRequestBuilder requestBuilder, FloodSettings settings, bool force = false)
        {
            var cachedCookies = _authCookieCache.Find(BuildCachedCookieKey(settings));

            if (cachedCookies == null || force)
            {
                var authenticateRequest = requestBuilder.Resource("/auth/authenticate").Post().Build();

                var body = new Dictionary <string, object>
                {
                    { "username", settings.Username },
                    { "password", settings.Password }
                };
                authenticateRequest.SetContent(body.ToJson());

                var response = HandleRequest(authenticateRequest, settings);
                cachedCookies = response.GetCookies();
                _authCookieCache.Set(BuildCachedCookieKey(settings), cachedCookies);
            }

            return(cachedCookies);
        }
예제 #11
0
 private string BuildCachedCookieKey(FloodSettings settings)
 {
     return($"{BuildUrl(settings)}:{settings.Username}");
 }
예제 #12
0
 private string BuildUrl(FloodSettings settings)
 {
     return($"{(settings.UseSsl ? "https://" : "http://")}{settings.Host}:{settings.Port}/{settings.UrlBase}");
 }