From a734f474047eba4f60adab10843d190f501fec4f Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Wed, 15 Jan 2025 15:05:55 -0600 Subject: [PATCH 1/3] docs: update README with client batch check info --- README.md | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9568404..0f81aae 100644 --- a/README.md +++ b/README.md @@ -719,8 +719,9 @@ response = await fga_client.check(body, options) ##### Batch Check -Run a set of [checks](#check). Batch Check will return `allowed: false` if it encounters an error, and will return the error in the body. -If 429s or 5xxs are encountered, the underlying check will retry up to 3 times before giving up. +Similar to [check](#check), but instead of checking a single user-object relationship, accepts a list of relationships to check. Requires OpenFGA version 1.8.0 or greater. + +[API Documentation](https://openfga.dev/api/service#/Relationship%20Queries/BatchCheck) ```python # from openfga_sdk import OpenFgaClient @@ -824,6 +825,82 @@ response = await fga_client.batch_check(ClientBatchCheckRequest(checks=checks), # ] ``` +If you are using an OpenFGA version less than 1.8.0, you can use the `clientBatchCheck` function, +which calls `check` in parallel. It will return `allowed: false` if it encounters an error, and will return the error in the body. +If 429s or 5xxs are encountered, the underlying check will retry up to 3 times before giving up. + +```python +# from openfga_sdk import OpenFgaClient +# from openfga_sdk.client import ClientCheckRequest +# from openfga_sdk.client.models import ClientTuple +# Initialize the fga_client +# fga_client = OpenFgaClient(configuration) + +options = { + # You can rely on the model id set in the configuration or override it for this specific request + "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1" +} +body = [ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="viewer", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +@@ -748,7 +750,7 @@ + context=dict( + ViewCount=100 + ) +), ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="admin", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +@@ -759,20 +761,21 @@ + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", + ), + ] +), ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="creator", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +), ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="deleter", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +)] + +response = await fga_client.batch_check(body, options) +# response.responses = [{ +# allowed: false, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "viewer", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +@@ -787,7 +790,8 @@ +# } +# }, { +# allowed: false, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "admin", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +@@ -799,35 +803,40 @@ +# } +# }, { +# allowed: false, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "creator", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +# }, +# error: +# }, { +# allowed: true, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "deleter", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +# }}, +# ] +``` + #### Expand Expands the relationships in userset tree format. From eaa826d393730592c7732aabbbbbca863e1a82a1 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Wed, 15 Jan 2025 15:09:58 -0600 Subject: [PATCH 2/3] fix client_batch_check reference --- README.md | 74 +------------------------------------------------------ 1 file changed, 1 insertion(+), 73 deletions(-) diff --git a/README.md b/README.md index 0f81aae..d97f3f9 100644 --- a/README.md +++ b/README.md @@ -825,82 +825,10 @@ response = await fga_client.batch_check(ClientBatchCheckRequest(checks=checks), # ] ``` -If you are using an OpenFGA version less than 1.8.0, you can use the `clientBatchCheck` function, +If you are using an OpenFGA version less than 1.8.0, you can use `client_batch_check`, which calls `check` in parallel. It will return `allowed: false` if it encounters an error, and will return the error in the body. If 429s or 5xxs are encountered, the underlying check will retry up to 3 times before giving up. -```python -# from openfga_sdk import OpenFgaClient -# from openfga_sdk.client import ClientCheckRequest -# from openfga_sdk.client.models import ClientTuple -# Initialize the fga_client -# fga_client = OpenFgaClient(configuration) - -options = { - # You can rely on the model id set in the configuration or override it for this specific request - "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1" -} -body = [ClientCheckRequest( - user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", - relation="viewer", - object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -@@ -748,7 +750,7 @@ - context=dict( - ViewCount=100 - ) -), ClientCheckRequest( - user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", - relation="admin", - object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -@@ -759,20 +761,21 @@ - object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", - ), - ] -), ClientCheckRequest( - user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", - relation="creator", - object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -), ClientCheckRequest( - user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", - relation="deleter", - object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -)] - -response = await fga_client.batch_check(body, options) -# response.responses = [{ -# allowed: false, -# request: { -# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", -# relation: "viewer", -# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -@@ -787,7 +790,8 @@ -# } -# }, { -# allowed: false, -# request: { -# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", -# relation: "admin", -# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -@@ -799,35 +803,40 @@ -# } -# }, { -# allowed: false, -# request: { -# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", -# relation: "creator", -# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -# }, -# error: -# }, { -# allowed: true, -# request: { -# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", -# relation: "deleter", -# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", -# }}, -# ] -``` - #### Expand Expands the relationships in userset tree format. From a9e55e8056397c1c17b97397b42ed1e9726fff9d Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Wed, 15 Jan 2025 17:05:36 -0600 Subject: [PATCH 3/3] add back example client_batch_check --- README.md | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/README.md b/README.md index d97f3f9..7fa03af 100644 --- a/README.md +++ b/README.md @@ -829,6 +829,99 @@ If you are using an OpenFGA version less than 1.8.0, you can use `client_batch_c which calls `check` in parallel. It will return `allowed: false` if it encounters an error, and will return the error in the body. If 429s or 5xxs are encountered, the underlying check will retry up to 3 times before giving up. +```python +# from openfga_sdk import OpenFgaClient +# from openfga_sdk.client import ClientCheckRequest +# from openfga_sdk.client.models import ClientTuple +# Initialize the fga_client +# fga_client = OpenFgaClient(configuration) + +options = { + # You can rely on the model id set in the configuration or override it for this specific request + "authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1" +} +body = [ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="viewer", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", + contextual_tuples=[ # optional + ClientTuple( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="editor", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", + ), + ], + context=dict( + ViewCount=100 + ) +), ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="admin", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", + contextual_tuples=[ # optional + ClientTuple( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="editor", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", + ), + ] +), ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="creator", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +), ClientCheckRequest( + user="user:81684243-9356-4421-8fbf-a4f8d36aa31b", + relation="deleter", + object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +)] + +response = await fga_client.client_batch_check(body, options) +# response.responses = [{ +# allowed: false, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "viewer", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +# contextual_tuples: [{ +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "editor", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a" +# }], +# context=dict( +# ViewCount=100 +# ) +# } +# }, { +# allowed: false, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "admin", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +# contextual_tuples: [{ +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "editor", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a" +# }] +# } +# }, { +# allowed: false, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "creator", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +# }, +# error: +# }, { +# allowed: true, +# request: { +# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b", +# relation: "deleter", +# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a", +# }}, +# ] +``` + + #### Expand Expands the relationships in userset tree format.