Skip to content

Commit

Permalink
Merge pull request #11 from Cabazure/azure-sample
Browse files Browse the repository at this point in the history
Rewrite sample to use Azure Rest API and netstandard2.0
  • Loading branch information
rickykaare authored Aug 8, 2024
2 parents ee2af5f + 173d0a1 commit 8efc2d2
Show file tree
Hide file tree
Showing 101 changed files with 2,123 additions and 1,902 deletions.
42 changes: 21 additions & 21 deletions Cabazure.Client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9FD398E9-51D
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{115ACFA6-5271-4A7B-844D-FFEA0A4F6BB7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Graph.Client", "samples\Microsoft.Graph\Microsoft.Graph.Client\Microsoft.Graph.Client.csproj", "{9D7D550E-15AA-4F2D-869D-53450E345FE7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{3932F1A3-4272-4E08-83F6-E1EB7DCED0FC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Graph", "Microsoft.Graph", "{3DF0DD77-F029-418F-96B9-5E977F11E8C2}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AzureRest", "AzureRest", "{D2D03799-0BB4-45C1-9D35-60F364C154EE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureRest.Client", "samples\AzureRest\AzureRest.Client\AzureRest.Client.csproj", "{469837C7-D9B8-4867-A1CD-747DBA16213B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Graph.TestApp", "samples\Microsoft.Graph\Microsoft.Graph.TestApp\Microsoft.Graph.TestApp.csproj", "{84D328C1-9835-4CDC-865F-8A1FD1D65489}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureRest.Contracts", "samples\AzureRest\AzureRest.Contracts\AzureRest.Contracts.csproj", "{C19CB361-795D-49FF-8821-E4C739EE5020}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Graph.Contracts", "samples\Microsoft.Graph\Microsoft.Graph.Contracts\Microsoft.Graph.Contracts.csproj", "{733A2A34-8B64-4A54-B996-EB84F07FE989}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzureRest.TestApp", "samples\AzureRest\AzureRest.TestApp\AzureRest.TestApp.csproj", "{CF5F841E-FC86-441D-8075-49EA5F280AB8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -53,18 +53,18 @@ Global
{A5A1DEAD-A6F6-47B5-B6A8-9ED08B5A0FD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5A1DEAD-A6F6-47B5-B6A8-9ED08B5A0FD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5A1DEAD-A6F6-47B5-B6A8-9ED08B5A0FD0}.Release|Any CPU.Build.0 = Release|Any CPU
{9D7D550E-15AA-4F2D-869D-53450E345FE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D7D550E-15AA-4F2D-869D-53450E345FE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D7D550E-15AA-4F2D-869D-53450E345FE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D7D550E-15AA-4F2D-869D-53450E345FE7}.Release|Any CPU.Build.0 = Release|Any CPU
{84D328C1-9835-4CDC-865F-8A1FD1D65489}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84D328C1-9835-4CDC-865F-8A1FD1D65489}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84D328C1-9835-4CDC-865F-8A1FD1D65489}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84D328C1-9835-4CDC-865F-8A1FD1D65489}.Release|Any CPU.Build.0 = Release|Any CPU
{733A2A34-8B64-4A54-B996-EB84F07FE989}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{733A2A34-8B64-4A54-B996-EB84F07FE989}.Debug|Any CPU.Build.0 = Debug|Any CPU
{733A2A34-8B64-4A54-B996-EB84F07FE989}.Release|Any CPU.ActiveCfg = Release|Any CPU
{733A2A34-8B64-4A54-B996-EB84F07FE989}.Release|Any CPU.Build.0 = Release|Any CPU
{469837C7-D9B8-4867-A1CD-747DBA16213B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{469837C7-D9B8-4867-A1CD-747DBA16213B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{469837C7-D9B8-4867-A1CD-747DBA16213B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{469837C7-D9B8-4867-A1CD-747DBA16213B}.Release|Any CPU.Build.0 = Release|Any CPU
{C19CB361-795D-49FF-8821-E4C739EE5020}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C19CB361-795D-49FF-8821-E4C739EE5020}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C19CB361-795D-49FF-8821-E4C739EE5020}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C19CB361-795D-49FF-8821-E4C739EE5020}.Release|Any CPU.Build.0 = Release|Any CPU
{CF5F841E-FC86-441D-8075-49EA5F280AB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF5F841E-FC86-441D-8075-49EA5F280AB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF5F841E-FC86-441D-8075-49EA5F280AB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF5F841E-FC86-441D-8075-49EA5F280AB8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -75,10 +75,10 @@ Global
{CED809C0-4423-4B2D-BAF8-0C942B52761B} = {115ACFA6-5271-4A7B-844D-FFEA0A4F6BB7}
{91529749-D468-43A2-8713-9428234FADFF} = {115ACFA6-5271-4A7B-844D-FFEA0A4F6BB7}
{A5A1DEAD-A6F6-47B5-B6A8-9ED08B5A0FD0} = {115ACFA6-5271-4A7B-844D-FFEA0A4F6BB7}
{9D7D550E-15AA-4F2D-869D-53450E345FE7} = {3DF0DD77-F029-418F-96B9-5E977F11E8C2}
{3DF0DD77-F029-418F-96B9-5E977F11E8C2} = {3932F1A3-4272-4E08-83F6-E1EB7DCED0FC}
{84D328C1-9835-4CDC-865F-8A1FD1D65489} = {3DF0DD77-F029-418F-96B9-5E977F11E8C2}
{733A2A34-8B64-4A54-B996-EB84F07FE989} = {3DF0DD77-F029-418F-96B9-5E977F11E8C2}
{D2D03799-0BB4-45C1-9D35-60F364C154EE} = {3932F1A3-4272-4E08-83F6-E1EB7DCED0FC}
{469837C7-D9B8-4867-A1CD-747DBA16213B} = {D2D03799-0BB4-45C1-9D35-60F364C154EE}
{C19CB361-795D-49FF-8821-E4C739EE5020} = {D2D03799-0BB4-45C1-9D35-60F364C154EE}
{CF5F841E-FC86-441D-8075-49EA5F280AB8} = {D2D03799-0BB4-45C1-9D35-60F364C154EE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3DD1DF42-FFAB-4F35-B264-B128D2778D40}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Cabazure.Client.Runtime\Cabazure.Client.Runtime.csproj" />
<ProjectReference Include="..\..\..\src\Cabazure.Client\Cabazure.Client.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Microsoft.Graph.Contracts\Microsoft.Graph.Contracts.csproj" />
<ProjectReference Include="..\AzureRest.Contracts\AzureRest.Contracts.csproj" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions samples/AzureRest/AzureRest.Client/AzureRestClientOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Azure.Core;
using Cabazure.Client;

namespace AzureRest.Client
{
public class AzureRestClientOptions : ICabazureAuthClientOptions
{
public TokenCredential? Credential { get; set; }

Uri ICabazureClientOptions.GetBaseAddress()
// Notice the trailing slash, it's important
// for the client to work correctly with relative paths.
=> new("https://management.azure.com/");

string ICabazureAuthClientOptions.GetScope()
// The default scope for Microsoft Graph.
=> "https://management.azure.com/.default";

TokenCredential ICabazureAuthClientOptions.GetCredential()
=> Credential
?? throw new InvalidOperationException(
"No Credential configured for GraphClientOptions");
}
}
16 changes: 16 additions & 0 deletions samples/AzureRest/AzureRest.Client/Endpoints/IGetSubscription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading;
using System.Threading.Tasks;
using AzureRest.Contracts;
using Cabazure.Client;

namespace AzureRest.Client.Endpoints
{
[ClientEndpoint("azure-rest-client")]
public interface IGetSubscription
{
[Get("subscriptions/{subscriptionId}?api-version=2022-12-01")]
public Task<EndpointResponse<Subscription>> ExecuteAsync(
[Path] string subscriptionId,
CancellationToken cancellationToken = default);
}
}
15 changes: 15 additions & 0 deletions samples/AzureRest/AzureRest.Client/Endpoints/IListSubscriptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading;
using System.Threading.Tasks;
using AzureRest.Contracts;
using Cabazure.Client;

namespace AzureRest.Client.Endpoints
{
[ClientEndpoint("azure-rest-client")]
public interface IListSubscriptions
{
[Get("https://management.azure.com/subscriptions?api-version=2022-12-01")]
public Task<EndpointResponse<ListResponse<Subscription>>> ExecuteAsync(
CancellationToken cancellationToken = default);
}
}
17 changes: 17 additions & 0 deletions samples/AzureRest/AzureRest.Client/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using AzureRest.Client;
using AzureRest.Contracts.Serialization;

namespace Microsoft.Extensions.DependencyInjection
{
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);
}
}
11 changes: 11 additions & 0 deletions samples/AzureRest/AzureRest.Contracts/AzureRest.Contracts.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions samples/AzureRest/AzureRest.Contracts/CountValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AzureRest.Contracts
{
public class CountValue
{
public string Type { get; set; }

public int Value { get; set; }
}
}
9 changes: 9 additions & 0 deletions samples/AzureRest/AzureRest.Contracts/ListResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AzureRest.Contracts
{
public class ListResponse<T>
{
public CountValue Count { get; set; }

public T[] Value { get; set; }
}
}
7 changes: 7 additions & 0 deletions samples/AzureRest/AzureRest.Contracts/ManagedByTenant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AzureRest.Contracts
{
public class ManagedByTenant
{
public string TenantId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace AzureRest.Contracts.Serialization
{
public static class JsonSerializerOptionsFactory
{
public static JsonSerializerOptions Create()
=> Configure(new JsonSerializerOptions());

public static JsonSerializerOptions Configure(
this JsonSerializerOptions options)
{
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.PropertyNameCaseInsensitive = true;
options.Converters.Add(
new JsonStringEnumConverter(
JsonNamingPolicy.CamelCase));

return options;
}
}
}
21 changes: 21 additions & 0 deletions samples/AzureRest/AzureRest.Contracts/Subscription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace AzureRest.Contracts
{
public class Subscription
{
public string AuthorizationSource { get; set; }

public string DisplayName { get; set; }

public string Id { get; set; }

public ManagedByTenant[] ManagedByTenants { get; set; }

public string State { get; set; }

public string SubscriptionId { get; set; }

public SubscriptionPolicies SubscriptionPolicies { get; set; }

public string TenantId { get; set; }
}
}
11 changes: 11 additions & 0 deletions samples/AzureRest/AzureRest.Contracts/SubscriptionPolicies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AzureRest.Contracts
{
public class SubscriptionPolicies
{
public string LocationPlacementId { get; set; }

public string QuotaId { get; set; }

public string SpendingLimit { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.Graph.Client\Microsoft.Graph.Client.csproj" />
<ProjectReference Include="..\AzureRest.Client\AzureRest.Client.csproj" />
</ItemGroup>



</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Azure.Identity;
using AzureRest.Client;
using Microsoft.Extensions.Options;

namespace AzureRest.TestApp.Options;

public class ConfigureAzureRestClientOptions
: IConfigureOptions<AzureRestClientOptions>
{
public void Configure(AzureRestClientOptions options)
{
options.Credential = new DefaultAzureCredential();
}
}
26 changes: 26 additions & 0 deletions samples/AzureRest/AzureRest.TestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using AzureRest.Client.Endpoints;
using AzureRest.TestApp.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.ConfigureOptions<ConfigureAzureRestClientOptions>();
builder.Services.AddAzureRestClient();

var app = builder.Build();

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

var result = await endpoint.ExecuteAsync();
if (result.OkContent is not { } list)
{
Console.WriteLine(result.StatusCode);
return;
}

Console.WriteLine($"Found {list.Count.Value} subscriptions ({list.Count.Type}):");
foreach (var subscription in list.Value)
{
Console.WriteLine($" - {subscription.DisplayName} ({subscription.SubscriptionId})");
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8efc2d2

Please sign in to comment.