Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AzureRest sample #13

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace AzureRest.Client.Endpoints
{
[ClientEndpoint("azure-rest-client")]
public interface IGetSubscription
public interface IGetSubscriptionEndpoint
{
[Get("subscriptions/{subscriptionId}?api-version=2022-12-01")]
public Task<EndpointResponse<Subscription>> ExecuteAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
namespace AzureRest.Client.Endpoints
{
[ClientEndpoint("azure-rest-client")]
public interface IListSubscriptions
public interface IListSubscriptionsEndpoint
{
[Get("https://management.azure.com/subscriptions?api-version=2022-12-01")]
[Get("subscriptions?api-version=2022-12-01")]
public Task<EndpointResponse<ListResponse<Subscription>>> ExecuteAsync(
CancellationToken cancellationToken = default);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading;
using System.Threading.Tasks;
using AzureRest.Contracts;
using Cabazure.Client;

namespace AzureRest.Client.Endpoints
{
public interface IListTenantsEndpoint
{
Task<EndpointResponse<ListResponse<Tenant>>> ExecuteAsync(
CancellationToken cancellationToken = default);
}
}
44 changes: 44 additions & 0 deletions samples/AzureRest/AzureRest.Client/Endpoints/ListTenantEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using AzureRest.Contracts;
using Cabazure.Client;
using Cabazure.Client.Builder;

namespace AzureRest.Client.Endpoints
{
internal class ListTenantEndpoint : IListTenantsEndpoint
{
private readonly IHttpClientFactory factory;
private readonly IMessageRequestFactory requestFactory;

public ListTenantEndpoint(
IHttpClientFactory factory,
IMessageRequestFactory requestFactory)
{
this.factory = factory;
this.requestFactory = requestFactory;
}

public async Task<EndpointResponse<ListResponse<Tenant>>> ExecuteAsync(
CancellationToken cancellationToken = default)
{
var client = factory.CreateClient("azure-rest-client");

using var requestMessage = requestFactory
.FromTemplate("azure-rest-client", "tenants?api-version=2022-12-01")
.Build(HttpMethod.Get);

using var response = await client
.SendAsync(requestMessage, cancellationToken);

return await requestFactory
.FromResponse("azure-rest-client", response)
.AddSuccessResponse<ListResponse<Tenant>>(HttpStatusCode.OK)
.GetAsync(
response => new EndpointResponse<ListResponse<Tenant>>(response),
cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using AzureRest.Client;
using AzureRest.Client.Endpoints;
using AzureRest.Contracts.Serialization;

namespace Microsoft.Extensions.DependencyInjection
Expand All @@ -9,9 +10,12 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddAzureRestClient(
this IServiceCollection services,
Action<AzureRestClientOptions>? clientOptions = null)
=> services.AddCabazureClient(
"azure-rest-client",
j => JsonSerializerOptionsFactory.Configure(j),
clientOptions);
=> services
// Custom endpoint needs to be added manually
.AddSingleton<IListTenantsEndpoint, ListTenantEndpoint>()
.AddCabazureClient(
"azure-rest-client",
j => JsonSerializerOptionsFactory.Configure(j),
clientOptions);
}
}
21 changes: 21 additions & 0 deletions samples/AzureRest/AzureRest.Contracts/Tenant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace AzureRest.Contracts
{
public class Tenant
{
public string CountryCode { get; set; }

public string DisplayName { get; set; }

public string DefaultDomain { get; set; }

public string[] Domains { get; set; }

public string Id { get; set; }

public string TenantCategory { get; set; }

public string TenantId { get; set; }

public string TenantType { get; set; }
}
}
2 changes: 1 addition & 1 deletion samples/AzureRest/AzureRest.TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

var app = builder.Build();

var endpoint = app.Services.GetRequiredService<IListSubscriptions>();
var endpoint = app.Services.GetRequiredService<IListSubscriptionsEndpoint>();

var result = await endpoint.ExecuteAsync();
if (result.OkContent is not { } list)
Expand Down