Skip to content

Commit fe5c3b6

Browse files
committed
Avoid duplicate logic
1 parent 07378a8 commit fe5c3b6

File tree

1 file changed

+26
-42
lines changed

1 file changed

+26
-42
lines changed

arkiv_sdk/types.py

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,30 @@ def __repr__(self) -> str:
168168
return f"ExpirationTime(blocks={self._blocks}, seconds={self.to_seconds()})"
169169

170170

171+
def getBTL(duration: int | ExpirationTime | None, blocks: int | None) -> int:
172+
"""Resolve the BTL given either a duration or a number of blocks."""
173+
if duration is not None:
174+
if isinstance(duration, int):
175+
# Treat as seconds and convert to blocks
176+
return ExpirationTime.from_seconds(duration).blocks
177+
# It's an ExpirationTime object
178+
return duration.blocks
179+
180+
if blocks is not None:
181+
# Warn about deprecated BTL
182+
warnings.warn(
183+
"⚠️ BTL is deprecated and will be removed in a future version. "
184+
"Please use 'expires_in' instead. "
185+
"Example: expires_in=3600 (seconds) or "
186+
"expires_in=ExpirationTime.from_hours(1)",
187+
DeprecationWarning,
188+
stacklevel=3,
189+
)
190+
return blocks
191+
192+
raise ValueError("Either 'expires_in' or 'btl' must be specified")
193+
194+
171195
def resolve_expiration_blocks(
172196
expires_in: int | ExpirationTime | None,
173197
btl: int | None,
@@ -188,27 +212,7 @@ def resolve_expiration_blocks(
188212
ValueError: If neither expires_in nor btl is specified
189213
190214
"""
191-
# Priority: expires_in takes precedence
192-
if expires_in is not None:
193-
if isinstance(expires_in, int):
194-
# Treat as seconds and convert to blocks
195-
return ExpirationTime.from_seconds(expires_in).blocks
196-
# It's an ExpirationTime object
197-
return expires_in.blocks
198-
199-
if btl is not None:
200-
# Warn about deprecated BTL
201-
warnings.warn(
202-
"⚠️ BTL is deprecated and will be removed in a future version. "
203-
"Please use 'expires_in' instead. "
204-
"Example: expires_in=3600 (seconds) or "
205-
"expires_in=ExpirationTime.from_hours(1)",
206-
DeprecationWarning,
207-
stacklevel=3,
208-
)
209-
return btl
210-
211-
raise ValueError("Either 'expires_in' or 'btl' must be specified")
215+
return getBTL(expires_in, btl)
212216

213217

214218
def resolve_extension_blocks(
@@ -231,27 +235,7 @@ def resolve_extension_blocks(
231235
ValueError: If neither duration nor number_of_blocks is specified
232236
233237
"""
234-
# Priority: duration takes precedence
235-
if duration is not None:
236-
if isinstance(duration, int):
237-
# Treat as seconds and convert to blocks
238-
return ExpirationTime.from_seconds(duration).blocks
239-
# It's an ExpirationTime object
240-
return duration.blocks
241-
242-
if number_of_blocks is not None:
243-
# Warn about deprecated number_of_blocks
244-
warnings.warn(
245-
"⚠️ number_of_blocks is deprecated and will be removed in a "
246-
"future version. Please use 'duration' instead. "
247-
"Example: duration=86400 (seconds) or "
248-
"duration=ExpirationTime.from_days(1)",
249-
DeprecationWarning,
250-
stacklevel=3,
251-
)
252-
return number_of_blocks
253-
254-
raise ValueError("Either 'duration' or 'number_of_blocks' must be specified")
238+
return getBTL(duration, number_of_blocks)
255239

256240

257241
# TODO: use new generic syntax once we can bump to python 3.12 or higher

0 commit comments

Comments
 (0)