diff --git a/examples/quickstart_async.py b/examples/quickstart_async.py index e5301db..3bbfca0 100644 --- a/examples/quickstart_async.py +++ b/examples/quickstart_async.py @@ -12,6 +12,7 @@ from didww_verification import ( AsyncVerificationClient, BasicAuth, + DidwwRateLimitedError, DidwwValidationError, Environment, ) @@ -21,9 +22,14 @@ async def main(destination: str) -> int: auth = BasicAuth(os.environ["DIDWW_KEY"], os.environ["DIDWW_SECRET"]) async with AsyncVerificationClient(auth, environment=Environment.SANDBOX) as client: - verification = await client.start_verification( - destination=destination, delivery_method="sms" - ) + try: + verification = await client.start_verification( + destination=destination, delivery_method="sms" + ) + except DidwwRateLimitedError as exc: + wait = f"{exc.retry_after}s" if exc.retry_after is not None else "a bit" + print(f"too soon after the last start; wait {wait} and try again") + return 1 print(f"started {verification.id}") code = await asyncio.to_thread(input, "code from the SMS: ") diff --git a/examples/quickstart_sync.py b/examples/quickstart_sync.py index d2d32f1..f58449c 100644 --- a/examples/quickstart_sync.py +++ b/examples/quickstart_sync.py @@ -10,6 +10,7 @@ from didww_verification import ( BasicAuth, + DidwwRateLimitedError, DidwwValidationError, Environment, SmsOptions, @@ -21,11 +22,16 @@ def main(destination: str) -> int: auth = BasicAuth(os.environ["DIDWW_KEY"], os.environ["DIDWW_SECRET"]) with VerificationClient(auth, environment=Environment.SANDBOX) as client: - verification = client.start_verification( - destination=destination, - delivery_method="sms", - sms=SmsOptions(languages=["en-US"]), - ) + try: + verification = client.start_verification( + destination=destination, + delivery_method="sms", + sms=SmsOptions(languages=["en-US"]), + ) + except DidwwRateLimitedError as exc: + wait = f"{exc.retry_after}s" if exc.retry_after is not None else "a bit" + print(f"too soon after the last start; wait {wait} and try again") + return 1 print(f"started {verification.id}, expires at {verification.expires_at}") if verification.sms is not None: print(f"rendered in {verification.sms.language}")