Setting the file. One moment.
X402 Fetch CLI · Agents Pay · aws/agent-toolkit-for-aws · Skills Docs
Repo No. 14 · Agents Pay
↖ Back to the coverEnd User Computing Skills
Messaging And Streaming Skills
Migration And Modernization Skills
Networking And Content Delivery Skills
Security And Identity Skills
Web And Mobile Development
132 skills · 818 min
ContentsBack to the top of the page 70
Creating Amazon Aurora Db Cluster With Instances
93
Routing Traffic With Route53 And CloudFront
Resilience Program Design
Creating API Gateway Stage
(opens in a new tab)
scripts/ x402_fetch_cli.py
Python · 83 lines · 3 KB
16
2 refused by policy, or missing configuration — NOT an error to retry
17 1 unexpected failure
18
19 A refusal is exit 2 and not 1 on purpose: it is a decision, not a fault. Retrying it
20 unchanged will refuse again.
21 """
22
23 from __future__ import annotations
24
25 import argparse
26 import json
27 import sys
28 from pathlib import Path
29
30 # Importable whether the harness runs this from the skill directory or by absolute
31 # path, without requiring PYTHONPATH to be set.
32 sys.path.insert( 0 , str (Path( __file__ ).resolve().parent))
33
34 import x402_fetch # noqa: E402
35
36
37 def main () -> int :
38 ap = argparse.ArgumentParser(
39 prog = "x402_fetch_cli.py" ,
40 description = "Pay for an x402-protected URL, or report payment session status." ,
41 epilog = "Exit 0 = ok, 2 = refused or unconfigured, 1 = unexpected failure." ,
42 )
43 ap.add_argument( "url" , nargs = "?" , help = "The https:// URL to fetch, paying if it returns 402" )
44 ap.add_argument( "--status" , action = "store_true" ,
45 help = "Report whether the payment session can currently be spent" )
46 ap.add_argument( "--browser-handle" , metavar = "URL" , dest = "browser_url" ,
47 help = "Pay for URL and return an opaque single-use handle for a browser "
48 "navigation, instead of fetching the content here" )
49 ap.add_argument( "--method" , default = "GET" , choices = [ "GET" , "HEAD" ],
50 help = "HTTP method (GET or HEAD only — a request body would let the "
51 "agent send data to an arbitrary origin)" )
52 ap.add_argument( "--purchase-id" , default = None ,
53 help = "Distinguish a deliberate repeat purchase of the same resource" )
54 args = ap.parse_args()
55
56 if args.status:
57 payload = x402_fetch.payment_session_status()
58 print (payload)
59 return 0 if json.loads(payload).get( "usable" ) else 2
60
61 target = args.browser_url or args.url
62 if not target:
63 ap.print_usage(sys.stderr)
64 print ( " \n Give a URL, or --status." , file = sys.stderr)
65 return 1
66
67 if args.browser_url:
68 payload = x402_fetch.prepare_browser_payment(target, args.purchase_id)
69 else :
70 payload = x402_fetch.x402_fetch(target, args.purchase_id, args.method)
71
72 print (payload)
73 result = json.loads(payload)
74 if result.get( "refused" ):
75 return 2
76 return 0
77
78
79 if __name__ == "__main__" :
80 try :
81 sys.exit(main())
82 except KeyboardInterrupt :
83 sys.exit( 1 )