Skip to content

Commit 858134f

Browse files
committed
feat: restructure and extract common class and func
1 parent aebd088 commit 858134f

5 files changed

Lines changed: 34 additions & 52 deletions

File tree

openfga_sdk/oauth2.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import asyncio
22
import json
3-
import math
43
import random
5-
import sys
64

7-
from dataclasses import dataclass
85
from datetime import datetime, timedelta
96

107
import urllib3
@@ -17,32 +14,11 @@
1714
)
1815
from openfga_sdk.credentials import Credentials
1916
from openfga_sdk.exceptions import AuthenticationError
17+
from openfga_sdk.oauth2_common import _TokenState, jitter
2018
from openfga_sdk.telemetry.attributes import TelemetryAttributes
2119
from openfga_sdk.telemetry.telemetry import Telemetry
2220

2321

24-
@dataclass(frozen=True)
25-
class _TokenState:
26-
access_token: str
27-
expiry_time: datetime
28-
expiry_buffer: float
29-
30-
31-
def jitter(loop_count, min_wait_in_ms):
32-
"""
33-
Generate a random jitter value for exponential backoff
34-
"""
35-
minimum = math.ceil(2**loop_count * min_wait_in_ms)
36-
maximum = math.ceil(2 ** (loop_count + 1) * min_wait_in_ms)
37-
jitter = random.randrange(minimum, maximum) / 1000
38-
39-
# If running in pytest, set jitter to 0 to speed up tests
40-
if "pytest" in sys.modules:
41-
jitter = 0
42-
43-
return jitter
44-
45-
4622
class OAuth2Client:
4723
def __init__(self, credentials: Credentials, configuration=None):
4824
self._credentials = credentials

openfga_sdk/oauth2_common.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import math
2+
import random
3+
import sys
4+
5+
from dataclasses import dataclass
6+
from datetime import datetime
7+
8+
9+
@dataclass(frozen=True)
10+
class _TokenState:
11+
access_token: str
12+
expiry_time: datetime
13+
expiry_buffer: float
14+
15+
16+
def jitter(loop_count, min_wait_in_ms):
17+
"""
18+
Generate a random jitter value for exponential backoff
19+
"""
20+
minimum = math.ceil(2**loop_count * min_wait_in_ms)
21+
maximum = math.ceil(2 ** (loop_count + 1) * min_wait_in_ms)
22+
jitter = random.randrange(minimum, maximum) / 1000
23+
24+
# If running in pytest, set jitter to 0 to speed up tests
25+
if "pytest" in sys.modules:
26+
jitter = 0
27+
28+
return jitter

openfga_sdk/sync/oauth2.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import json
2-
import math
32
import random
4-
import sys
53
import threading
64
import time
75

8-
from dataclasses import dataclass
96
from datetime import datetime, timedelta
107

118
import urllib3
@@ -18,32 +15,11 @@
1815
)
1916
from openfga_sdk.credentials import Credentials
2017
from openfga_sdk.exceptions import AuthenticationError
18+
from openfga_sdk.oauth2_common import _TokenState, jitter
2119
from openfga_sdk.telemetry.attributes import TelemetryAttributes
2220
from openfga_sdk.telemetry.telemetry import Telemetry
2321

2422

25-
@dataclass(frozen=True)
26-
class _TokenState:
27-
access_token: str
28-
expiry_time: datetime
29-
expiry_buffer: float
30-
31-
32-
def jitter(loop_count, min_wait_in_ms):
33-
"""
34-
Generate a random jitter value for exponential backoff
35-
"""
36-
minimum = math.ceil(2**loop_count * min_wait_in_ms)
37-
maximum = math.ceil(2 ** (loop_count + 1) * min_wait_in_ms)
38-
jitter = random.randrange(minimum, maximum) / 1000
39-
40-
# If running in pytest, set jitter to 0 to speed up tests
41-
if "pytest" in sys.modules:
42-
jitter = 0
43-
44-
return jitter
45-
46-
4723
class OAuth2Client:
4824
def __init__(self, credentials: Credentials, configuration=None):
4925
self._credentials = credentials

test/oauth2_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from openfga_sdk.constants import TOKEN_EXPIRY_THRESHOLD_BUFFER_IN_SEC, USER_AGENT
1212
from openfga_sdk.credentials import CredentialConfiguration, Credentials
1313
from openfga_sdk.exceptions import AuthenticationError
14-
from openfga_sdk.oauth2 import OAuth2Client, _TokenState
14+
from openfga_sdk.oauth2 import OAuth2Client
15+
from openfga_sdk.oauth2_common import _TokenState
1516

1617

1718
# Helper function to construct mock response

test/sync/oauth2_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
from openfga_sdk.constants import TOKEN_EXPIRY_THRESHOLD_BUFFER_IN_SEC, USER_AGENT
1212
from openfga_sdk.credentials import CredentialConfiguration, Credentials
1313
from openfga_sdk.exceptions import AuthenticationError
14+
from openfga_sdk.oauth2_common import _TokenState
1415
from openfga_sdk.sync import rest
15-
from openfga_sdk.sync.oauth2 import OAuth2Client, _TokenState
16+
from openfga_sdk.sync.oauth2 import OAuth2Client
1617

1718

1819
# Helper function to construct mock response

0 commit comments

Comments
 (0)