Skip to content

Commit ab9d5e2

Browse files
authored
feat!: add support for BatchCheck API (#154)
2 parents 7af8409 + 36f5016 commit ab9d5e2

34 files changed

+3054
-108
lines changed

.openapi-generator/FILES

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ docs/Assertion.md
1919
docs/AssertionTupleKey.md
2020
docs/AuthErrorCode.md
2121
docs/AuthorizationModel.md
22+
docs/BatchCheckItem.md
23+
docs/BatchCheckRequest.md
24+
docs/BatchCheckResponse.md
25+
docs/BatchCheckSingleResult.md
26+
docs/CheckError.md
2227
docs/CheckRequest.md
2328
docs/CheckRequestTupleKey.md
2429
docs/CheckResponse.md
@@ -119,8 +124,12 @@ openfga_sdk/client/client.py
119124
openfga_sdk/client/configuration.py
120125
openfga_sdk/client/models/__init__.py
121126
openfga_sdk/client/models/assertion.py
127+
openfga_sdk/client/models/batch_check_item.py
128+
openfga_sdk/client/models/batch_check_request.py
122129
openfga_sdk/client/models/batch_check_response.py
130+
openfga_sdk/client/models/batch_check_single_response.py
123131
openfga_sdk/client/models/check_request.py
132+
openfga_sdk/client/models/client_batch_check_response.py
124133
openfga_sdk/client/models/expand_request.py
125134
openfga_sdk/client/models/list_objects_request.py
126135
openfga_sdk/client/models/list_relations_request.py
@@ -142,6 +151,11 @@ openfga_sdk/models/assertion.py
142151
openfga_sdk/models/assertion_tuple_key.py
143152
openfga_sdk/models/auth_error_code.py
144153
openfga_sdk/models/authorization_model.py
154+
openfga_sdk/models/batch_check_item.py
155+
openfga_sdk/models/batch_check_request.py
156+
openfga_sdk/models/batch_check_response.py
157+
openfga_sdk/models/batch_check_single_result.py
158+
openfga_sdk/models/check_error.py
145159
openfga_sdk/models/check_request.py
146160
openfga_sdk/models/check_request_tuple_key.py
147161
openfga_sdk/models/check_response.py

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
## [Unreleased](https://github.com/openfga/python-sdk/compare/v0.8.1...HEAD)
44

55
- feat: remove client-side validation - thanks @GMorris-professional (#155)
6-
- feat: add support for `start_time` parameter in `ReadChanges` endpoint (#156)
6+
- feat: add support for `start_time` parameter in `ReadChanges` endpoint (#156) - Note, this feature requires v1.8.0 of OpenFGA or newer
7+
- feat!: add support for `BatchCheck` API (#154) - Note, this feature requires v1.8.2 of OpenFGA or newer
78
- fix: change default max retry limit to 3 from 15 - thanks @ovindu-a (#155)
89

10+
BREAKING CHANGE:
11+
12+
Usage of the existing batch_check should now use client_batch_check instead, additionally the existing
13+
BatchCheckResponse has been renamed to ClientBatchCheckClientResponse.
14+
15+
Please see (#154)(https://github.com/openfga/python-sdk/pull/154) for more details on this change.
16+
917
## v0.8.1
1018

1119
### [0.8.1](https://github.com/openfga/python-sdk/compare/v0.8.0...v0.8.1) (2024-11-26)

README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -724,17 +724,19 @@ If 429s or 5xxs are encountered, the underlying check will retry up to 3 times b
724724

725725
```python
726726
# from openfga_sdk import OpenFgaClient
727-
# from openfga_sdk.client import ClientCheckRequest
728-
# from openfga_sdk.client.models import ClientTuple
729-
727+
# from openfga_sdk.client.models import (
728+
# ClientTuple,
729+
# ClientBatchCheckItem,
730+
# ClientBatchCheckRequest,
731+
# )
730732
# Initialize the fga_client
731733
# fga_client = OpenFgaClient(configuration)
732734

733735
options = {
734736
# You can rely on the model id set in the configuration or override it for this specific request
735737
"authorization_model_id": "01GXSA8YR785C4FYS3C0RTG7B1"
736738
}
737-
body = [ClientCheckRequest(
739+
checks = [ClientBatchCheckItem(
738740
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
739741
relation="viewer",
740742
object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
@@ -748,7 +750,7 @@ body = [ClientCheckRequest(
748750
context=dict(
749751
ViewCount=100
750752
)
751-
), ClientCheckRequest(
753+
), ClientBatchCheckItem(
752754
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
753755
relation="admin",
754756
object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
@@ -759,20 +761,21 @@ body = [ClientCheckRequest(
759761
object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
760762
),
761763
]
762-
), ClientCheckRequest(
764+
), ClientBatchCheckItem(
763765
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
764766
relation="creator",
765767
object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
766-
), ClientCheckRequest(
768+
), ClientBatchCheckItem(
767769
user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
768770
relation="deleter",
769771
object="document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
770772
)]
771773

772-
response = await fga_client.batch_check(body, options)
773-
# response.responses = [{
774+
response = await fga_client.batch_check(ClientBatchCheckRequest(checks=checks), options)
775+
# response.result = [{
774776
# allowed: false,
775-
# request: {
777+
# correlation_id: "de3630c2-f9be-4ee5-9441-cb1fbd82ce75",
778+
# tuple: {
776779
# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
777780
# relation: "viewer",
778781
# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
@@ -787,7 +790,8 @@ response = await fga_client.batch_check(body, options)
787790
# }
788791
# }, {
789792
# allowed: false,
790-
# request: {
793+
# correlation_id: "6d7c7129-9607-480e-bfd0-17c16e46b9ec",
794+
# tuple: {
791795
# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
792796
# relation: "admin",
793797
# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
@@ -799,14 +803,19 @@ response = await fga_client.batch_check(body, options)
799803
# }
800804
# }, {
801805
# allowed: false,
802-
# request: {
806+
# correlation_id: "210899b9-6bc3-4491-bdd1-d3d79780aa31",
807+
# tuple: {
803808
# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
804809
# relation: "creator",
805810
# object: "document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a",
806811
# },
807-
# error: <FgaError ...>
812+
# error: {
813+
# input_error: "validation_error",
814+
# message: "relation 'document#creator' not found"
815+
# }
808816
# }, {
809817
# allowed: true,
818+
# correlation_id: "55cc1946-9fc3-4710-bd40-8fe2687ed8da",
810819
# request: {
811820
# user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
812821
# relation: "deleter",
@@ -1043,6 +1052,7 @@ async def main():
10431052

10441053
Class | Method | HTTP request | Description
10451054
------------ | ------------- | ------------- | -------------
1055+
*OpenFgaApi* | [**batch_check**](https://github.com/openfga/python-sdk/blob/main/docs/OpenFgaApi.md#batch_check) | **POST** /stores/{store_id}/batch-check | Send a list of &#x60;check&#x60; operations in a single request
10461056
*OpenFgaApi* | [**check**](https://github.com/openfga/python-sdk/blob/main/docs/OpenFgaApi.md#check) | **POST** /stores/{store_id}/check | Check whether a user is authorized to access an object
10471057
*OpenFgaApi* | [**create_store**](https://github.com/openfga/python-sdk/blob/main/docs/OpenFgaApi.md#create_store) | **POST** /stores | Create a store
10481058
*OpenFgaApi* | [**delete_store**](https://github.com/openfga/python-sdk/blob/main/docs/OpenFgaApi.md#delete_store) | **DELETE** /stores/{store_id} | Delete a store
@@ -1072,6 +1082,11 @@ Class | Method | HTTP request | Description
10721082
- [AssertionTupleKey](https://github.com/openfga/python-sdk/blob/main/docs/AssertionTupleKey.md)
10731083
- [AuthErrorCode](https://github.com/openfga/python-sdk/blob/main/docs/AuthErrorCode.md)
10741084
- [AuthorizationModel](https://github.com/openfga/python-sdk/blob/main/docs/AuthorizationModel.md)
1085+
- [BatchCheckItem](https://github.com/openfga/python-sdk/blob/main/docs/BatchCheckItem.md)
1086+
- [BatchCheckRequest](https://github.com/openfga/python-sdk/blob/main/docs/BatchCheckRequest.md)
1087+
- [BatchCheckResponse](https://github.com/openfga/python-sdk/blob/main/docs/BatchCheckResponse.md)
1088+
- [BatchCheckSingleResult](https://github.com/openfga/python-sdk/blob/main/docs/BatchCheckSingleResult.md)
1089+
- [CheckError](https://github.com/openfga/python-sdk/blob/main/docs/CheckError.md)
10751090
- [CheckRequest](https://github.com/openfga/python-sdk/blob/main/docs/CheckRequest.md)
10761091
- [CheckRequestTupleKey](https://github.com/openfga/python-sdk/blob/main/docs/CheckRequestTupleKey.md)
10771092
- [CheckResponse](https://github.com/openfga/python-sdk/blob/main/docs/CheckResponse.md)

docs/BatchCheckItem.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# BatchCheckItem
2+
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**tuple_key** | [**CheckRequestTupleKey**](CheckRequestTupleKey.md) | |
8+
**contextual_tuples** | [**ContextualTupleKeys**](ContextualTupleKeys.md) | | [optional]
9+
**context** | **object** | | [optional]
10+
**correlation_id** | **str** | correlation_id must be a string containing only letters, numbers, or hyphens, with length ≤ 36 characters. |
11+
12+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
13+
14+

docs/BatchCheckRequest.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# BatchCheckRequest
2+
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**checks** | [**list[BatchCheckItem]**](BatchCheckItem.md) | |
8+
**authorization_model_id** | **str** | | [optional]
9+
**consistency** | [**ConsistencyPreference**](ConsistencyPreference.md) | | [optional]
10+
11+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
12+
13+

docs/BatchCheckResponse.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BatchCheckResponse
2+
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**result** | [**dict[str, BatchCheckSingleResult]**](BatchCheckSingleResult.md) | map keys are the correlation_id values from the BatchCheckItems in the request | [optional]
8+
9+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
10+
11+

docs/BatchCheckSingleResult.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# BatchCheckSingleResult
2+
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**allowed** | **bool** | | [optional]
8+
**error** | [**CheckError**](CheckError.md) | | [optional]
9+
10+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
11+
12+

docs/CheckError.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# CheckError
2+
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**input_error** | [**ErrorCode**](ErrorCode.md) | | [optional]
8+
**internal_error** | [**InternalErrorCode**](InternalErrorCode.md) | | [optional]
9+
**message** | **str** | | [optional]
10+
11+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
12+
13+

docs/OpenFgaApi.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All URIs are relative to *api.fga.example*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
7+
[**batch_check**](OpenFgaApi.md#batch_check) | **POST** /stores/{store_id}/batch-check | Send a list of &#x60;check&#x60; operations in a single request
78
[**check**](OpenFgaApi.md#check) | **POST** /stores/{store_id}/check | Check whether a user is authorized to access an object
89
[**create_store**](OpenFgaApi.md#create_store) | **POST** /stores | Create a store
910
[**delete_store**](OpenFgaApi.md#delete_store) | **DELETE** /stores/{store_id} | Delete a store
@@ -22,6 +23,90 @@ Method | HTTP request | Description
2223
[**write_authorization_model**](OpenFgaApi.md#write_authorization_model) | **POST** /stores/{store_id}/authorization-models | Create a new authorization model
2324

2425

26+
# **batch_check**
27+
> BatchCheckResponse batch_check(body)
28+
29+
Send a list of `check` operations in a single request
30+
31+
The `BatchCheck` API functions nearly identically to `Check`, but instead of checking a single user-object relationship BatchCheck accepts a list of relationships to check and returns a map containing `BatchCheckItem` response for each check it received. An associated `correlation_id` is required for each check in the batch. This ID is used to correlate a check to the appropriate response. It is a string consisting of only alphanumeric characters or hyphens with a maximum length of 36 characters. This `correlation_id` is used to map the result of each check to the item which was checked, so it must be unique for each item in the batch. We recommend using a UUID or ULID as the `correlation_id`, but you can use whatever unique identifier you need as long as it matches this regex pattern: `^[\\w\\d-]{1,36}$` For more details on how `Check` functions, see the docs for `/check`. ### Examples #### A BatchCheckRequest ```json { \"checks\": [ { \"tuple_key\": { \"object\": \"document:2021-budget\" \"relation\": \"reader\", \"user\": \"user:anne\", }, \"contextual_tuples\": {...} \"context\": {} \"correlation_id\": \"01JA8PM3QM7VBPGB8KMPK8SBD5\" }, { \"tuple_key\": { \"object\": \"document:2021-budget\" \"relation\": \"reader\", \"user\": \"user:bob\", }, \"contextual_tuples\": {...} \"context\": {} \"correlation_id\": \"01JA8PMM6A90NV5ET0F28CYSZQ\" } ] } ``` Below is a possible response to the above request. Note that the result map's keys are the `correlation_id` values from the checked items in the request: ```json { \"result\": { \"01JA8PMM6A90NV5ET0F28CYSZQ\": { \"allowed\": false, \"error\": {\"message\": \"\"} }, \"01JA8PM3QM7VBPGB8KMPK8SBD5\": { \"allowed\": true, \"error\": {\"message\": \"\"} } } ```
32+
33+
### Example
34+
35+
```python
36+
import time
37+
import openfga_sdk
38+
from openfga_sdk.rest import ApiException
39+
from pprint import pprint
40+
# To configure the configuration
41+
# host is mandatory
42+
# api_scheme is optional and default to https
43+
# store_id is mandatory
44+
# See configuration.py for a list of all supported configuration parameters.
45+
configuration = openfga_sdk.Configuration(
46+
scheme = "https",
47+
api_host = "api.fga.example",
48+
store_id = 'YOUR_STORE_ID',
49+
)
50+
51+
52+
# When authenticating via the API TOKEN method
53+
credentials = Credentials(method='api_token', configuration=CredentialConfiguration(api_token='TOKEN1'))
54+
configuration = openfga_sdk.Configuration(
55+
scheme = "https",
56+
api_host = "api.fga.example",
57+
store_id = 'YOUR_STORE_ID',
58+
credentials = credentials
59+
)
60+
61+
# Enter a context with an instance of the API client
62+
async with openfga_sdk.ApiClient(configuration) as api_client:
63+
# Create an instance of the API class
64+
api_instance = openfga_sdk.OpenFgaApi(api_client)
65+
body = openfga_sdk.BatchCheckRequest() # BatchCheckRequest |
66+
67+
try:
68+
# Send a list of `check` operations in a single request
69+
api_response = await api_instance.api_instance.batch_check(body)
70+
pprint(api_response)
71+
except ApiException as e:
72+
print("Exception when calling OpenFgaApi->batch_check: %s\n" % e)
73+
await api_client.close()
74+
```
75+
76+
77+
### Parameters
78+
79+
Name | Type | Description | Notes
80+
------------- | ------------- | ------------- | -------------
81+
**body** | [**BatchCheckRequest**](BatchCheckRequest.md)| |
82+
83+
### Return type
84+
85+
[**BatchCheckResponse**](BatchCheckResponse.md)
86+
87+
### Authorization
88+
89+
No authorization required
90+
91+
### HTTP request headers
92+
93+
- **Content-Type**: application/json
94+
- **Accept**: application/json
95+
96+
### HTTP response details
97+
| Status code | Description | Response headers |
98+
|-------------|-------------|------------------|
99+
**200** | A successful response. | - |
100+
**400** | Request failed due to invalid input. | - |
101+
**401** | Not authenticated. | - |
102+
**403** | Forbidden. | - |
103+
**404** | Request failed due to incorrect path. | - |
104+
**409** | Request was aborted due a transaction conflict. | - |
105+
**422** | Request timed out due to excessive request throttling. | - |
106+
**500** | Request failed due to internal server error. | - |
107+
108+
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
109+
25110
# **check**
26111
> CheckResponse check(body)
27112

0 commit comments

Comments
 (0)