본문으로 건너뛰기

C#

Platform API 토큰을 만들고 platform.proto를 받으려면 gRPC 가이드부터 시작하세요. 아래 명령은 해당 파일이 있는 디렉터리에서 실행하세요.

.NET 콘솔 프로젝트를 만드세요:

dotnet new console -n ReconstructExample

프로젝트 디렉터리로 이동하세요:

cd ReconstructExample

gRPC 클라이언트 패키지를 추가하세요:

dotnet add package Grpc.Net.Client
dotnet add package Grpc.Tools
dotnet add package Google.Protobuf

다운로드한 platform.proto를 ReconstructExample 디렉터리에 복사하세요.

프로젝트 .csproj 파일에 다음 항목을 추가하세요. 빌드할 때 Grpc.Tools가 클라이언트를 생성합니다:

<ItemGroup>
<Protobuf Include="platform.proto" GrpcServices="Client" />
</ItemGroup>

생성된 형식을 Program.cs에서 사용하세요:

using Cr.Reconstruct.External.Grpc;
using Grpc.Core;
using Grpc.Net.Client;

var token = Environment.GetEnvironmentVariable("CR_RECONSTRUCT_API_TOKEN")
?? throw new InvalidOperationException("Set CR_RECONSTRUCT_API_TOKEN.");
var address = Environment.GetEnvironmentVariable("CR_RECONSTRUCT_GRPC_URL")
?? "https://reconstruct.dev";

using var channel = GrpcChannel.ForAddress(address);
var client = new PlatformService.PlatformServiceClient(channel);
var headers = new Metadata { { "authorization", $"Bearer {token}" } };
var page = await client.ListCatalogsAsync(
new ListCatalogsRequest { PageSize = 10 }, headers, deadline: DateTime.UtcNow.AddSeconds(10));

foreach (var catalog in page.Catalogs)
{
var detail = await client.GetCatalogAsync(
new GetCatalogRequest { CatalogId = catalog.CatalogId }, headers);
Console.WriteLine($"{detail.CatalogId}: {detail.Description}");
}

dotnet run을 실행하세요. 네이티브 gRPC 채널은 HTTPS authority를 사용하며 REST 경로 /api/v1/platform은 포함하지 않습니다.