Skip to content

Commit 8489a4a

Browse files
committed
chore: add constants file
1 parent 6f06c2f commit 8489a4a

14 files changed

Lines changed: 162 additions & 52 deletions

openfga_sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
1111
"""
1212

13-
__version__ = "0.9.7"
13+
from openfga_sdk.constants import SDK_VERSION as __version__
1414

1515
from openfga_sdk.api.open_fga_api import OpenFgaApi
1616
from openfga_sdk.api_client import ApiClient

openfga_sdk/api_client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
)
3838
from openfga_sdk.telemetry import Telemetry
3939
from openfga_sdk.telemetry.attributes import TelemetryAttribute, TelemetryAttributes
40-
41-
42-
DEFAULT_USER_AGENT = "openfga-sdk python/0.9.7"
40+
from openfga_sdk.constants import USER_AGENT as DEFAULT_USER_AGENT
41+
from openfga_sdk.constants import (
42+
MAX_BACKOFF_TIME_IN_SEC,
43+
RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC,
44+
)
4345

4446

4547
def random_time(loop_count, min_wait_in_ms) -> float:
@@ -258,7 +260,7 @@ async def __call_api(
258260
self.configuration.retry_params is not None
259261
and self.configuration.retry_params.max_wait_in_sec is not None
260262
)
261-
else 120
263+
else MAX_BACKOFF_TIME_IN_SEC
262264
)
263265
if _retry_params is not None:
264266
if _retry_params.max_retry is not None:
@@ -425,7 +427,7 @@ def _parse_retry_after_header(self, headers) -> int:
425427
except ApiException:
426428
wait_time_in_sec = int(retry_after_header)
427429

428-
if wait_time_in_sec > 1800 or wait_time_in_sec < 1:
430+
if wait_time_in_sec > RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC or wait_time_in_sec < 1:
429431
raise ValueError("Retry-After header is invalid")
430432

431433
return math.ceil(wait_time_in_sec)

openfga_sdk/client/client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@
7474
)
7575
from openfga_sdk.models.write_request import WriteRequest
7676
from openfga_sdk.validation import is_well_formed_ulid_string
77-
78-
79-
CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method"
80-
CLIENT_BULK_REQUEST_ID_HEADER = "X-OpenFGA-Client-Bulk-Request-Id"
77+
from openfga_sdk.constants import (
78+
CLIENT_METHOD_HEADER,
79+
CLIENT_BULK_REQUEST_ID_HEADER,
80+
CLIENT_MAX_METHOD_PARALLEL_REQUESTS,
81+
CLIENT_MAX_BATCH_SIZE,
82+
)
8183

8284

8385
def _chuck_array(array, max_size):
@@ -714,7 +716,7 @@ async def client_batch_check(
714716
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
715717
)
716718

717-
max_parallel_requests = 10
719+
max_parallel_requests = CLIENT_MAX_METHOD_PARALLEL_REQUESTS
718720
if options is not None and "max_parallel_requests" in options:
719721
if (
720722
isinstance(options["max_parallel_requests"], str)
@@ -773,7 +775,7 @@ async def batch_check(
773775
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
774776
)
775777

776-
max_parallel_requests = 10
778+
max_parallel_requests = CLIENT_MAX_METHOD_PARALLEL_REQUESTS
777779
if options is not None and "max_parallel_requests" in options:
778780
if (
779781
isinstance(options["max_parallel_requests"], str)
@@ -783,7 +785,7 @@ async def batch_check(
783785
elif isinstance(options["max_parallel_requests"], int):
784786
max_parallel_requests = options["max_parallel_requests"]
785787

786-
max_batch_size = 50
788+
max_batch_size = CLIENT_MAX_BATCH_SIZE
787789
if options is not None and "max_batch_size" in options:
788790
if (
789791
isinstance(options["max_batch_size"], str)

openfga_sdk/configuration.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
from openfga_sdk.telemetry.counters import TelemetryCounter
3030
from openfga_sdk.telemetry.histograms import TelemetryHistogram
3131
from openfga_sdk.validation import is_well_formed_ulid_string
32+
from openfga_sdk.constants import (
33+
DEFAULT_MAX_RETRY,
34+
DEFAULT_MIN_WAIT_IN_MS,
35+
MAX_BACKOFF_TIME_IN_SEC,
36+
RETRY_MAX_ALLOWED_NUMBER,
37+
SDK_VERSION,
38+
)
3239

3340

3441
class RetryParams:
@@ -44,7 +51,7 @@ class RetryParams:
4451
:param max_wait_in_sec: Maximum wait (in seconds) between retry
4552
"""
4653

