-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgram.cs
More file actions
552 lines (472 loc) · 25 KB
/
Program.cs
File metadata and controls
552 lines (472 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using OpenFga.Sdk.ApiClient;
using OpenFga.Sdk.Client;
using OpenFga.Sdk.Client.Model;
using OpenFga.Sdk.Configuration;
using OpenFga.Sdk.Model;
using System.Net.Http;
namespace ApiExecutorExample;
/// <summary>
/// This example demonstrates how to use ApiExecutor to make custom HTTP requests
/// to the OpenFGA API with full response details (status code, headers, raw response, typed data).
///
/// Prerequisites: Run an OpenFGA server on localhost:8080
/// docker run -p 8080:8080 openfga/openfga:latest run
/// </summary>
class Program {
static async Task Main(string[] args) {
Console.WriteLine("=== OpenFGA Custom API Requests Example ===\n");
// Configure client to connect to local OpenFGA instance
var config = new ClientConfiguration {
ApiUrl = "http://localhost:8080"
};
using var client = new OpenFgaClient(config);
var executor = client.ApiExecutor;
try {
// Example 1: List stores using raw GET request
await ListStoresExample(executor, config.ApiUrl);
// Example 2: Create a store using raw POST request with typed response
var storeId = await CreateStoreExample(executor, config.ApiUrl);
// Example 3: Get store details using path parameters
await GetStoreExample(executor, config.ApiUrl, storeId);
// Example 4: Create an authorization model
var modelId = await CreateAuthorizationModelExample(executor, config.ApiUrl, storeId);
// Example 5: Write relationship tuples
await WriteTuplesExample(executor, config.ApiUrl, storeId);
// Example 6: Read relationship tuples
await ReadTuplesExample(executor, config.ApiUrl, storeId);
// Example 7: Check permissions
await CheckPermissionExample(executor, config.ApiUrl, storeId, modelId);
// Example 8: Use raw JSON response instead of typed
await RawJsonResponseExample(executor, config.ApiUrl);
// Example 9: Custom headers
await CustomHeadersExample(executor, config.ApiUrl);
// Example 10: Fluent API for building requests
await FluentApiExample(executor, config.ApiUrl);
// Example 11: Streaming API via ExecuteStreamingAsync
await StreamingExample(executor, config.ApiUrl);
// Cleanup: Delete the store we created
await DeleteStoreExample(executor, config.ApiUrl, storeId);
Console.WriteLine("\n=== All examples completed successfully! ===");
} catch (Exception ex) {
Console.WriteLine($"\nError: {ex.Message}");
Console.WriteLine("\nMake sure OpenFGA is running on localhost:8080");
Console.WriteLine("Run: docker run -p 8080:8080 openfga/openfga:latest run");
Environment.Exit(1);
}
}
static async Task ListStoresExample(ApiExecutor executor, string basePath) {
Console.WriteLine("Example 1: List Stores");
Console.WriteLine("Making GET request to /stores");
var request = new RequestBuilder<Any> {
Method = HttpMethod.Get,
BasePath = basePath,
PathTemplate = "/stores",
PathParameters = new Dictionary<string, string>(),
QueryParameters = new Dictionary<string, string>()
};
var response = await executor.ExecuteAsync<Any, ListStoresResponse>(request, "ListStores");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Is Successful: {response.IsSuccessful}");
Console.WriteLine($" Found {response.Data.Stores?.Count ?? 0} store(s)");
Console.WriteLine();
}
static async Task<string> CreateStoreExample(ApiExecutor executor, string basePath) {
Console.WriteLine("Example 2: Create Store");
Console.WriteLine("Making POST request to /stores");
var storeName = "ApiExecutor-Example-" + DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var requestBody = new Dictionary<string, object> {
{ "name", storeName }
};
var request = new RequestBuilder<object> {
Method = HttpMethod.Post,
BasePath = basePath,
PathTemplate = "/stores",
PathParameters = new Dictionary<string, string>(),
QueryParameters = new Dictionary<string, string>(),
Body = requestBody
};
var response = await executor.ExecuteAsync<object, CreateStoreResponse>(request, "CreateStore");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Store ID: {response.Data.Id}");
Console.WriteLine($" Store Name: {response.Data.Name}");
Console.WriteLine($" Raw Response Length: {response.RawResponse.Length} chars");
Console.WriteLine();
return response.Data.Id!;
}
static async Task GetStoreExample(ApiExecutor executor, string basePath, string storeId) {
Console.WriteLine("Example 3: Get Store Details");
Console.WriteLine($"Making GET request to /stores/{{store_id}}");
var request = new RequestBuilder<Any> {
Method = HttpMethod.Get,
BasePath = basePath,
PathTemplate = "/stores/{store_id}",
PathParameters = new Dictionary<string, string> { { "store_id", storeId } },
QueryParameters = new Dictionary<string, string>()
};
var response = await executor.ExecuteAsync<Any, GetStoreResponse>(request, "GetStore");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Store Name: {response.Data.Name}");
Console.WriteLine($" Created At: {response.Data.CreatedAt}");
Console.WriteLine($" Response Headers: {response.Headers.Count}");
Console.WriteLine();
}
static async Task<string> CreateAuthorizationModelExample(ApiExecutor executor, string basePath, string storeId) {
Console.WriteLine("Example 4: Create Authorization Model");
Console.WriteLine("Making POST request to /stores/{store_id}/authorization-models");
var requestBody = new Dictionary<string, object> {
{ "schema_version", "1.1" },
{
"type_definitions", new List<Dictionary<string, object>> {
new() {
{ "type", "user" },
{ "relations", new Dictionary<string, object>() }
},
new() {
{ "type", "document" },
{
"relations", new Dictionary<string, object> {
{
"reader", new Dictionary<string, object> {
{ "this", new Dictionary<string, object>() }
}
},
{
"writer", new Dictionary<string, object> {
{ "this", new Dictionary<string, object>() }
}
}
}
},
{
"metadata", new Dictionary<string, object> {
{
"relations", new Dictionary<string, object> {
{
"reader", new Dictionary<string, object> {
{
"directly_related_user_types", new List<Dictionary<string, string>> {
new() { { "type", "user" } }
}
}
}
},
{
"writer", new Dictionary<string, object> {
{
"directly_related_user_types", new List<Dictionary<string, string>> {
new() { { "type", "user" } }
}
}
}
}
}
}
}
}
}
}
}
};
var request = new RequestBuilder<object> {
Method = HttpMethod.Post,
BasePath = basePath,
PathTemplate = "/stores/{store_id}/authorization-models",
PathParameters = new Dictionary<string, string> { { "store_id", storeId } },
QueryParameters = new Dictionary<string, string>(),
Body = requestBody
};
var response = await executor.ExecuteAsync<object, WriteAuthorizationModelResponse>(request, "WriteAuthorizationModel");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Model ID: {response.Data.AuthorizationModelId}");
Console.WriteLine();
return response.Data.AuthorizationModelId!;
}
static async Task WriteTuplesExample(ApiExecutor executor, string basePath, string storeId) {
Console.WriteLine("Example 5: Write Relationship Tuples");
Console.WriteLine("Making POST request to /stores/{store_id}/write");
var requestBody = new Dictionary<string, object> {
{
"writes", new Dictionary<string, object> {
{
"tuple_keys", new List<Dictionary<string, string>> {
new() {
{ "user", "user:alice" },
{ "relation", "writer" },
{ "object", "document:roadmap" }
},
new() {
{ "user", "user:bob" },
{ "relation", "reader" },
{ "object", "document:roadmap" }
}
}
}
}
}
};
var request = new RequestBuilder<object> {
Method = HttpMethod.Post,
BasePath = basePath,
PathTemplate = "/stores/{store_id}/write",
PathParameters = new Dictionary<string, string> { { "store_id", storeId } },
QueryParameters = new Dictionary<string, string>(),
Body = requestBody
};
var response = await executor.ExecuteAsync<object, object>(request, "Write");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine(" Tuples written successfully");
Console.WriteLine();
}
static async Task ReadTuplesExample(ApiExecutor executor, string basePath, string storeId) {
Console.WriteLine("Example 6: Read Relationship Tuples");
Console.WriteLine("Making POST request to /stores/{store_id}/read");
var requestBody = new Dictionary<string, object> {
{
"tuple_key", new Dictionary<string, string> {
{ "object", "document:roadmap" }
}
}
};
var request = new RequestBuilder<object> {
Method = HttpMethod.Post,
BasePath = basePath,
PathTemplate = "/stores/{store_id}/read",
PathParameters = new Dictionary<string, string> { { "store_id", storeId } },
QueryParameters = new Dictionary<string, string>(),
Body = requestBody
};
var response = await executor.ExecuteAsync<object, ReadResponse>(request, "Read");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Found {response.Data.Tuples?.Count ?? 0} tuple(s):");
if (response.Data.Tuples != null) {
foreach (var tuple in response.Data.Tuples) {
Console.WriteLine($" - {tuple.Key.User} is {tuple.Key.Relation} of {tuple.Key.Object}");
}
}
Console.WriteLine();
}
static async Task CheckPermissionExample(ApiExecutor executor, string basePath, string storeId, string modelId) {
Console.WriteLine("Example 7: Check Permission");
Console.WriteLine("Making POST request to /stores/{store_id}/check");
var requestBody = new Dictionary<string, object> {
{ "authorization_model_id", modelId },
{
"tuple_key", new Dictionary<string, string> {
{ "user", "user:alice" },
{ "relation", "writer" },
{ "object", "document:roadmap" }
}
}
};
var request = new RequestBuilder<object> {
Method = HttpMethod.Post,
BasePath = basePath,
PathTemplate = "/stores/{store_id}/check",
PathParameters = new Dictionary<string, string> { { "store_id", storeId } },
QueryParameters = new Dictionary<string, string>(),
Body = requestBody
};
var response = await executor.ExecuteAsync<object, CheckResponse>(request, "Check");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Allowed: {response.Data.Allowed}");
Console.WriteLine();
}
static async Task RawJsonResponseExample(ApiExecutor executor, string basePath) {
Console.WriteLine("Example 8: Raw JSON Response");
Console.WriteLine("Getting response as raw JSON string instead of typed object");
var request = new RequestBuilder<Any> {
Method = HttpMethod.Get,
BasePath = basePath,
PathTemplate = "/stores",
PathParameters = new Dictionary<string, string>(),
QueryParameters = new Dictionary<string, string> { { "page_size", "5" } }
};
var response = await executor.ExecuteAsync(request, "ListStores");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Raw JSON (first 100 chars): {response.Data?.Substring(0, Math.Min(100, response.Data.Length))}...");
Console.WriteLine($" RawResponse and Data are the same: {response.RawResponse == response.Data}");
Console.WriteLine();
}
static async Task CustomHeadersExample(ApiExecutor executor, string basePath) {
Console.WriteLine("Example 9: Custom Headers");
Console.WriteLine("Making request with custom headers");
var request = new RequestBuilder<Any> {
Method = HttpMethod.Get,
BasePath = basePath,
PathTemplate = "/stores",
PathParameters = new Dictionary<string, string>(),
QueryParameters = new Dictionary<string, string>()
};
var options = new ClientRequestOptions {
Headers = new Dictionary<string, string> {
{ "X-Custom-Header", "example-value" },
{ "X-Request-ID", Guid.NewGuid().ToString() }
}
};
var response = await executor.ExecuteAsync<Any, ListStoresResponse>(request, "ListStores", options);
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine(" Custom headers sent successfully");
Console.WriteLine($" Response has {response.Headers.Count} headers");
Console.WriteLine();
}
static async Task FluentApiExample(ApiExecutor executor, string basePath) {
Console.WriteLine("Example 10: Fluent API for Request Building");
Console.WriteLine("Using RequestBuilder with fluent methods");
var request = RequestBuilder<Any>
.Create(HttpMethod.Get, basePath, "/stores")
.WithQueryParameter("page_size", "10")
.WithQueryParameter("continuation_token", "");
var response = await executor.ExecuteAsync<Any, ListStoresResponse>(request, "ListStores");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine($" Found {response.Data.Stores?.Count ?? 0} store(s)");
Console.WriteLine();
}
static async Task StreamingExample(ApiExecutor executor, string basePath) {
Console.WriteLine("Example 11: Streaming API");
Console.WriteLine("Streaming list-objects for a computed relation via ExecuteStreamingAsync");
// Create a dedicated store for this streaming demo
var storeResponse = await executor.ExecuteAsync<object, CreateStoreResponse>(
RequestBuilder<object>
.Create(HttpMethod.Post, basePath, "/stores")
.WithBody(new Dictionary<string, object> { { "name", "streaming-demo" } }),
"CreateStore");
var streamStoreId = storeResponse.Data.Id!;
Console.WriteLine($" Created store: {streamStoreId}");
// Write an authorization model with owner, viewer, and a computed can_read relation
var modelResponse = await executor.ExecuteAsync<object, WriteAuthorizationModelResponse>(
RequestBuilder<object>
.Create(HttpMethod.Post, basePath, "/stores/{store_id}/authorization-models")
.WithPathParameter("store_id", streamStoreId)
.WithBody(new Dictionary<string, object> {
{ "schema_version", "1.1" },
{
"type_definitions", new List<Dictionary<string, object>> {
new() {
{ "type", "user" },
{ "relations", new Dictionary<string, object>() }
},
new() {
{ "type", "document" },
{
"relations", new Dictionary<string, object> {
{ "owner", new Dictionary<string, object> { { "this", new Dictionary<string, object>() } } },
{ "viewer", new Dictionary<string, object> { { "this", new Dictionary<string, object>() } } },
{
"can_read", new Dictionary<string, object> {
{
"union", new Dictionary<string, object> {
{
"child", new List<Dictionary<string, object>> {
new() { { "computedUserset", new Dictionary<string, object> { { "relation", "owner" } } } },
new() { { "computedUserset", new Dictionary<string, object> { { "relation", "viewer" } } } }
}
}
}
}
}
}
}
},
{
"metadata", new Dictionary<string, object> {
{
"relations", new Dictionary<string, object> {
{ "owner", new Dictionary<string, object> { { "directly_related_user_types", new List<Dictionary<string, string>> { new() { { "type", "user" } } } } } },
{ "viewer", new Dictionary<string, object> { { "directly_related_user_types", new List<Dictionary<string, string>> { new() { { "type", "user" } } } } } },
{ "can_read", new Dictionary<string, object> { { "directly_related_user_types", new List<Dictionary<string, string>>() } } }
}
}
}
}
}
}
}
}),
"WriteAuthorizationModel");
var streamModelId = modelResponse.Data.AuthorizationModelId!;
Console.WriteLine($" Created model: {streamModelId}");
// Write 1000 documents where anne is the owner and 1000 where she is a viewer (batches of 100)
Console.WriteLine(" Writing tuples (1000 as owner, 1000 as viewer)...");
const int batchSize = 100;
var totalWritten = 0;
for (var batch = 0; batch < 10; batch++) {
var tuples = new List<Dictionary<string, string>>();
for (var i = 1; i <= batchSize; i++) {
tuples.Add(new Dictionary<string, string> {
{ "user", "user:anne" }, { "relation", "owner" }, { "object", $"document:{batch * batchSize + i}" }
});
}
await executor.ExecuteAsync<object, object>(
RequestBuilder<object>
.Create(HttpMethod.Post, basePath, "/stores/{store_id}/write")
.WithPathParameter("store_id", streamStoreId)
.WithBody(new Dictionary<string, object> {
{ "writes", new Dictionary<string, object> { { "tuple_keys", tuples } } },
{ "authorization_model_id", streamModelId }
}),
"Write");
totalWritten += tuples.Count;
}
for (var batch = 0; batch < 10; batch++) {
var tuples = new List<Dictionary<string, string>>();
for (var i = 1; i <= batchSize; i++) {
tuples.Add(new Dictionary<string, string> {
{ "user", "user:anne" }, { "relation", "viewer" }, { "object", $"document:{1000 + batch * batchSize + i}" }
});
}
await executor.ExecuteAsync<object, object>(
RequestBuilder<object>
.Create(HttpMethod.Post, basePath, "/stores/{store_id}/write")
.WithPathParameter("store_id", streamStoreId)
.WithBody(new Dictionary<string, object> {
{ "writes", new Dictionary<string, object> { { "tuple_keys", tuples } } },
{ "authorization_model_id", streamModelId }
}),
"Write");
totalWritten += tuples.Count;
}
Console.WriteLine($" Wrote {totalWritten} tuples");
// Stream objects via the computed can_read relation using ExecuteStreamingAsync
Console.WriteLine(" Streaming objects via computed 'can_read' relation...");
var count = 0;
var streamRequest = RequestBuilder<object>
.Create(HttpMethod.Post, basePath, "/stores/{store_id}/streamed-list-objects")
.WithPathParameter("store_id", streamStoreId)
.WithBody(new Dictionary<string, object> {
{ "user", "user:anne" },
{ "relation", "can_read" },
{ "type", "document" },
{ "authorization_model_id", streamModelId }
});
await foreach (var item in executor.ExecuteStreamingAsync<object, StreamedListObjectsResponse>(streamRequest, "StreamedListObjects")) {
count++;
if (count <= 3 || count % 500 == 0) {
Console.WriteLine($" - {item.Object}");
}
}
Console.WriteLine($"Streamed {count} objects");
// Clean up the streaming demo store
await executor.ExecuteAsync<Any, object>(
RequestBuilder<Any>
.Create(HttpMethod.Delete, basePath, "/stores/{store_id}")
.WithPathParameter("store_id", streamStoreId),
"DeleteStore");
Console.WriteLine(" Streaming demo store deleted");
Console.WriteLine();
}
static async Task DeleteStoreExample(ApiExecutor executor, string basePath, string storeId) {
Console.WriteLine("Cleanup: Delete Store");
Console.WriteLine($"Making DELETE request to /stores/{{store_id}}");
var request = new RequestBuilder<Any> {
Method = HttpMethod.Delete,
BasePath = basePath,
PathTemplate = "/stores/{store_id}",
PathParameters = new Dictionary<string, string> { { "store_id", storeId } },
QueryParameters = new Dictionary<string, string>()
};
var response = await executor.ExecuteAsync<Any, object>(request, "DeleteStore");
Console.WriteLine($"Status: {response.StatusCode}");
Console.WriteLine(" Store deleted successfully");
Console.WriteLine();
}
}