private static string GetBaseURL(OmbiSettings settings) { var protocol = settings.UseSSL ? "https" : "http"; return($"{protocol}://{settings.Hostname}:{settings.Port}{settings.BaseUrl}"); }
private static async Task <HttpResponseMessage> HttpGetAsync(HttpClient client, OmbiSettings settings, string url) { var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Add("Accept", "application/json"); request.Headers.Add("ApiKey", settings.ApiKey); return(await client.SendAsync(request)); }
public static async Task TestConnectionAsync(HttpClient httpClient, ILogger <OmbiClient> logger, OmbiSettings 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)}/api/v1/Settings/ombi"); if (response.StatusCode == HttpStatusCode.Unauthorized) { 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.baseUrl.ToString().Equals(settings.BaseUrl, StringComparison.InvariantCultureIgnoreCase)) { throw new Exception("Base url does not match what is set in Ombi"); } } catch { throw new Exception("Base url does not match what is set in Ombi"); } testSuccessful = true; } catch (HttpRequestException ex) { logger.LogWarning(ex, "Error while testing Ombi connection: " + ex.Message); throw new Exception("Invalid host and/or port"); } catch (System.Exception ex) { logger.LogWarning(ex, "Error while testing Ombi 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"); } }