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}/api{version}"); }
public static async Task <IList <JSONProfile> > GetProfiles(HttpClient httpClient, ILogger <Sonarr> logger, SonarrSettings settings) { var retryCount = 0; while (retryCount <= 5) { try { var response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}/qualityprofile"); var jsonResponse = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <IList <JSONProfile> >(jsonResponse)); } catch (System.Exception ex) { logger.LogWarning("An error while getting Sonarr profiles: " + ex.Message); retryCount++; await Task.Delay(1000); } } throw new System.Exception("An error occurred while getting Sonarr profiles"); }
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)); }
public static async Task TestConnectionAsync(HttpClient httpClient, ILogger <Sonarr> logger, SonarrSettings settings) { var testSuccessful = false; try { var response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}/system/status"); testSuccessful = response.IsSuccessStatusCode; } catch (System.Exception ex) { logger.LogWarning("Error while testing Sonarr connection: " + ex.Message); throw new Exception("Could not connect to Sonarr"); } if (!testSuccessful) { throw new Exception("Could not connect to Sonarr"); } }