private static HttpClient GetHttpClientWithPreferences(IPreferences preferences) { if (preferences.GetBoolValue(WellKnownPreference.UseDefaultCredentials)) { return(new HttpClient(new HttpClientHandler { UseDefaultCredentials = true })); } return(new HttpClient()); }
private static HttpClient GetHttpClientWithPreferences(IPreferences preferences) { bool useDefaultCredentials = preferences.GetBoolValue(WellKnownPreference.UseDefaultCredentials); bool proxyUseDefaultCredentials = preferences.GetBoolValue(WellKnownPreference.ProxyUseDefaultCredentials); if (useDefaultCredentials || proxyUseDefaultCredentials) { #pragma warning disable CA2000 // Dispose objects before losing scope HttpClientHandler handler = new HttpClientHandler() { UseDefaultCredentials = useDefaultCredentials, DefaultProxyCredentials = proxyUseDefaultCredentials ? CredentialCache.DefaultCredentials : null }; return(new HttpClient(handler)); #pragma warning restore CA2000 // Dispose objects before losing scope } return(new HttpClient()); }
private static HttpClient GetHttpClientWithPreferences(IPreferences preferences) { if (preferences.GetBoolValue(WellKnownPreference.UseDefaultCredentials)) { #pragma warning disable CA2000 // Dispose objects before losing scope return(new HttpClient(new HttpClientHandler { UseDefaultCredentials = true })); #pragma warning restore CA2000 // Dispose objects before losing scope } return(new HttpClient()); }
private ApiConnection GetConnectionInfo(IShellState shellState, HttpState programState, string rootAddress, string baseAddress, string swaggerAddress, IPreferences preferences, bool isVerbosityEnabled) { rootAddress = rootAddress?.Trim(); baseAddress = baseAddress?.Trim(); swaggerAddress = swaggerAddress?.Trim(); if (string.IsNullOrWhiteSpace(rootAddress) && string.IsNullOrWhiteSpace(baseAddress) && string.IsNullOrWhiteSpace(swaggerAddress)) { shellState.ConsoleManager.Error.WriteLine(Resources.Strings.ConnectCommand_Error_NothingSpecified); return(null); } if (!string.IsNullOrWhiteSpace(rootAddress) && !Uri.IsWellFormedUriString(rootAddress, UriKind.Absolute)) { shellState.ConsoleManager.Error.WriteLine(Resources.Strings.ConnectCommand_Error_RootAddressNotValid); return(null); } // Even if verbosity is not enabled, we still want to be verbose about finding OpenAPI Descriptions // if they specified one directly. bool logVerboseMessages = isVerbosityEnabled || !string.IsNullOrWhiteSpace(swaggerAddress); ApiConnection apiConnection = new ApiConnection(programState, preferences, shellState.ConsoleManager, logVerboseMessages); if (!string.IsNullOrWhiteSpace(rootAddress)) { // The `dotnet new webapi` template now has a default start url of `swagger`. Because // the default Swashbuckle-generated OpenAPI description doesn't contain a Servers element // this will put HttpRepl users into a pit of failure by having a base address of // https://localhost:{port}/swagger/, even though the API is, by default, based at the root. // Since it is unlikely a user would put their API inside the /swagger path, we will // special-case this scenario and remove that from the url. We will give the user an escape // hatch via the preference if they do put their API under that path. if (rootAddress.EndsWith(WebApiDefaultPathSuffix, StringComparison.OrdinalIgnoreCase)) { WebApiF5FixEvent fixEvent; if (preferences.GetBoolValue(WellKnownPreference.ConnectCommandSkipRootFix)) { fixEvent = new WebApiF5FixEvent(skippedByPreference: true); } else { rootAddress = rootAddress.Substring(0, rootAddress.Length - WebApiDefaultPathSuffix.Length); fixEvent = new WebApiF5FixEvent(); } _telemetry.TrackEvent(fixEvent); } apiConnection.RootUri = new Uri(rootAddress, UriKind.Absolute); } if (!SetupBaseAddress(shellState, baseAddress, apiConnection) || !SetupSwaggerAddress(shellState, swaggerAddress, apiConnection)) { return(null); } apiConnection.AllowBaseOverrideBySwagger = !apiConnection.HasBaseUri; if (apiConnection.HasRootUri && !apiConnection.HasBaseUri) { apiConnection.BaseUri = apiConnection.RootUri; } return(apiConnection); }