Exemplo n.º 1
0
        private static string GetBaseURL(RadarrSettings settings)
        {
            var version  = settings.Version == "2" ? string.Empty : $"/v{settings.Version}";
            var protocol = settings.UseSSL ? "https" : "http";

            return($"{protocol}://{settings.Hostname}:{settings.Port}{settings.BaseUrl}/api{version}");
        }
Exemplo n.º 2
0
        public static async Task <IList <JSONRootPath> > GetRootPaths(HttpClient httpClient, ILogger <RadarrClient> logger, RadarrSettings settings)
        {
            try
            {
                var response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}/rootfolder");

                var jsonResponse = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <IList <JSONRootPath> >(jsonResponse));
            }
            catch (System.Exception ex)
            {
                logger.LogWarning(ex, "An error while getting Radarr root paths: " + ex.Message);
            }

            throw new System.Exception("An error occurred while getting Radarr root paths");
        }
Exemplo n.º 3
0
        private static string GetBaseURL(RadarrSettings settings)
        {
            var protocol = settings.UseSSL ? "https" : "http";

            return($"{protocol}://{settings.Hostname}:{settings.Port}{settings.BaseUrl}/api");
        }
Exemplo n.º 4
0
        private static async Task <HttpResponseMessage> HttpGetAsync(HttpClient client, RadarrSettings settings, string url)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, url);

            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("X-Api-Key", settings.ApiKey);

            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5)))
            {
                return(await client.SendAsync(request, cts.Token));
            }
        }
Exemplo n.º 5
0
        public static async Task TestConnectionAsync(HttpClient httpClient, ILogger <RadarrClient> logger, RadarrSettings settings)
        {
            if (!string.IsNullOrWhiteSpace(settings.BaseUrl) && !settings.BaseUrl.StartsWith("/"))
            {
                throw new Exception("Invalid base URL, must start with /");
            }

            var testSuccessful = false;

            try
            {
                var response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}/config/host");

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new Exception("Invalid api key");
                }
                else if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new Exception("Incorrect api version");
                }

                try
                {
                    var responseString = await response.Content.ReadAsStringAsync();

                    dynamic jsonResponse = JObject.Parse(responseString);

                    if (!jsonResponse.urlBase.ToString().Equals(settings.BaseUrl, StringComparison.InvariantCultureIgnoreCase))
                    {
                        throw new Exception("Base url does not match what is set in Radarr");
                    }
                }
                catch
                {
                    throw new Exception("Base url does not match what is set in Radarr");
                }

                testSuccessful = true;
            }
            catch (HttpRequestException ex)
            {
                logger.LogWarning(ex, "Error while testing Radarr connection: " + ex.Message);
                throw new Exception("Invalid host and/or port");
            }
            catch (System.Exception ex)
            {
                logger.LogWarning(ex, "Error while testing Radarr connection: " + ex.Message);

                if (ex.GetType() == typeof(System.Exception))
                {
                    throw;
                }
                else
                {
                    throw new Exception("Invalid host and/or port");
                }
            }

            if (!testSuccessful)
            {
                throw new Exception("Invalid host and/or port");
            }
        }
Exemplo n.º 6
0
        private static async Task <HttpResponseMessage> HttpGetAsync(HttpClient client, RadarrSettings settings, string url)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, url);

            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("X-Api-Key", settings.ApiKey);

            return(await client.SendAsync(request));
        }
Exemplo n.º 7
0
        public static async Task <IList <JSONTag> > GetTags(HttpClient httpClient, ILogger <RadarrClient> logger, RadarrSettings settings)
        {
            var retryCount = 0;

            while (retryCount <= 5)
            {
                try
                {
                    var response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}/tag");

                    var jsonResponse = await response.Content.ReadAsStringAsync();

                    return(JsonConvert.DeserializeObject <IList <JSONTag> >(jsonResponse));
                }
                catch (System.Exception ex)
                {
                    logger.LogWarning(ex, "An error while getting Radarr tags: " + ex.Message);
                    retryCount++;
                    await Task.Delay(1000);
                }
            }

            throw new System.Exception("An error occurred while getting Radarr tags");
        }
Exemplo n.º 8
0
 public static Task <IList <JSONTag> > GetTags(HttpClient httpClient, ILogger <RadarrClient> logger, RadarrSettings settings)
 {
     if (settings.Version == "2")
     {
         return(Task.FromResult((IList <JSONTag>) new List <JSONTag>()));
     }
     else
     {
         return(RadarrClientV3.GetTags(httpClient, logger, settings));
     }
 }
Exemplo n.º 9
0
 public static Task <IList <JSONProfile> > GetProfiles(HttpClient httpClient, ILogger <RadarrClient> logger, RadarrSettings settings)
 {
     if (settings.Version == "2")
     {
         return(RadarrClientV2.GetProfiles(httpClient, logger, settings));
     }
     else
     {
         return(RadarrClientV3.GetProfiles(httpClient, logger, settings));
     }
 }
Exemplo n.º 10
0
 public static Task TestConnectionAsync(HttpClient httpClient, ILogger <RadarrClient> logger, RadarrSettings settings)
 {
     if (settings.Version == "2")
     {
         return(RadarrClientV2.TestConnectionAsync(httpClient, logger, settings));
     }
     else
     {
         return(RadarrClientV3.TestConnectionAsync(httpClient, logger, settings));
     }
 }