Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/bedrock_agentcore/payments/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

logger = logging.getLogger(__name__)

_PAYMENT_REQUIRED_FIELDS = frozenset({"statusCode", "headers", "body"})
_X402_PAYLOAD_REQUIRED_FIELDS = frozenset({"x402Version", "accepts"})


class PaymentError(Exception):
"""Base exception for payment operations."""
Expand Down Expand Up @@ -1454,8 +1457,7 @@ def _validate_input_parameters(
)

# Validate required fields in payment_required_request
required_fields = {"statusCode", "headers", "body"}
if not all(field in payment_required_request for field in required_fields):
if not all(field in payment_required_request for field in _PAYMENT_REQUIRED_FIELDS):
raise PaymentError(
"Input Validation: 402 payment required request is missing required fields - "
"402 payment required request must contain statusCode, headers, and body"
Expand Down Expand Up @@ -1553,12 +1555,11 @@ def _extract_x402_payload(self, payment_required_request: Dict[str, Any]) -> tup
) from ve

# Validate required fields
required_fields = {"x402Version", "accepts"}
missing_fields = required_fields - set(x402_payload.keys())
missing_fields = _X402_PAYLOAD_REQUIRED_FIELDS - set(x402_payload.keys())
if missing_fields:
raise PaymentError(
f"X.402 Validation: Missing required fields - "
f"x402Payload must contain {', '.join(sorted(required_fields))}, "
f"x402Payload must contain {', '.join(sorted(_X402_PAYLOAD_REQUIRED_FIELDS))}, "
f"but missing: {', '.join(sorted(missing_fields))}"
)

Expand Down
4 changes: 2 additions & 2 deletions src/bedrock_agentcore/policy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
logger = logging.getLogger(__name__)

_FAILED_STATUSES = {"CREATE_FAILED", "UPDATE_FAILED", "DELETE_FAILED"}
_GENERATION_FAILED_STATUSES = frozenset({"GENERATE_FAILED", "DELETE_FAILED"})


class PolicyEngineClient:
Expand Down Expand Up @@ -271,14 +272,13 @@ def generate_policy_asset_and_wait(
generation_id = generation["policyGenerationId"]
logger.info("Started policy generation %s, waiting for completion...", generation_id)

_generation_failed = {"GENERATE_FAILED", "DELETE_FAILED"}
result = wait_until(
lambda: self.cp_client.get_policy_generation(
policyEngineId=policy_engine_id,
policyGenerationId=generation_id,
),
"GENERATED",
_generation_failed,
_GENERATION_FAILED_STATUSES,
wait_config,
)

Expand Down
Loading