3737from openfga_sdk .models .write_request import WriteRequest
3838
3939import asyncio
40+ import uuid
4041from typing import List
4142
43+ CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method"
44+ CLIENT_BULK_REQUEST_ID_HEADER = "X-OpenFGA-Client-Bulk-Request-Id"
45+
4246
4347def _chuck_array (array , max_size ):
4448 """
@@ -47,6 +51,21 @@ def _chuck_array(array, max_size):
4751 return [array [i * max_size :(i + 1 ) * max_size ] for i in range ((len (array ) + max_size - 1 ) // max_size )]
4852
4953
54+ def set_heading_if_not_set (options : dict [str , int | str ], name : str , value : str ):
55+ """
56+ Set heading to the value if it is not set
57+ """
58+ if options is None :
59+ options = {}
60+ headers = options .get ("headers" )
61+ if headers is None :
62+ headers = {}
63+ if headers .get (name ) is None :
64+ headers [name ] = value
65+ options ["headers" ] = headers
66+ return options
67+
68+
5069def options_to_kwargs (options : dict [str , int | str ]):
5170 """
5271 Return kargs with continuation_token and page_size
@@ -124,6 +143,7 @@ async def list_stores(self, options: dict[str, int | str]):
124143 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
125144 """
126145 # convert options to kargs
146+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "ListStores" )
127147 kwargs = options_to_kwargs (options )
128148 api_response = await self ._api .list_stores (
129149 ** kwargs ,
@@ -138,6 +158,7 @@ async def create_store(self, body: CreateStoreRequest, options: dict[str, int |
138158 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
139159 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
140160 """
161+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "CreateStore" )
141162 kwargs = options_to_kwargs (options )
142163 api_response = await self ._api .create_store (
143164 body ,
@@ -153,6 +174,7 @@ async def get_store(self, options: dict[str, int | str]):
153174 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
154175 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
155176 """
177+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "GetStore" )
156178 kwargs = options_to_kwargs (options )
157179 api_response = await self ._api .get_store (
158180 ** kwargs ,
@@ -167,6 +189,7 @@ async def delete_store(self, options: dict[str, int | str]):
167189 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
168190 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
169191 """
192+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "DeleteStore" )
170193 kwargs = options_to_kwargs (options )
171194 api_response = await self ._api .delete_store (
172195 ** kwargs ,
@@ -185,6 +208,7 @@ async def read_authorization_models(self, options: dict[str, int | str]):
185208 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
186209 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
187210 """
211+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "ReadAuthorizationModels" )
188212 kwargs = options_to_kwargs (options )
189213 api_response = await self ._api .read_authorization_models (
190214 ** kwargs ,
@@ -200,6 +224,7 @@ async def write_authorization_model(self, body: WriteAuthorizationModelRequest,
200224 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
201225 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
202226 """
227+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "WriteAuthorizationModel" )
203228 kwargs = options_to_kwargs (options )
204229 api_response = await self ._api .write_authorization_model (
205230 body ,
@@ -215,6 +240,7 @@ async def read_authorization_model(self, options: dict[str, int | str]):
215240 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
216241 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
217242 """
243+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "ReadAuthorizationModel" )
218244 kwargs = options_to_kwargs (options )
219245 authorization_model_id = self ._get_authorization_model_id (options )
220246 api_response = await self ._api .read_authorization_model (
@@ -231,6 +257,8 @@ async def read_latest_authorization_model(self, options: dict[str, int | str]):
231257 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
232258 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
233259 """
260+ options = set_heading_if_not_set (
261+ options , CLIENT_METHOD_HEADER , "ReadLatestAuthoriationModel" )
234262 options ["page_size" ] = 1
235263 api_response = await self .read_authorization_models (options )
236264 return api_response
@@ -250,6 +278,7 @@ async def read_changes(self, body: ReadChangesBody, options: dict[str, str]):
250278 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
251279 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
252280 """
281+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "ReadChanges" )
253282 kwargs = options_to_kwargs (options )
254283 kwargs ["type" ] = body .type
255284 api_response = await self ._api .read_changes (
@@ -268,6 +297,7 @@ async def read(self, body: TupleKey, options: dict[str, str]):
268297 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
269298 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
270299 """
300+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "Read" )
271301 page_size = None
272302 continuation_token = None
273303 if options :
@@ -358,11 +388,13 @@ async def writes(self, body: ClientWriteRequest, options: dict[str, str]):
358388 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
359389 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
360390 """
391+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "Writes" )
361392 transaction = options_to_transaction_info (options )
362393 if not transaction .disabled :
363394 results = await self ._write_with_transaction (body , options )
364395 return results
365396
397+ options = set_heading_if_not_set (options , CLIENT_BULK_REQUEST_ID_HEADER , str (uuid .uuid4 ()))
366398 # otherwise, it is not a transaction and it is a batch write requests
367399 writes_response = None
368400 if body .writes :
@@ -381,6 +413,7 @@ async def write_tuples(self, body: List[ClientTuple], options: dict[str, str]):
381413 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
382414 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
383415 """
416+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "WriteTuples" )
384417 result = await self .writes (ClientWriteRequest (body , None ), options )
385418 return result
386419
@@ -393,6 +426,7 @@ async def delete_tuples(self, body: List[ClientTuple], options: dict[str, str]):
393426 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
394427 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
395428 """
429+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "DeleteTuples" )
396430 result = await self .writes (ClientWriteRequest (None , body ), options )
397431 return result
398432
@@ -409,6 +443,8 @@ async def check(self, body: CheckRequestBody, options: dict[str, str]): # noqa:
409443 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
410444 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
411445 """
446+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "Check" )
447+
412448 kwargs = options_to_kwargs (options )
413449
414450 req_body = CheckRequest (
@@ -450,6 +486,9 @@ async def batch_check(self, body: List[CheckRequestBody], options: dict[str, str
450486 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
451487 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
452488 """
489+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "BatchCheck" )
490+ options = set_heading_if_not_set (options , CLIENT_BULK_REQUEST_ID_HEADER , str (uuid .uuid4 ()))
491+
453492 max_parallel_requests = 10
454493 if options is not None and "max_parallel_requests" in options :
455494 max_parallel_requests = options ["max_parallel_requests" ]
@@ -472,6 +511,7 @@ async def expand(self, body: ExpandRequestBody, options: dict[str, str]): # noq
472511 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
473512 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
474513 """
514+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "Expand" )
475515 kwargs = options_to_kwargs (options )
476516
477517 req_body = ExpandRequest (
@@ -497,6 +537,7 @@ async def list_objects(self, body: ListObjectsRequestBody, options: dict[str, st
497537 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
498538 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
499539 """
540+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "ListObjects" )
500541 kwargs = options_to_kwargs (options )
501542
502543 req_body = ListObjectsRequest (
@@ -525,6 +566,9 @@ async def list_relations(self, body: ListObjectsRequestBody, options: dict[str,
525566 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
526567 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
527568 """
569+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "ListRelations" )
570+ options = set_heading_if_not_set (options , CLIENT_BULK_REQUEST_ID_HEADER , str (uuid .uuid4 ()))
571+
528572 request_body = [construct_check_request_body (
529573 user = body .user , relation = i , object = body .object , contextual_tuples = body .contextual_tuples ) for i in body .relations ]
530574 result = await self .batch_check (request_body , options )
@@ -546,6 +590,8 @@ async def read_assertions(self, options: dict[str, str]): # noqa: E501
546590 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
547591 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
548592 """
593+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "ReadAssertions" )
594+
549595 kwargs = options_to_kwargs (options )
550596 authorization_model_id = self ._get_authorization_model_id (options )
551597 api_response = await self ._api .read_assertions (authorization_model_id , ** kwargs )
@@ -561,6 +607,7 @@ async def write_assertions(self, body: List[Assertion], options: dict[str, str])
561607 :param retryParams.maxRetry(options) - Override the max number of retries on each API request
562608 :param retryParams.minWaitInMs(options) - Override the minimum wait before a retry is initiated
563609 """
610+ options = set_heading_if_not_set (options , CLIENT_METHOD_HEADER , "WriteAssertions" )
564611 kwargs = options_to_kwargs (options )
565612 authorization_model_id = self ._get_authorization_model_id (options )
566613 api_response = await self ._api .write_assertions (authorization_model_id , WriteAssertionsRequest (body ), ** kwargs )
0 commit comments