HttpFactory.cs
1 using Logging; 2 3 namespace WebUtils 4 { 5 public interface IHttpFactory 6 { 7 IHttp CreateHttp(string id, Action<HttpClient> onClientCreated); 8 IHttp CreateHttp(string id, Action<HttpClient> onClientCreated, IWebCallTimeSet timeSet); 9 IHttp CreateHttp(string id); 10 11 IWebCallTimeSet WebCallTimeSet { get; } 12 } 13 14 public class HttpFactory : IHttpFactory 15 { 16 private readonly ILog log; 17 private readonly IWebCallTimeSet timeSet; 18 private readonly Action<HttpClient> factoryOnClientCreated; 19 20 public HttpFactory(ILog log) 21 : this (log, new DefaultWebCallTimeSet()) 22 { 23 } 24 25 public HttpFactory(ILog log, Action<HttpClient> onClientCreated) 26 : this(log, new DefaultWebCallTimeSet(), onClientCreated) 27 { 28 } 29 30 public HttpFactory(ILog log, IWebCallTimeSet defaultTimeSet) 31 : this(log, defaultTimeSet, DoNothing) 32 { 33 } 34 35 public HttpFactory(ILog log, IWebCallTimeSet defaultTimeSet, Action<HttpClient> onClientCreated) 36 { 37 this.log = log; 38 timeSet = defaultTimeSet; 39 factoryOnClientCreated = onClientCreated; 40 } 41 42 public IWebCallTimeSet WebCallTimeSet => timeSet; 43 44 public IHttp CreateHttp(string id, Action<HttpClient> onClientCreated) 45 { 46 return CreateHttp(id, onClientCreated, timeSet); 47 } 48 49 public IHttp CreateHttp(string id, Action<HttpClient> onClientCreated, IWebCallTimeSet ts) 50 { 51 return new Http(id, log, ts, (c) => 52 { 53 factoryOnClientCreated(c); 54 onClientCreated(c); 55 }); 56 } 57 58 public IHttp CreateHttp(string id) 59 { 60 return new Http(id, log, timeSet, factoryOnClientCreated); 61 } 62 63 private static void DoNothing(HttpClient client) 64 { 65 } 66 } 67 }