47-
def __init__(self, max_retry=3, min_wait_in_ms=100, max_wait_in_sec=120):
54+
def __init__(self, max_retry=DEFAULT_MAX_RETRY, min_wait_in_ms=DEFAULT_MIN_WAIT_IN_MS, max_wait_in_sec=MAX_BACKOFF_TIME_IN_SEC):
4855
self._max_retry = max_retry
4956
self._min_wait_in_ms = min_wait_in_ms
5057
self._max_wait_in_sec = max_wait_in_sec
@@ -54,9 +61,9 @@ def max_retry(self):
5461
"""
5562
Return the maximum number of retry
5663
"""
57-
if self._max_retry > 15:
64+
if self._max_retry > RETRY_MAX_ALLOWED_NUMBER:
5865
raise FgaValidationException(
59-
"RetryParams.max_retry exceeds maximum allowed limit of 15"
66+
f"RetryParams.max_retry exceeds maximum allowed limit of {RETRY_MAX_ALLOWED_NUMBER}"
6067
)
6168

6269
return self._max_retry
@@ -71,9 +78,9 @@ def max_retry(self, value):
7178
"RetryParams.max_retry must be an integer greater than or equal to 0"
7279
)
7380

74-
if value > 15:
81+
if value > RETRY_MAX_ALLOWED_NUMBER:
7582
raise FgaValidationException(
76-
"RetryParams.max_retry exceeds maximum allowed limit of 15"
83+
f"RetryParams.max_retry exceeds maximum allowed limit of {RETRY_MAX_ALLOWED_NUMBER}"
7784
)
7885

7986
self._max_retry = value
@@ -197,7 +204,7 @@ def __init__(
197204
| dict[TelemetryAttribute | str, bool]
198205
| None,
199206
]
200-
| None,
207+
| None
201208
]
202209
| None
203210
) = None,
@@ -543,7 +550,7 @@ def to_debug_report(self):
543550
f"OS: {sys.platform}\n"
544551
f"Python Version: {sys.version}\n"
545552
"Version of the API: 1.x\n"
546-
"SDK Package Version: 0.9.7"
553+
f"SDK Package Version: {SDK_VERSION}"
547554
)
548555

549556
def get_host_settings(self):

