メインコンテンツまでスキップ

C#

REST ガイドに従って Platform API トークンを作成します。この例は HttpClient と組み込みの JSON API を使用し、生成された gRPC クライアントは不要です。

コンソールプロジェクトを作成します。

dotnet new console -n ReconstructRestExample

プロジェクトのディレクトリーに移動します。

cd ReconstructRestExample

次の Program.cs を使用します。

using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;

var token = Environment.GetEnvironmentVariable("CR_RECONSTRUCT_API_TOKEN")
?? throw new InvalidOperationException("Set CR_RECONSTRUCT_API_TOKEN.");
var baseUrl = Environment.GetEnvironmentVariable("CR_RECONSTRUCT_REST_URL")
?? "https://reconstruct.dev/api/v1/platform";

using var client = new HttpClient { BaseAddress = new Uri(baseUrl.TrimEnd('/') + "/") };
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

using var page = await client.GetFromJsonAsync<JsonDocument>("catalogs?pageSize=10")
?? throw new InvalidOperationException("The catalog response was empty.");

if (page.RootElement.TryGetProperty("catalogs", out var catalogs))
{
foreach (var catalog in catalogs.EnumerateArray())
{
var id = catalog.GetProperty("catalogId").GetString()!;
using var detail = await client.GetFromJsonAsync<JsonDocument>(
"catalogs/" + Uri.EscapeDataString(id))
?? throw new InvalidOperationException("The catalog detail was empty.");
Console.WriteLine($"{id}: {detail.RootElement.GetProperty("description").GetString()}");
}
}

dotnet run を実行します。一覧の応答に nextPageToken がある場合は、URL エンコードしたクエリパラメーター pageToken として渡し、続きを取得します。