-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirmwareBackupClientFactory.cs
More file actions
62 lines (52 loc) · 2.46 KB
/
Copy pathFirmwareBackupClientFactory.cs
File metadata and controls
62 lines (52 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using CreativeCoders.Core;
using CreativeCoders.HomeMatic.FirmwareBackup.Internal;
using System.IO.Abstractions;
namespace CreativeCoders.HomeMatic.FirmwareBackup;
/// <summary>
/// Default <see cref="IFirmwareBackupClientFactory"/> implementation. Resolves the configured
/// <see cref="HttpClient"/> via <see cref="IHttpClientFactory"/> and wires up a
/// <see cref="FirmwareBackupClient"/> with its internal collaborators.
/// </summary>
public sealed class FirmwareBackupClientFactory : IFirmwareBackupClientFactory
{
/// <summary>
/// Name of the named <see cref="HttpClient"/> registered for firmware backup operations
/// using the platform's standard server certificate validation.
/// </summary>
public const string HttpClientName = "CreativeCoders.HomeMatic.FirmwareBackup";
/// <summary>
/// Name of the named <see cref="HttpClient"/> registered for firmware backup operations
/// that accepts any (including self-signed) server certificate.
/// </summary>
public const string HttpClientNameAcceptAnyCertificate =
HttpClientName + ".AcceptAnyCertificate";
private readonly IHttpClientFactory _httpClientFactory;
private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of <see cref="FirmwareBackupClientFactory"/>.
/// </summary>
/// <param name="httpClientFactory">Factory used to obtain the named HTTP client.</param>
/// <param name="fileSystem">File system abstraction used by created clients.</param>
public FirmwareBackupClientFactory(IHttpClientFactory httpClientFactory, IFileSystem fileSystem)
{
_httpClientFactory = Ensure.NotNull(httpClientFactory);
_fileSystem = Ensure.NotNull(fileSystem);
}
/// <inheritdoc />
public IFirmwareBackupClient Create(FirmwareBackupOptions options)
{
Ensure.NotNull(options);
var clientName = options.AcceptAnyServerCertificate
? HttpClientNameAcceptAnyCertificate
: HttpClientName;
var httpClient = _httpClientFactory.CreateClient(clientName);
httpClient.Timeout = options.Timeout;
var sessionClient = new CcuSessionClient(httpClient, options.BaseUrl, options.JsonRpcPath);
var downloader = new FirmwareBackupDownloader(
httpClient,
options.BaseUrl,
options.BackupCgiPath,
options.BackupAction);
return new FirmwareBackupClient(sessionClient, downloader, options, _fileSystem);
}
}