44from collections .abc import Awaitable , Callable , Mapping
55from functools import wraps
66from inspect import signature
7- from typing import ParamSpec , TypeVar
7+ from typing import Any , ParamSpec , TypeVar
88
99from mobilerun .errors .cloud import BillingError , BillingRequired
1010from mobilerun .models .billing import Billing , Plan , ResourcePlan
11+ from mobilerun .utils .terminal import can_prompt
1112
1213P = ParamSpec ("P" )
1314R = TypeVar ("R" )
1415
1516
1617def billing_prompt_is_interactive () -> bool :
1718 """Return whether a billing prompt can use terminal stdin and stderr."""
18- stdin , stderr = sys .stdin , sys .stderr
19- return (
20- stdin is not None and stderr is not None and stdin .isatty () and stderr .isatty ()
21- )
19+ return can_prompt (sys .stderr )
2220
2321
2422async def _confirm_billing (error : BillingRequired ) -> bool :
@@ -66,10 +64,30 @@ async def with_billing_retry(
6664 return await operation (needed ), needed
6765
6866
67+ def buys_connect_proxy (proxy : object ) -> bool :
68+ """Return whether a proxy spec orders a new Connect proxy by country."""
69+ connect = proxy .get ("connect" ) if isinstance (proxy , Mapping ) else None
70+ return isinstance (connect , Mapping ) and bool (connect .get ("country" ))
71+
72+
73+ def rents_resource (arguments : Mapping [str , Any ]) -> bool :
74+ """Return whether a number or mailbox call asks for a rented resource."""
75+ plan = arguments .get ("plan" )
76+ return plan is not None and ResourcePlan (plan ) is ResourcePlan .RENT
77+
78+
6979def requires (
70- minimum : Billing , * , operation : str | None = None
80+ minimum : Billing ,
81+ * ,
82+ operation : str | None = None ,
83+ paid_if : Callable [[Mapping [str , Any ]], bool ] | None = None ,
7184) -> Callable [[Callable [P , R ]], Callable [P , R ]]:
72- """Declare and enforce a boundary's minimum commerce mode before any work."""
85+ """Declare and enforce a boundary's minimum commerce mode before any work.
86+
87+ ``paid_if`` receives the bound arguments and returns whether this call needs
88+ paid billing. Declare it on the function it guards: the guard never infers
89+ a rule from a module or function name.
90+ """
7391
7492 def decorate (function : Callable [P , R ]) -> Callable [P , R ]:
7593 sig = signature (function )
@@ -79,30 +97,10 @@ def guarded(*args: P.args, **kwargs: P.kwargs) -> R:
7997 bound = sig .bind (* args , ** kwargs )
8098 given = bound .arguments ["billing" ]
8199 needed = minimum
82- params = bound .arguments .get ("params" , {})
83- proxy = bound .arguments .get ("proxy" ) or params
84- connect = proxy .get ("connect" , {}) if isinstance (proxy , Mapping ) else {}
85- if function .__module__ .endswith ((".devices" , ".proxies" )):
86- if isinstance (connect , Mapping ) and connect .get ("country" ):
87- needed = Billing .PAID
88100 try :
101+ if paid_if is not None and paid_if (bound .arguments ):
102+ needed = Billing .PAID
89103 mode = Billing (given )
90- if (
91- function .__module__ .endswith (".devices" )
92- and function .__name__ == "create"
93- ):
94- plan = bound .arguments .get ("plan" )
95- if plan is not None :
96- plan = Plan (plan )
97- if plan in (Plan .AUTO , Plan .MINUTE ):
98- needed = Billing .PAID
99- if (
100- function .__module__ .endswith ((".numbers" , ".mailboxes" ))
101- and function .__name__ == "create"
102- ):
103- plan = bound .arguments .get ("plan" )
104- if plan is not None and ResourcePlan (plan ) is ResourcePlan .RENT :
105- needed = Billing .PAID
106104 except (ValueError , TypeError ) as exc :
107105 error = BillingError (
108106 operation = function .__name__ ,
0 commit comments