Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API key support for JSON-RPC requests #763

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions xrpl/asyncio/clients/json_rpc_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

from json import JSONDecodeError
from typing import Optional

from httpx import AsyncClient
from typing_extensions import Self
Expand All @@ -21,6 +22,17 @@ class JsonRpcBase(Client):
:meta private:
"""

def __init__(self: Self, url: str, api_key: Optional[str] = None) -> None:
"""
Initializes a new JsonRpcBase client.

Arguments:
url: The URL of the XRPL node to connect to.
api_key: Optional API key for connecting to a private XRPL server.
"""
super().__init__(url)
self.api_key = api_key # Store the API key if provided

async def _request_impl(
self: Self, request: Request, *, timeout: float = REQUEST_TIMEOUT
) -> Response:
Expand All @@ -40,10 +52,18 @@ async def _request_impl(

:meta private:
"""
# Prepare headers, including the API key if it’s available
headers = {"Content-Type": "application/json"}
if self.api_key:
headers[
"Authorization"
] = f"Bearer {self.api_key}" # Adjust key name if necessary

async with AsyncClient(timeout=timeout) as http_client:
response = await http_client.post(
self.url,
json=request_to_json_rpc(request),
headers=headers, # Include the headers with the optional API key
)
try:
return json_to_response(response.json())
Expand Down