Skip to content

Commit 6c8bab5

Browse files
committed
add headers
1 parent 12db14d commit 6c8bab5

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

xrpl/asyncio/clients/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from abc import ABC, abstractmethod
6-
from typing import Optional
6+
from typing import Optional, Dict
77

88
from typing_extensions import Final, Self
99

@@ -24,7 +24,11 @@ class Client(ABC):
2424
:meta private:
2525
"""
2626

27-
def __init__(self: Self, url: str) -> None:
27+
def __init__(
28+
self: Self,
29+
url: str,
30+
headers: Optional[Dict[str, str]] = None,
31+
) -> None:
2832
"""
2933
Initializes a client.
3034
@@ -34,6 +38,7 @@ def __init__(self: Self, url: str) -> None:
3438
self.url = url
3539
self.network_id: Optional[int] = None
3640
self.build_version: Optional[str] = None
41+
self.headers = headers or {}
3742

3843
@abstractmethod
3944
async def _request_impl(

xrpl/asyncio/clients/json_rpc_base.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from json import JSONDecodeError
6+
from typing import Optional, Dict
67

78
from httpx import AsyncClient
89
from typing_extensions import Self
@@ -21,8 +22,25 @@ class JsonRpcBase(Client):
2122
:meta private:
2223
"""
2324

25+
def __init__(
26+
self: Self,
27+
url: str,
28+
headers: Optional[Dict[str, str]] = None,
29+
) -> None:
30+
"""
31+
Initializes a new JsonRpcBase client.
32+
Arguments:
33+
url: The URL of the XRPL node to connect to.
34+
headers: Optional default headers for all requests (e.g. API key or Dhali payment-claim).
35+
"""
36+
super().__init__(url, headers=headers)
37+
2438
async def _request_impl(
25-
self: Self, request: Request, *, timeout: float = REQUEST_TIMEOUT
39+
self: Self,
40+
request: Request,
41+
*,
42+
timeout: float = REQUEST_TIMEOUT,
43+
headers: Optional[Dict[str, str]] = None,
2644
) -> Response:
2745
"""
2846
Base ``_request_impl`` implementation for JSON RPC.
@@ -40,10 +58,18 @@ async def _request_impl(
4058
4159
:meta private:
4260
"""
61+
62+
merged_headers = {
63+
"Content-Type": "application/json",
64+
**self.headers,
65+
**(headers or {}),
66+
}
67+
4368
async with AsyncClient(timeout=timeout) as http_client:
4469
response = await http_client.post(
4570
self.url,
4671
json=request_to_json_rpc(request),
72+
headers=merged_headers,
4773
)
4874
try:
4975
return json_to_response(response.json())

xrpl/asyncio/clients/websocket_base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ class WebsocketBase(Client):
5656
:meta private:
5757
"""
5858

59-
def __init__(self: Self, url: str) -> None:
59+
def __init__(
60+
self: Self, url: str, headers: Optional[Dict[str, str]] = None
61+
) -> None:
6062
"""
6163
Initializes a websocket client.
6264
@@ -71,7 +73,7 @@ def __init__(self: Self, url: str) -> None:
7173
# will initialize a new event loop when it opens the connection, so for
7274
# that client the initializer cannot create the queue
7375
self._messages: Optional[_MESSAGES_TYPE] = None
74-
super().__init__(url)
76+
super().__init__(url, headers=headers)
7577

7678
def is_open(self: Self) -> bool:
7779
"""
@@ -90,7 +92,9 @@ def is_open(self: Self) -> bool:
9092
async def _do_open(self: Self) -> None:
9193
"""Connects the client to the Web Socket API at its URL."""
9294
# open the connection
93-
self._websocket = await websocket_client.connect(self.url)
95+
self._websocket = await websocket_client.connect(
96+
self.url, extra_headers=self.headers
97+
)
9498

9599
# make a message queue
96100
self._messages = asyncio.Queue()

0 commit comments

Comments
 (0)