private static HttpClient InitClient(OpenHABHttpClientType connectionType, bool disposable = false) { if (string.IsNullOrWhiteSpace(BaseUrl) && !disposable) { return(null); } var handler = new HttpClientHandler(); if (_settings.WillIgnoreSSLCertificate.HasValue && _settings.WillIgnoreSSLHostname.HasValue) { handler.ServerCertificateCustomValidationCallback = CheckValidationResult; } var credentials = GetCredentials(connectionType); if (credentials != null) { handler.Credentials = credentials; } var client = new HttpClient(handler); if (!disposable) { client.BaseAddress = new Uri(BaseUrl); } return(client); }
private static NetworkCredential GetCredentials(OpenHABHttpClientType connectionType) { string username = connectionType == OpenHABHttpClientType.Local ? _settings.Username : _settings.RemoteUsername; string password = connectionType == OpenHABHttpClientType.Local ? _settings.Password : _settings.RemotePassword; if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password)) { return(new NetworkCredential(username, password)); } return(null); }
private async Task <bool> SetValidUrl(Settings settings) { var isRunningInDemoMode = settings.IsRunningInDemoMode != null && settings.IsRunningInDemoMode.Value; // no url configured yet if (string.IsNullOrWhiteSpace(settings.OpenHABUrl) && string.IsNullOrWhiteSpace(settings.OpenHABRemoteUrl) && !isRunningInDemoMode) { return(false); } if (isRunningInDemoMode) { OpenHABHttpClient.BaseUrl = Constants.Api.DemoModeUrl; return(true); } if (NetworkHelper.Instance.ConnectionInformation.IsInternetOnMeteredConnection) { if (settings.OpenHABRemoteUrl.Trim() == string.Empty) { throw new OpenHABException("No remote url configured"); } OpenHABHttpClient.BaseUrl = settings.OpenHABRemoteUrl; _connectionType = OpenHABHttpClientType.Remote; return(true); } bool isReachable = await CheckUrlReachability(settings.OpenHABUrl, settings, OpenHABHttpClientType.Local).ConfigureAwait(false); if (isReachable) { OpenHABHttpClient.BaseUrl = settings.OpenHABUrl; _connectionType = OpenHABHttpClientType.Local; return(true); } else { // If remote URL is configured if (!string.IsNullOrWhiteSpace(settings.OpenHABRemoteUrl) && await CheckUrlReachability(settings.OpenHABRemoteUrl, settings, OpenHABHttpClientType.Remote).ConfigureAwait(false)) { OpenHABHttpClient.BaseUrl = settings.OpenHABRemoteUrl; _connectionType = OpenHABHttpClientType.Remote; return(true); } Messenger.Default.Send <FireInfoMessage>(new FireInfoMessage(MessageType.NotReachable)); } return(false); }
/// <summary> /// Initializes a new instance of the <see cref="ConnectionDialogViewModel"/> class. /// </summary> /// <param name="connectionConfig">The connection configuration.</param> /// <param name="openHabsdk">OpenHABSDK class.</param> public ConnectionDialogViewModel(OpenHABConnection connectionConfig, IOpenHAB openHabsdk, OpenHABHttpClientType type) : base(connectionConfig) { _openHabsdk = openHabsdk; _type = type; List <ConnectionProfileViewModel> list = new List <ConnectionProfileViewModel>(Settings.ConnectionProfiles.Where(x => x.Type == _type).OrderBy(x => x.Id).Select(x => new ConnectionProfileViewModel(x))); _profiles = new ObservableCollection <ConnectionProfileViewModel>(list); if (Model != null) { _profile = list.FirstOrDefault(x => x.Id == Model.Profile.Id); } }
/// <summary> /// Initializes a new instance of the <see cref="ConnectionDialogViewModel"/> class. /// </summary> /// <param name="connectionConfig">The connection configuration.</param> /// <param name="openHabsdk">OpenHABSDK class.</param> /// <param name="type">Defines if openHAB instance is local or remote.</param> public ConnectionDialogViewModel(OpenHABConnection connectionConfig, IOpenHAB openHabsdk, OpenHABHttpClientType type) : base(connectionConfig) { _openHabsdk = openHabsdk; _type = type; _connectionState = OpenHABUrlState.Unknown; List <ConnectionProfileViewModel> list = new List <ConnectionProfileViewModel>(Settings.ConnectionProfiles.Where(x => x.Type == _type).OrderBy(x => x.Id).Select(x => new ConnectionProfileViewModel(x))); _profiles = new ObservableCollection <ConnectionProfileViewModel>(list); if (Model != null) { _profile = list.FirstOrDefault(x => x.Id == Model.Profile.Id); } if (!string.IsNullOrEmpty(Model?.Url)) { CheckConnectionSettings(Model.Url); } }
/// <inheritdoc/> public async Task <bool> CheckUrlReachability(string openHABUrl, Settings settings, OpenHABHttpClientType connectionType) { if (string.IsNullOrWhiteSpace(openHABUrl)) { return(false); } if (!openHABUrl.EndsWith("/")) { openHABUrl = openHABUrl + "/"; } try { var client = OpenHABHttpClient.DisposableClient(connectionType, settings); var result = await client.GetAsync(openHABUrl + "rest").ConfigureAwait(false); if (result.IsSuccessStatusCode) { return(true); } } catch (InvalidOperationException) { return(false); } catch (HttpRequestException) { return(false); } return(false); }
/// <summary> /// Create an HttpClient instance for one-time use. /// </summary> /// <returns>The HttpClient instance.</returns> public static HttpClient DisposableClient(OpenHABHttpClientType connectionType, Settings settings) { _settings = settings; return(InitClient(connectionType, true)); }
/// <summary> /// Fetch the HttpClient instance. /// </summary> /// <returns>The HttpClient instance</returns> public static HttpClient Client(OpenHABHttpClientType connectionType, Settings settings) { _settings = settings; return(_client ?? (_client = InitClient(connectionType))); }