openfga_sdk/constants.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Python SDK for OpenFGA
3+
4+
API version: 1.x
5+
Website: https://openfga.dev
6+
Documentation: https://openfga.dev/docs
7+
Support: https://openfga.dev/community
8+
License: [Apache-2.0](https://github.com/openfga/python-sdk/blob/main/LICENSE)
9+
10+
NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
11+
"""
12+
13+
"""
14+
Constants used throughout the OpenFGA Python SDK.
15+
"""
16+
17+
from typing import Final
18+
19+
# SDK version information
20+
SDK_VERSION: Final[str] = "0.9.7" # Version of the OpenFGA Python SDK
21+
22+
# User agent for HTTP requests
23+
USER_AGENT: Final[str] = "openfga-sdk python/0.9.7" # User agent used in HTTP requests
24+
25+
# Sample API domain for examples
26+
SAMPLE_BASE_DOMAIN: Final[str] = "fga.example" # Example API domain for documentation/tests
27+
28+
# Retry configuration
29+
RETRY_MAX_ALLOWED_NUMBER: Final[int] = 15 # Maximum allowed number of retries for HTTP requests
30+
DEFAULT_MAX_RETRY: Final[int] = 3 # Default maximum number of retries for HTTP requests
31+
DEFAULT_MIN_WAIT_IN_MS: Final[int] = 100 # Default minimum wait time between retries in milliseconds
32+
MAX_BACKOFF_TIME_IN_SEC: Final[int] = 120 # Maximum backoff time in seconds
33+
RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC: Final[int] = 1800 # Maximum allowable duration for retry headers in seconds
34+
35+
# Client configuration
36+
CLIENT_MAX_METHOD_PARALLEL_REQUESTS: Final[int] = 10 # Maximum number of parallel requests for a single method
37+
CLIENT_MAX_BATCH_SIZE: Final[int] = 50 # Maximum batch size for batch requests
38+
DEFAULT_CONNECTION_TIMEOUT_IN_MS: Final[int] = 10000 # Default connection timeout in milliseconds
39+
40+
# Token management
41+
TOKEN_EXPIRY_THRESHOLD_BUFFER_IN_SEC: Final[int] = 300 # Buffer time in seconds before token expiry to consider it expired
42+
TOKEN_EXPIRY_JITTER_IN_SEC: Final[int] = 300 # Jitter time in seconds to add randomness to token expiry checks
43+
44+
# HTTP headers
45+
CLIENT_METHOD_HEADER: Final[str] = "X-OpenFGA-Client-Method" # Header used to identify the client method
46+
CLIENT_BULK_REQUEST_ID_HEADER: Final[str] = "X-OpenFGA-Client-Bulk-Request-Id" # Header used to identify bulk requests
47+
RETRY_AFTER_HEADER_NAME: Final[str] = "Retry-After" # Standard HTTP header for retry after
48+
RATE_LIMIT_RESET_HEADER_NAME: Final[str] = "X-RateLimit-Reset" # Rate limit reset header name
49+
RATE_LIMIT_RESET_ALT_HEADER_NAME: Final[str] = "X-Rate-Limit-Reset" # Alternative rate limit reset header name

openfga_sdk/oauth2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from openfga_sdk.exceptions import AuthenticationError
2626
from openfga_sdk.telemetry.attributes import TelemetryAttributes
2727
from openfga_sdk.telemetry.telemetry import Telemetry
28+
from openfga_sdk.constants import USER_AGENT
2829

2930

3031
def jitter(loop_count, min_wait_in_ms):
@@ -90,7 +91,7 @@ async def _obtain_token(self, client):
9091
{
9192
"Accept": "application/json",
9293
"Content-Type": "application/x-www-form-urlencoded",
93-
"User-Agent": "openfga-sdk (python) 0.9.7",
94+
"User-Agent": USER_AGENT,
9495
}
9596
)
9697

openfga_sdk/sync/api_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
from openfga_sdk.sync import oauth2, rest
3737
from openfga_sdk.telemetry import Telemetry
3838
from openfga_sdk.telemetry.attributes import TelemetryAttribute, TelemetryAttributes
39-
40-
41-
DEFAULT_USER_AGENT = "openfga-sdk python/0.9.7"
39+
from openfga_sdk.constants import USER_AGENT as DEFAULT_USER_AGENT
40+
from openfga_sdk.constants import MAX_BACKOFF_TIME_IN_SEC
41+
from openfga_sdk.constants import RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC
4242

4343

4444
def random_time(loop_count, min_wait_in_ms) -> float:
@@ -257,7 +257,7 @@ def __call_api(
257257
self.configuration.retry_params is not None
258258
and self.configuration.retry_params.max_wait_in_sec is not None
259259
)
260-
else 120
260+
else MAX_BACKOFF_TIME_IN_SEC
261261
)
262262
if _retry_params is not None:
263263
if _retry_params.max_retry is not None:
@@ -423,7 +423,7 @@ def _parse_retry_after_header(self, headers) -> int:
423423
except ApiException:
424424
wait_time_in_sec = int(retry_after_header)
425425

426-
if wait_time_in_sec > 1800 or wait_time_in_sec < 1:
426+
if wait_time_in_sec > RETRY_HEADER_MAX_ALLOWABLE_DURATION_IN_SEC or wait_time_in_sec < 1:
427427
raise ValueError("Retry-After header is invalid")
428428

429429
return math.ceil(wait_time_in_sec)

openfga_sdk/sync/client/client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@
7575
from openfga_sdk.sync.api_client import ApiClient
7676
from openfga_sdk.sync.open_fga_api import OpenFgaApi
7777
from openfga_sdk.validation import is_well_formed_ulid_string
78-
79-
80-
CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method"
81-
CLIENT_BULK_REQUEST_ID_HEADER = "X-OpenFGA-Client-Bulk-Request-Id"
78+
from openfga_sdk.constants import (
79+
CLIENT_METHOD_HEADER,
80+
CLIENT_BULK_REQUEST_ID_HEADER,
81+
CLIENT_MAX_METHOD_PARALLEL_REQUESTS,
82+
CLIENT_MAX_BATCH_SIZE,
83+
)
8284

8385

8486
def _chuck_array(array, max_size):
@@ -711,7 +713,7 @@ def client_batch_check(
711713
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
712714
)
713715

714-
max_parallel_requests = 10
716+
max_parallel_requests = CLIENT_MAX_METHOD_PARALLEL_REQUESTS
715717
if options is not None and "max_parallel_requests" in options:
716718
if (
717719
isinstance(options["max_parallel_requests"], str)
@@ -770,7 +772,7 @@ def batch_check(
770772
options, CLIENT_BULK_REQUEST_ID_HEADER, str(uuid.uuid4())
771773
)
772774

773-
max_parallel_requests = 10
775+
max_parallel_requests = CLIENT_MAX_METHOD_PARALLEL_REQUESTS
774776
if options is not None and "max_parallel_requests" in options:
775777
if (
776778
isinstance(options["max_parallel_requests"], str)
@@ -780,7 +782,7 @@ def batch_check(
780782
elif isinstance(options["max_parallel_requests"], int):
781783
max_parallel_requests = options["max_parallel_requests"]
782784

783-
max_batch_size = 50
785+
max_batch_size = CLIENT_MAX_BATCH_SIZE
784786
if options is not None and "max_batch_size" in options:
785787
if (
786788
isinstance(options["max_batch_size"], str)

openfga_sdk/sync/oauth2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from openfga_sdk.exceptions import AuthenticationError
2626
from openfga_sdk.telemetry.attributes import TelemetryAttributes
2727
from openfga_sdk.telemetry.telemetry import Telemetry
28+
from openfga_sdk.constants import USER_AGENT
2829

2930

3031
def jitter(loop_count, min_wait_in_ms):
@@ -90,7 +91,7 @@ def _obtain_token(self, client):
9091
{
9192
"Accept": "application/json",
9293
"Content-Type": "application/x-www-form-urlencoded",
93-
"User-Agent": "openfga-sdk (python) 0.9.7",
94+
"User-Agent": USER_AGENT,
9495
}
9596
)
9697

test/api/open_fga_api_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
from openfga_sdk.models.write_request import WriteRequest
9292
from openfga_sdk.models.write_request_deletes import WriteRequestDeletes
9393
from openfga_sdk.models.write_request_writes import WriteRequestWrites
94+
from openfga_sdk.constants import USER_AGENT
9495

9596

9697
store_id = "01H0H015178Y2V4CX10C2KGHF4"
@@ -1782,7 +1783,7 @@ async def test_check_api_token(self, mock_request):
17821783
{
17831784
"Accept": "application/json",
17841785
"Content-Type": "application/json",
1785-
"User-Agent": "openfga-sdk python/0.9.7",
1786+
"User-Agent": USER_AGENT,
17861787
"Authorization": "Bearer TOKEN1",
17871788
}
17881789
)
@@ -1836,7 +1837,7 @@ async def test_check_custom_header(self, mock_request):
18361837
{
18371838
"Accept": "application/json",
18381839
"Content-Type": "application/json",
1839-
"User-Agent": "openfga-sdk python/0.9.7",
1840+
"User-Agent": USER_AGENT,
18401841
"Custom Header": "custom value",
18411842
}
18421843
)
@@ -2084,7 +2085,7 @@ async def test_check_custom_header_override_default_header(self, mock_request):
20842085
{
20852086
"Accept": "application/json",
20862087
"Content-Type": "application/json",
2087-
"User-Agent": "openfga-sdk python/0.9.7",
2088+
"User-Agent": USER_AGENT,
20882089
"X-Custom-Header": "per-request-value", # Should be the per-request value
20892090
}
20902091
)
@@ -2143,7 +2144,7 @@ async def test_check_per_request_header_and_default_header_coexist(
21432144
{
21442145
"Accept": "application/json",
21452146
"Content-Type": "application/json",
2146-
"User-Agent": "openfga-sdk python/0.9.7",
2147+
"User-Agent": USER_AGENT,
21472148
"X-Default-Header": "default-value", # Default header preserved
21482149
"X-Per-Request-Header": "per-request-value", # Per-request header added
21492150
}

0 commit comments

Comments
 (0)