bitcoin/contrib/codegen/templates/python_client.jinja2
enoch 7a4108c30e Add Python client template using Jinja2
Include the python_client.jinja2 template in the code generator. This
template defines the structure for the generated Python RPC client and
integrates with generate_client.py to reflect RPC schema changes
automatically.
2025-03-23 18:41:46 +01:00

40 lines
1.4 KiB
Django/Jinja

#!/usr/bin/env python3
"""
Auto-generated Bitcoin RPC client (Python)
"""
import requests
import json
class BitcoinRPCClient:
def __init__(self, rpc_user, rpc_password, host="127.0.0.1", port=8332):
self.url = f"http://{host}:{port}"
self.auth = (rpc_user, rpc_password)
def call(self, method, params):
payload = {"method": method, "params": params, "id": 1}
headers = {"Content-Type": "application/json"}
response = requests.post(self.url, auth=self.auth, headers=headers, data=json.dumps(payload))
return response.json()
{% for rpc_name, details in rpcs.items() %}
def {{ rpc_name }}(self{% if details.argument_names|length > 0 %}, {% for arg in details.argument_names %}{{ arg }}{% if not loop.last %}, {% endif %}{% endfor %}{% endif %}):
"""
{{ details.description }}
{% if details.argument_names|length > 0 %}
Arguments:
{% for arg in details.argument_names %}
- {{ arg }}
{% endfor %}
{% endif %}
"""
params = [{% for arg in details.argument_names %}{{ arg }}{% if not loop.last %}, {% endif %}{% endfor %}]
return self.call("{{ rpc_name }}", params)
{% endfor %}
if __name__ == '__main__':
# Example usage:
client = BitcoinRPCClient("rpcuser", "rpcpassword")
result = client.getblockchaininfo()
print(result)