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
63
64
65
66
67
68
69
70
71
72
73
|
using PKISharp.WACS.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace PKISharp.WACS.Plugins.ValidationPlugins.Dreamhost
{
public class DnsManagementClient
{
private readonly string _apiKey;
private readonly ILogService _logService;
private readonly string uri = "https://api.dreamhost.com/";
public DnsManagementClient(string apiKey, ILogService logService)
{
_apiKey = apiKey;
_logService = logService;
}
public async Task CreateRecord(string record, RecordType type, string value)
{
var response = await SendRequest("dns-add_record",
new Dictionary<string, string>
{
{"record", record},
{"type", type.ToString()},
{"value", value}
});
var content = await response.Content.ReadAsStringAsync();
_logService.Information("Dreamhost Responded with: {0}", content);
_logService.Information("Waiting for 30 seconds");
await Task.Delay(30000);
}
public async Task DeleteRecord(string record, RecordType type, string value)
{
var args = new Dictionary<string, string>
{
{"record", record},
{"type", type.ToString()},
{"value", value}
};
var response = await SendRequest("dns-remove_record", args);
var content = await response.Content.ReadAsStringAsync();
_logService.Information("Dreamhost Responded with: {0}", content);
_logService.Information("Waiting for 30 seconds");
await Task.Delay(30000);
}
private async Task<HttpResponseMessage> SendRequest(string command, IEnumerable<KeyValuePair<string, string>> args)
{
using (var client = new HttpClient { BaseAddress = new Uri(uri) })
{
var queryString = new Dictionary<string, string>
{
{ "key", _apiKey },
{ "unique_id", Guid.NewGuid().ToString() },
{ "format", "json" },
{ "cmd", command }
};
foreach (var arg in args)
{
queryString.Add(arg.Key, arg.Value);
}
return await client.GetAsync("?" + string.Join("&", queryString.Select(kvp => $"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}")));
};
}
}
}
|