private static string GetBaseURL(OverseerrSettings settings) { var protocol = settings.UseSSL ? "https" : "http"; return($"{protocol}://{settings.Hostname}:{settings.Port}/api/v{settings.Version}/"); }
public static async Task TestConnectionAsync(HttpClient httpClient, ILogger <OverseerrClient> logger, OverseerrSettings settings) { var testSuccessful = false; try { var response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}settings/main"); if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden) { throw new Exception("Invalid api key"); } else if (response.StatusCode == HttpStatusCode.NotFound) { throw new Exception("Invalid host and/or port"); } try { var responseString = await response.Content.ReadAsStringAsync(); dynamic jsonResponse = JObject.Parse(responseString); if (!jsonResponse.apiKey.ToString().Equals(settings.ApiKey, StringComparison.InvariantCultureIgnoreCase)) { throw new Exception("Invalid host and/or port"); } } catch { throw new Exception("Invalid host and/or port"); } testSuccessful = true; } catch (HttpRequestException ex) { logger.LogWarning(ex, "Error while testing Overseeerr connection: " + ex.Message); throw new Exception("Invalid host and/or port"); } catch (System.Exception ex) { logger.LogWarning(ex, "Error while testing Overseeerr 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, OverseerrSettings 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 <OverseerrClient> logger, OverseerrSettings settings) { var testSuccessful = false; try { var response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}settings/main"); if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden) { throw new Exception("Invalid api key"); } else if (response.StatusCode == HttpStatusCode.NotFound) { throw new Exception("Invalid host and/or port"); } try { var responseString = await response.Content.ReadAsStringAsync(); dynamic jsonResponse = JObject.Parse(responseString); if (!jsonResponse.apiKey.ToString().Equals(settings.ApiKey, StringComparison.InvariantCultureIgnoreCase)) { throw new Exception("Invalid host and/or port"); } } catch { throw new Exception("Invalid host and/or port"); } if (!string.IsNullOrWhiteSpace(settings.DefaultApiUserID)) { if (!int.TryParse(settings.DefaultApiUserID, out var userId)) { throw new Exception("Overseerr default user ID must be a number."); } response = await HttpGetAsync(httpClient, settings, $"{GetBaseURL(settings)}user/{userId}"); try { await response.ThrowIfNotSuccessfulAsync("OverseerrFindSpecificUser failed", x => x.error); } catch (System.Exception ex) { logger.LogWarning(ex, $"Default overseerr user with user id \"{userId}\" could not found: " + ex.Message); throw new Exception($"Default overseerr user with user id \"{userId}\" could not found."); } } testSuccessful = true; } catch (HttpRequestException ex) { logger.LogWarning(ex, "Error while testing Overseeerr connection: " + ex.Message); throw new Exception("Invalid host and/or port"); } catch (System.Exception ex) { logger.LogWarning(ex, "Error while testing Overseeerr 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"); } }