示例#1
0
        private static string GetBaseURL(SonarrSettings 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}");
        }
示例#2
0
        public static async Task <IList <JSONRootPath> > GetRootPaths(HttpClient httpClient, ILogger <SonarrClient> logger, SonarrSettings settings)
        {
            var retryCount = 0;

            while (retryCount <= 5)
            {
                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 Sonarr root paths: " + ex.Message);
                    retryCount++;
                    await Task.Delay(1000);
                }
            }

            throw new System.Exception("An error occurred while getting Sonarr root paths");
        }
示例#3
0
        private static async Task <HttpResponseMessage> HttpGetAsync(HttpClient client, SonarrSettings 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));
        }
示例#4
0
        private static async Task <HttpResponseMessage> HttpGetAsync(HttpClient client, SonarrSettings 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));
            }
        }
示例#5
0
        public static async Task TestConnectionAsync(HttpClient httpClient, ILogger <SonarrClient> logger, SonarrSettings 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 Sonarr");
                    }
                }
                catch
                {
                    throw new Exception("Base url does not match what is set in Sonarr");
                }

                testSuccessful = true;
            }
            catch (HttpRequestException ex)
            {
                logger.LogWarning(ex, "Error while testing Sonarr connection: " + ex.Message);
                throw new Exception("Invalid host and/or port");
            }
            catch (System.Exception ex)
            {
                logger.LogWarning(ex, "Error while testing Sonarr 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");
            }
        }
示例#6
0
        public static async Task <IList <JSONTag> > GetTags(HttpClient httpClient, ILogger <SonarrClient> logger, SonarrSettings settings)
        {
            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 Sonarr tags: " + ex.Message);
            }

            throw new System.Exception("An error occurred while getting Sonarr tags");
        }
示例#7
0
        private static string GetBaseURL(SonarrSettings settings)
        {
            var protocol = settings.UseSSL ? "https" : "http";

            return($"{protocol}://{settings.Hostname}:{settings.Port}{settings.BaseUrl}/api");
        }
示例#8
0
 public static Task <IList <JSONTag> > GetTags(HttpClient httpClient, ILogger <SonarrClient> logger, SonarrSettings settings)
 {
     if (settings.Version == "2")
     {
         return(Task.FromResult((IList <JSONTag>) new List <JSONTag>()));
     }
     else
     {
         return(SonarrClientV3.GetTags(httpClient, logger, settings));
     }
 }
示例#9
0
 public static Task <IList <JSONProfile> > GetProfiles(HttpClient httpClient, ILogger <SonarrClient> logger, SonarrSettings settings)
 {
     if (settings.Version == "2")
     {
         return(SonarrClientV2.GetProfiles(httpClient, logger, settings));
     }
     else
     {
         return(SonarrClientV3.GetProfiles(httpClient, logger, settings));
     }
 }
示例#10
0
 public static Task TestConnectionAsync(HttpClient httpClient, ILogger <SonarrClient> logger, SonarrSettings settings)
 {
     if (settings.Version == "2")
     {
         return(SonarrClientV2.TestConnectionAsync(httpClient, logger, settings));
     }
     else
     {
         return(SonarrClientV3.TestConnectionAsync(httpClient, logger, settings));
     }
 }