/// <summary> /// Initializes the asynchronous. /// </summary> /// <param name="httpClient">The HTTP client.</param> /// <param name="options">The options.</param> /// <returns></returns> public Task <bool> InitializeAsync(HttpClient httpClient, RestEaseClientFactoryOptions options = null) { Serilog.Log.Information("Client Factory Initialized"); try { HttpClient = httpClient; Options = options; ApiClient = new RestClient(HttpClient) { ResponseDeserializer = Options?.ResponseDeserializer, RequestBodySerializer = Options?.JsonRequestBodySerializer, JsonSerializerSettings = Options?.JsonSerializerSettings }; Api = ApiClient.For <T>(); return(Task.FromResult(true)); } catch (Exception ex) { Serilog.Log.Error(ex, "Failed to initialize API Client"); } return(Task.FromResult(false)); }
/// <summary> /// Initializes the asynchronous. /// </summary> /// <param name="url">The URL.</param> /// <param name="token">The token.</param> /// <param name="options">The options.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">token - token cannot be null</exception> public Task <bool> InitializeAsync(string url, string token, RestEaseClientFactoryOptions options = null) { if (string.IsNullOrEmpty(token)) { throw new ArgumentNullException(nameof(token), "token cannot be null"); } #if DEBUG Serilog.Log.Debug($"Authentication Token: {token}"); #endif return(InitializeAsync(url, new AuthenticatedHttpClientHandler(() => Task.FromResult(token)) { AuthenticationScheme = "Bearer" }, options)); }
/// <summary> /// initialize as an asynchronous operation. /// </summary> /// <param name="url">The URL.</param> /// <param name="clientHandler">The client handler.</param> /// <param name="options">The options.</param> /// <returns> /// Task<System.Boolean>. /// </returns> public Task <bool> InitializeAsync(string url, HttpClientHandler clientHandler, RestEaseClientFactoryOptions options = null) { Serilog.Log.Information("Client Factory Initialized"); HttpClient httpClient; if (options?.EnableHttpLogging == null) { httpClient = new HttpClient(new HttpLoggingHandler(clientHandler)) { BaseAddress = new Uri(url) }; } else { httpClient = new HttpClient(clientHandler) { BaseAddress = new Uri(url) }; } return(InitializeAsync(httpClient, options)); }
/// <summary> /// Initializes the factory. /// </summary> /// <param name="url">The URL.</param> /// <param name="options">The options.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">token - token cannot be null</exception> public Task <bool> InitializeAsync(string url, RestEaseClientFactoryOptions options = null) { return(InitializeAsync(url, new HttpClientHandler(), options)); }
/// <summary> /// initialize the factory. /// </summary> /// <param name="url">The URL.</param> /// <param name="clientHandler">The client handler.</param> /// <param name="options">The options.</param> /// <returns> /// Task<System.Boolean>. /// </returns> public Task <bool> InitializeAsync(string url, HttpClientHandler clientHandler, RestEaseClientFactoryOptions options = null) { Serilog.Log.Information("Client Factory Initialized"); HttpClient httpClient; if (options?.EnableHttpLogging == true) { var loggingHandler = new HttpLoggingHandler(clientHandler); if (OnSendAsyncBefore != null) { loggingHandler.OnSendAsyncBefore += OnSendAsyncBefore; } if (OnSendAsyncAfter != null) { loggingHandler.OnSendAsyncAfter += OnSendAsyncAfter; } httpClient = new HttpClient(new HttpLoggingHandler(clientHandler)) { BaseAddress = new Uri(url) }; } else { httpClient = new HttpClient(clientHandler) { BaseAddress = new Uri(url) }; } return(InitializeAsync(httpClient, options)); }