/// <summary> /// Create a copy of the HTTP client builder whose clients will log requests and responses to the specified logger. /// </summary> /// <param name="clientBuilder"> /// The HTTP client builder. /// </param> /// <param name="loggerFactory"> /// A delegate that produces the logger for each client. /// </param> /// <returns> /// The new <see cref="ClientBuilder"/>. /// </returns> /// <remarks> /// Each call to <paramref name="loggerFactory"/> should return a new instance of the logger (it's not good practice to share the same instance of a logger between multiple clients). /// </remarks> public static ClientBuilder WithLogging(this ClientBuilder clientBuilder, Func <ILogger> loggerFactory) { return(clientBuilder.AddHandler(() => { ILogger logger = loggerFactory(); return new LoggingMessageHandler(logger); })); }
/// <summary> /// Create and configure a <see cref="KubeApiClient"/> using the specified options. /// </summary> /// <param name="options"> /// The <see cref="KubeClientOptions"/> used to configure the client. /// </param> /// <param name="loggerFactory"> /// An optional <see cref="ILoggerFactory"/> used to create loggers for client components. /// </param> /// <returns> /// The configured <see cref="KubeApiClient"/>. /// </returns> public static KubeApiClient Create(KubeClientOptions options, ILoggerFactory loggerFactory = null) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.EnsureValid(); var clientBuilder = new ClientBuilder(); if (!String.IsNullOrWhiteSpace(options.AccessToken)) { clientBuilder = clientBuilder.AddHandler( () => new BearerTokenHandler(options.AccessToken) ); } if (options.AllowInsecure) { clientBuilder = clientBuilder.AcceptAnyServerCertificate(); } else if (options.CertificationAuthorityCertificate != null) { clientBuilder = clientBuilder.WithServerCertificate(options.CertificationAuthorityCertificate); } if (options.ClientCertificate != null) { clientBuilder = clientBuilder.WithClientCertificate(options.ClientCertificate); } if (loggerFactory != null) { LogMessageComponents logComponents = LogMessageComponents.Basic; if (options.LogHeaders) { logComponents |= LogMessageComponents.Headers; } if (options.LogPayloads) { logComponents |= LogMessageComponents.Body; } clientBuilder = clientBuilder.WithLogging( logger: loggerFactory.CreateLogger( typeof(KubeApiClient).FullName + ".Http" ), requestComponents: logComponents, responseComponents: logComponents ); } HttpClient httpClient = clientBuilder.CreateClient(options.ApiEndPoint); return(new KubeApiClient(httpClient, options, loggerFactory)); }
/// <summary> /// Create a copy of the <see cref="ClientBuilder"/>, enabling activity-correlation support for its clients. /// </summary> /// <param name="clientBuilder"> /// The HTTP client builder. /// </param> /// <returns> /// The new HTTP client builder. /// </returns> public static ClientBuilder WithActivityCorrelation(this ClientBuilder clientBuilder) { if (clientBuilder == null) { throw new ArgumentNullException(nameof(clientBuilder)); } return(clientBuilder.AddHandler( () => new CorrelationMessageHandler() )); }
/// <summary> /// Create a copy of the <see cref="ClientBuilder"/> with transparent authentication support for its clients. /// </summary> /// <param name="clientBuilder"> /// The HTTP client builder. /// </param> /// <param name="authenticationProviderFactory"> /// A/// delegate that creates a new <see cref="IHttpRequestAuthenticationProvider"/> for each <see cref="HttpClient"/> produced by the <see cref="ClientBuilder"/>. /// </param> /// <returns> /// The new HTTP client builder. /// </returns> public static ClientBuilder WithAuthentication(this ClientBuilder clientBuilder, Func <IHttpRequestAuthenticationProvider> authenticationProviderFactory) { if (clientBuilder == null) { throw new ArgumentNullException(nameof(clientBuilder)); } if (authenticationProviderFactory == null) { throw new ArgumentNullException(nameof(authenticationProviderFactory)); } return(clientBuilder.AddHandler(() => { IHttpRequestAuthenticationProvider authenticationProvider = authenticationProviderFactory(); return new AuthenticationMessageHandler(authenticationProvider); })); }
/// <summary> /// Create and configure a <see cref="KubeApiClient"/> using the specified options. /// </summary> /// <param name="options"> /// The <see cref="KubeClientOptions"/> used to configure the client. /// </param> /// <param name="loggerFactory"> /// An optional <see cref="ILoggerFactory"/> used to create loggers for client components. /// </param> /// <returns> /// The configured <see cref="KubeApiClient"/>. /// </returns> public static KubeApiClient Create(KubeClientOptions options, ILoggerFactory loggerFactory = null) { if (options == null) { throw new ArgumentNullException(nameof(options)); } options.EnsureValid(); var clientBuilder = new ClientBuilder(); switch (options.AuthStrategy) { case KubeAuthStrategy.BearerToken: { clientBuilder = clientBuilder.AddHandler( () => new StaticBearerTokenHandler(options.AccessToken) ); break; } case KubeAuthStrategy.BearerTokenProvider: { clientBuilder = clientBuilder.AddHandler( () => new CommandBearerTokenHandler( accessTokenCommand: options.AccessTokenCommand, accessTokenCommandArguments: options.AccessTokenCommandArguments, accessTokenSelector: options.AccessTokenSelector, accessTokenExpirySelector: options.AccessTokenExpirySelector, initialAccessToken: options.InitialAccessToken, initialTokenExpiryUtc: options.InitialTokenExpiryUtc ) ); break; } case KubeAuthStrategy.ClientCertificate: { if (options.ClientCertificate == null) { throw new KubeClientException("Cannot specify ClientCertificate authentication strategy without supplying a client certificate."); } clientBuilder = clientBuilder.WithClientCertificate(options.ClientCertificate); break; } } if (options.AllowInsecure) { clientBuilder = clientBuilder.AcceptAnyServerCertificate(); } else if (options.CertificationAuthorityCertificate != null) { clientBuilder = clientBuilder.WithServerCertificate(options.CertificationAuthorityCertificate); } if (loggerFactory != null) { LogMessageComponents logComponents = LogMessageComponents.Basic; if (options.LogHeaders) { logComponents |= LogMessageComponents.Headers; } if (options.LogPayloads) { logComponents |= LogMessageComponents.Body; } clientBuilder = clientBuilder.WithLogging( logger: loggerFactory.CreateLogger( typeof(KubeApiClient).FullName + ".Http" ), requestComponents: logComponents, responseComponents: logComponents ); } HttpClient httpClient = clientBuilder.CreateClient(options.ApiEndPoint); return(new KubeApiClient(httpClient, options, loggerFactory)); }