Skip to content

Commit a2c74c3

Browse files
committed
Add list zone endpoint
1 parent 3cc3724 commit a2c74c3

File tree

7 files changed

+67
-0
lines changed

7 files changed

+67
-0
lines changed

src/Core/AppJsonSerializerContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Core;
66

77
[JsonSerializable(typeof(UpdateDnsRecordRequest))]
8+
[JsonSerializable(typeof(ListDnsRecordsResponse))]
9+
[JsonSerializable(typeof(ListResponse))]
810
[JsonSerializable(typeof(UpdateDnsRecordResponse))]
911
[JsonSerializable(typeof(UpdateResponse))]
1012
public partial class AppJsonSerializerContext : JsonSerializerContext

src/Core/Clients/CloudflareClient.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Text.Json;
22
using Core.Requests;
3+
using Core.Responses;
34

45
namespace Core.Clients;
56

@@ -35,4 +36,22 @@ public async Task UpdateDnsRecord(string zoneId, string dnsRecordId, string ipAd
3536
throw new Exception("Received unsuccessful response from Cloudflare API");
3637
}
3738
}
39+
40+
public async Task<IList<DnsRecordResponse>> ListDnsRecords(string zoneId)
41+
{
42+
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"zones/{zoneId}/dns_records");
43+
44+
using var responseMessage = await httpClient.SendAsync(requestMessage);
45+
46+
responseMessage.EnsureSuccessStatusCode();
47+
48+
var response = await responseMessage.Content.ReadFromJsonAsync(_context.ListDnsRecordsResponse);
49+
50+
if (response is not { Success: true })
51+
{
52+
throw new Exception("Received unsuccessful response from Cloudflare API");
53+
}
54+
55+
return response.Result;
56+
}
3857
}

src/Core/Handlers/ListHandler.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Core.Clients;
2+
using Core.Responses;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace Core.Handlers;
6+
7+
public static class ListHandler
8+
{
9+
public static async Task<IResult> Handle(
10+
CloudflareClient cloudflareClient,
11+
[FromQuery] string zoneId
12+
)
13+
{
14+
var records = await cloudflareClient.ListDnsRecords(zoneId);
15+
16+
return Results.Ok(new ListResponse
17+
{
18+
Records = records
19+
});
20+
}
21+
}

src/Core/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static void Main(string[] args)
2525
var app = builder.Build();
2626

2727
app.MapGet("/update", UpdateHandler.Handle);
28+
app.MapGet("/list", ListHandler.Handle);
2829

2930
app.Run();
3031
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Core.Responses;
2+
3+
public record DnsRecordResponse
4+
{
5+
public required string Id { get; init; }
6+
7+
public required string Name { get; init; }
8+
9+
public required string Type { get; init; }
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Core.Responses;
2+
3+
public record ListDnsRecordsResponse
4+
{
5+
public required bool Success { get; set; }
6+
7+
public required IList<DnsRecordResponse> Result { get; set; }
8+
};

src/Core/Responses/ListResponse.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Core.Responses;
2+
3+
public record ListResponse
4+
{
5+
public required IList<DnsRecordResponse> Records { get; init; }
6+
}

0 commit comments

Comments
 (0)