Skip to content

Commit 28bbb65

Browse files
committed
feat(cli): replace balance check with CloudflareSolver
Updated the balance command to use the new CloudflareSolver class for retrieving account balance, improving error handling and API interaction.
1 parent 28cc153 commit 28bbb65

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

cfsolver/cli.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -403,31 +403,19 @@ def request(
403403
@click.pass_context
404404
def balance(ctx, api_key, api_base, api_proxy):
405405
"""Check account balance."""
406-
from curl_cffi.requests import Session
406+
from .client import CloudflareSolver
407407

408408
api_key = get_api_key(api_key)
409409
api_base = get_api_base(api_base)
410410

411411
try:
412-
with Session(verify=False, proxy=api_proxy) as session:
413-
resp = session.post(
414-
f"{api_base}/api/getBalance",
415-
json={"clientKey": api_key},
416-
)
417-
418-
if resp.status_code != 200:
419-
click.echo(f"[x] Error: HTTP {resp.status_code}", err=True)
420-
sys.exit(1)
421-
422-
data = resp.json()
423-
if data.get("errorId"):
424-
click.echo(
425-
f"[x] Error: {data.get('errorDescription', 'Unknown error')}",
426-
err=True,
427-
)
428-
sys.exit(1)
429-
430-
balance_val = data.get("balance", 0)
412+
with CloudflareSolver(
413+
api_key=api_key,
414+
api_base=api_base,
415+
api_proxy=api_proxy,
416+
solve=False,
417+
) as solver:
418+
balance_val = solver.get_balance()
431419
click.echo(f"[+] Balance: {balance_val}")
432420

433421
except Exception as e:

cfsolver/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,31 @@ def _wait_for_result(self, task_id: str, timeout: int = 120) -> Dict[str, Any]:
535535

536536
raise CFSolverTimeoutError("Task timed out")
537537

538+
def get_balance(self) -> float:
539+
"""
540+
Get the current account balance.
541+
542+
Returns:
543+
The account balance as a float
544+
545+
Raises:
546+
CFSolverAPIError: If the API request fails
547+
"""
548+
with Session(verify=False, proxy=self.api_proxy) as api_session:
549+
resp = api_session.post(
550+
f"{self.api_base}/api/getBalance",
551+
json={"clientKey": self.api_key},
552+
)
553+
_raise_for_status(resp)
554+
data = resp.json()
555+
556+
if data.get("errorId"):
557+
raise CFSolverAPIError(
558+
f"Failed to get balance: {data.get('errorDescription', 'Unknown error')}"
559+
)
560+
561+
return float(data.get("balance", 0))
562+
538563
def solve_turnstile(self, url: str, sitekey: str) -> str:
539564
"""
540565
Solve a Turnstile challenge and return the token.

0 commit comments

Comments
 (0)