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}"); }
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"); }
private static string GetBaseURL(RadarrSettings settings) { var protocol = settings.UseSSL ? "https" : "http"; return($"{protocol}://{settings.Hostname}:{settings.Port}{settings.BaseUrl}/api"); }
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)); } }
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"); } }
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)); }
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"); }
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)); } }
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)); } }
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)); } }