public void ResetAuth() { lock (_pool) { foreach (var client in _pool.Where(x => x.State == ApiClientState.Available).ToList()) { this.SetAuth(client); } PoolChanged?.Invoke(this, new EventArgs()); } }
public HttpClient Fetch() { lock (_pool) { var client = _pool.FirstOrDefault(x => x.State == State.Available); if (client == null) { // No available clients, create a new one, use message handler if specified in configuration if (Configuration.MessageHandler != null) { client = new TinyHttpClient(Configuration.MessageHandler); } else { client = new TinyHttpClient(); } // Allow for user injected initialization of the client ClientInitializationOnCreation?.Invoke(client); // Hook up events client.OnDispose += (sender, e) => { client.State = State.Available; if (Configuration.ResetHeadersOnReuse) { client.DefaultRequestHeaders.Clear(); } PoolChanged?.Invoke(this, new EventArgs()); }; _pool.Add(client); } // Check if there is any configuration we need if (!String.IsNullOrWhiteSpace(Configuration.BaseUrl)) { client.BaseAddress = new Uri(Configuration.BaseUrl); } ClientInitializationOnFetch?.Invoke(client); client.State = State.InUse; PoolChanged?.Invoke(this, new EventArgs()); return(client); } }
public void Flush() { lock (_pool) { foreach (var client in _pool.Where(x => x.State == State.Available).ToList()) { client.State = State.Disposed; client.InternalDispose(); _pool.Remove(client); } PoolChanged?.Invoke(this, new EventArgs()); } }
public ApiClient Fetch() { lock (_pool) { var client = _pool.FirstOrDefault(x => x.State == ApiClientState.Available); if (client == null) { if (Configuration.MessageHandler != null) { client = new ApiClient(Configuration.MessageHandler); } else { client = new ApiClient(); } ClientInitializationOnCreation?.Invoke(client); client.OnDispose += (sender, e) => { client.State = ApiClientState.Available; if (Configuration.ResetHeadersOnReuse) { client.DefaultRequestHeaders.Clear(); } PoolChanged?.Invoke(this, new EventArgs()); }; _pool.Add(client); } if (!String.IsNullOrWhiteSpace(Configuration.BaseUrl)) { client.BaseAddress = new Uri(Configuration.BaseUrl); } this.SetAuth(client); ClientInitializationOnFetch?.Invoke(client); client.State = ApiClientState.InUse; PoolChanged?.Invoke(this, new EventArgs()); return(client); } }