68 lines
1.9 KiB
C#
Raw Normal View History

2025-01-16 11:31:50 +01:00
using Logging;
namespace WebUtils
{
public interface IHttpFactory
{
IHttp CreateHttp(string id, Action<HttpClient> onClientCreated);
IHttp CreateHttp(string id, Action<HttpClient> onClientCreated, IWebCallTimeSet timeSet);
IHttp CreateHttp(string id);
2025-07-09 14:48:51 +02:00
IWebCallTimeSet WebCallTimeSet { get; }
2025-01-16 11:31:50 +01:00
}
public class HttpFactory : IHttpFactory
{
private readonly ILog log;
private readonly IWebCallTimeSet timeSet;
2025-04-09 15:25:31 +02:00
private readonly Action<HttpClient> factoryOnClientCreated;
2025-01-16 11:31:50 +01:00
public HttpFactory(ILog log)
: this (log, new DefaultWebCallTimeSet())
{
}
2025-04-09 15:25:31 +02:00
public HttpFactory(ILog log, Action<HttpClient> onClientCreated)
: this(log, new DefaultWebCallTimeSet(), onClientCreated)
{
}
2025-01-16 11:31:50 +01:00
public HttpFactory(ILog log, IWebCallTimeSet defaultTimeSet)
2025-04-09 15:25:31 +02:00
: this(log, defaultTimeSet, DoNothing)
{
}
public HttpFactory(ILog log, IWebCallTimeSet defaultTimeSet, Action<HttpClient> onClientCreated)
2025-01-16 11:31:50 +01:00
{
this.log = log;
timeSet = defaultTimeSet;
factoryOnClientCreated = onClientCreated;
2025-01-16 11:31:50 +01:00
}
2025-07-09 14:48:51 +02:00
public IWebCallTimeSet WebCallTimeSet => timeSet;
2025-01-16 11:31:50 +01:00
public IHttp CreateHttp(string id, Action<HttpClient> onClientCreated)
{
return CreateHttp(id, onClientCreated, timeSet);
2025-01-16 11:31:50 +01:00
}
public IHttp CreateHttp(string id, Action<HttpClient> onClientCreated, IWebCallTimeSet ts)
{
2025-04-09 15:25:31 +02:00
return new Http(id, log, ts, (c) =>
{
factoryOnClientCreated(c);
onClientCreated(c);
});
2025-01-16 11:31:50 +01:00
}
public IHttp CreateHttp(string id)
{
return new Http(id, log, timeSet, factoryOnClientCreated);
2025-04-09 15:25:31 +02:00
}
private static void DoNothing(HttpClient client)
{
2025-01-16 11:31:50 +01:00
}
}
}