Skip to content

Commit

Permalink
Merge pull request #8 from enclave-networks/develop
Browse files Browse the repository at this point in the history
Develop into master first release
  • Loading branch information
enclave-alistair authored Dec 14, 2021
2 parents a1d51f3 + bdf06ba commit 0e8b1e3
Show file tree
Hide file tree
Showing 103 changed files with 5,810 additions and 187 deletions.
14 changes: 0 additions & 14 deletions src/Enclave.Sdk/Clients/AuthorityClient.cs

This file was deleted.

7 changes: 3 additions & 4 deletions src/Enclave.Sdk/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http.Json;
using System.Net.Mime;
using System.Text;
Expand All @@ -10,7 +9,7 @@ namespace Enclave.Sdk.Api.Clients;
/// <summary>
/// Base class used for commonly accessed methods and properties for all clients.
/// </summary>
public abstract class ClientBase
internal abstract class ClientBase
{
/// <summary>
/// HttpClient used for all clients API calls.
Expand All @@ -33,7 +32,7 @@ protected ClientBase(HttpClient httpClient)
/// <param name="data">the object to encode.</param>
/// <returns>String content of object.</returns>
/// <exception cref="ArgumentNullException">throws if data provided is null.</exception>
protected StringContent CreateJsonContent<TModel>(TModel data)
protected static StringContent CreateJsonContent<TModel>(TModel data)
{
if (data is null)
{
Expand All @@ -52,7 +51,7 @@ protected StringContent CreateJsonContent<TModel>(TModel data)
/// <typeparam name="TModel">the object type to deserialise to.</typeparam>
/// <param name="httpContent">httpContent from the API call.</param>
/// <returns>the object of type specified.</returns>
protected async Task<TModel?> DeserialiseAsync<TModel>(HttpContent httpContent)
protected static async Task<TModel?> DeserialiseAsync<TModel>(HttpContent httpContent)
{
if (httpContent is null)
{
Expand Down
239 changes: 237 additions & 2 deletions src/Enclave.Sdk/Clients/DNSClient.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,249 @@
using System.Net.Http.Json;
using System.Web;
using Enclave.Sdk.Api.Clients.Interfaces;
using Enclave.Sdk.Api.Data;
using Enclave.Sdk.Api.Data.Dns;
using Enclave.Sdk.Api.Data.Pagination;
using Enclave.Sdk.Api.Data.PatchModel;

namespace Enclave.Sdk.Api.Clients;

public class DnsClient : ClientBase, IDnsClient
/// <inheritdoc cref="IDnsClient" />
internal class DnsClient : ClientBase, IDnsClient
{
private string _orgRoute;
private readonly string _orgRoute;

/// <summary>
/// Constructor which will be called by <see cref="OrganisationClient"/> when it's created.
/// </summary>
/// <param name="httpClient">an instance of httpClient with a baseURL referencing the API.</param>
/// <param name="orgRoute">The organisation API route.</param>
public DnsClient(HttpClient httpClient, string orgRoute)
: base(httpClient)
{
_orgRoute = orgRoute;
}

/// <inheritdoc/>
public async Task<DnsSummary> GetPropertiesSummaryAsync()
{
var model = await HttpClient.GetFromJsonAsync<DnsSummary>($"{_orgRoute}/dns", Constants.JsonSerializerOptions);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<PaginatedResponseModel<DnsZoneSummary>> GetZonesAsync(int? pageNumber = null, int? perPage = null)
{
var queryString = BuildQueryString(null, null, pageNumber, perPage);

var model = await HttpClient.GetFromJsonAsync<PaginatedResponseModel<DnsZoneSummary>>($"{_orgRoute}/dns/zones?{queryString}");

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<DnsZone> CreateZoneAsync(DnsZoneCreate createModel)
{
if (createModel is null)
{
throw new ArgumentNullException(nameof(createModel));
}

var result = await HttpClient.PostAsJsonAsync($"{_orgRoute}/dns/zones", createModel, Constants.JsonSerializerOptions);

var model = await DeserialiseAsync<DnsZone>(result.Content);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<DnsZone> GetZoneAsync(DnsZoneId dnsZoneId)
{
var model = await HttpClient.GetFromJsonAsync<DnsZone>($"{_orgRoute}/dns/zones/{dnsZoneId}", Constants.JsonSerializerOptions);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<DnsZone> UpdateZoneAsync(DnsZoneId dnsZoneId, PatchBuilder<DnsZonePatch> builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

using var encoded = CreateJsonContent(builder.Send());
var result = await HttpClient.PatchAsync($"{_orgRoute}/dns/zones/{dnsZoneId}", encoded);

result.EnsureSuccessStatusCode();

var model = await DeserialiseAsync<DnsZone>(result.Content);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<DnsZone> DeleteZoneAsync(DnsZoneId dnsZoneId)
{
var result = await HttpClient.DeleteAsync($"{_orgRoute}/dns/zones/{dnsZoneId}");

result.EnsureSuccessStatusCode();

var model = await DeserialiseAsync<DnsZone>(result.Content);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<PaginatedResponseModel<DnsRecordSummary>> GetRecordsAsync(
DnsZoneId? dnsZoneId = null,
string? hostname = null,
int? pageNumber = null,
int? perPage = null)
{
var queryString = BuildQueryString(dnsZoneId, hostname, pageNumber, perPage);

var model = await HttpClient.GetFromJsonAsync<PaginatedResponseModel<DnsRecordSummary>>($"{_orgRoute}/dns/records?{queryString}");

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<DnsRecord> CreateRecordAsync(DnsRecordCreate createModel)
{
if (createModel is null)
{
throw new ArgumentNullException(nameof(createModel));
}

var result = await HttpClient.PostAsJsonAsync($"{_orgRoute}/dns/records", createModel, Constants.JsonSerializerOptions);

var model = await DeserialiseAsync<DnsRecord>(result.Content);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<int> DeleteRecordsAsync(params DnsRecordId[] records)
{
using var content = CreateJsonContent(new
{
recordIds = records,
});

using var request = new HttpRequestMessage
{
Content = content,
Method = HttpMethod.Delete,
RequestUri = new Uri($"{HttpClient.BaseAddress}{_orgRoute}/dns/records"),
};

var result = await HttpClient.SendAsync(request);

result.EnsureSuccessStatusCode();

var model = await DeserialiseAsync<BulkDnsRecordDeleteResult>(result.Content);

EnsureNotNull(model);

return model.DnsRecordsDeleted;
}

/// <inheritdoc/>
public async Task<int> DeleteRecordsAsync(IEnumerable<DnsRecordId> records)
{
if (records is null)
{
throw new ArgumentNullException(nameof(records));
}

return await DeleteRecordsAsync(records.ToArray());
}

/// <inheritdoc/>
public async Task<DnsRecord> GetRecordAsync(DnsRecordId dnsRecordId)
{
var model = await HttpClient.GetFromJsonAsync<DnsRecord>($"{_orgRoute}/dns/records/{dnsRecordId}", Constants.JsonSerializerOptions);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<DnsRecord> UpdateRecordAsync(DnsRecordId dnsRecordId, PatchBuilder<DnsRecordPatch> builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

using var encoded = CreateJsonContent(builder.Send());
var result = await HttpClient.PatchAsync($"{_orgRoute}/dns/records/{dnsRecordId}", encoded);

result.EnsureSuccessStatusCode();

var model = await DeserialiseAsync<DnsRecord>(result.Content);

EnsureNotNull(model);

return model;
}

/// <inheritdoc/>
public async Task<DnsRecord> DeleteRecordAsync(DnsRecordId dnsRecordId)
{
var result = await HttpClient.DeleteAsync($"{_orgRoute}/dns/records/{dnsRecordId}");

result.EnsureSuccessStatusCode();

var model = await DeserialiseAsync<DnsRecord>(result.Content);

EnsureNotNull(model);

return model;
}

private static string? BuildQueryString(DnsZoneId? dnsZoneId, string? hostname, int? pageNumber, int? perPage)
{
var queryString = HttpUtility.ParseQueryString(string.Empty);
if (dnsZoneId is not null)
{
queryString.Add("zoneId", dnsZoneId.ToString());
}

if (hostname is not null)
{
queryString.Add("hostname", hostname);
}

if (pageNumber is not null)
{
queryString.Add("page", pageNumber.ToString());
}

if (perPage is not null)
{
queryString.Add("per_page", perPage.ToString());
}

return queryString.ToString();
}
}
Loading

0 comments on commit 0e8b1e3

Please sign in to comment.