using Logging; namespace WebUtils { public interface IHttpFactory { IHttp CreateHttp(string id, Action onClientCreated); IHttp CreateHttp(string id, Action onClientCreated, IWebCallTimeSet timeSet); IHttp CreateHttp(string id); IWebCallTimeSet WebCallTimeSet { get; } } public class HttpFactory : IHttpFactory { private readonly ILog log; private readonly IWebCallTimeSet timeSet; private readonly Action factoryOnClientCreated; public HttpFactory(ILog log) : this (log, new DefaultWebCallTimeSet()) { } public HttpFactory(ILog log, Action onClientCreated) : this(log, new DefaultWebCallTimeSet(), onClientCreated) { } public HttpFactory(ILog log, IWebCallTimeSet defaultTimeSet) : this(log, defaultTimeSet, DoNothing) { } public HttpFactory(ILog log, IWebCallTimeSet defaultTimeSet, Action onClientCreated) { this.log = log; timeSet = defaultTimeSet; factoryOnClientCreated = onClientCreated; } public IWebCallTimeSet WebCallTimeSet => timeSet; public IHttp CreateHttp(string id, Action onClientCreated) { return CreateHttp(id, onClientCreated, timeSet); } public IHttp CreateHttp(string id, Action onClientCreated, IWebCallTimeSet ts) { return new Http(id, log, ts, (c) => { factoryOnClientCreated(c); onClientCreated(c); }); } public IHttp CreateHttp(string id) { return new Http(id, log, timeSet, factoryOnClientCreated); } private static void DoNothing(HttpClient client) { } } }