btcpayserver/BTCPayServer/Services/LightningClientFactoryService.cs

50 lines
2.1 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-04-11 05:41:38 +02:00
using System.Net.Http;
using BTCPayServer.Lightning;
namespace BTCPayServer.Services
{
public class LightningClientFactoryService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IEnumerable<Func<HttpClient, ILightningConnectionStringHandler>>
_lightningConnectionStringHandlersFactories;
private readonly IEnumerable<ILightningConnectionStringHandler> _lightningConnectionStringHandlers;
public LightningClientFactoryService(IHttpClientFactory httpClientFactory,
IEnumerable<Func<HttpClient, ILightningConnectionStringHandler>> lightningConnectionStringHandlersFactories, IEnumerable<ILightningConnectionStringHandler> lightningConnectionStringHandlers)
2019-04-11 05:41:38 +02:00
{
_httpClientFactory = httpClientFactory;
_lightningConnectionStringHandlersFactories = lightningConnectionStringHandlersFactories;
_lightningConnectionStringHandlers = lightningConnectionStringHandlers;
}
private LightningClientFactory GetFactory(string namedClient, BTCPayNetwork network)
{
var httpClient = _httpClientFactory.CreateClient(namedClient);
return new LightningClientFactory(_lightningConnectionStringHandlersFactories
.Select(handler => handler(httpClient)).Concat(_lightningConnectionStringHandlers)
.ToArray(), network.NBitcoinNetwork);
2019-04-11 05:41:38 +02:00
}
public static string OnionNamedClient { get; set; } = "lightning.onion";
public ILightningClient Create(string lightningConnectionString, BTCPayNetwork network)
2019-04-11 05:41:38 +02:00
{
ArgumentNullException.ThrowIfNull(lightningConnectionString);
ArgumentNullException.ThrowIfNull(network);
var httpClient = lightningConnectionString.Contains(".onion")
? OnionNamedClient
: $"{network.CryptoCode}: Lightning client";
return GetFactory(httpClient, network).Create(lightningConnectionString);
2019-04-11 05:41:38 +02:00
}
}
}