Skip to content

Commit 40a25dc

Browse files
committed
fix: add peers api
1 parent c346ef2 commit 40a25dc

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

hathor/p2p/resources/add_peers.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
from json import JSONDecodeError
1616

17+
from twisted.internet.defer import Deferred
18+
from twisted.web.http import Request
19+
1720
from hathor.api_util import Resource, render_options, set_cors
1821
from hathor.cli.openapi_files.register import register_resource
22+
from hathor.manager import HathorManager
1923
from hathor.p2p.peer_discovery import BootstrapPeerDiscovery
2024
from hathor.util import json_dumpb, json_loadb
2125

@@ -28,16 +32,17 @@ class AddPeersResource(Resource):
2832
"""
2933
isLeaf = True
3034

31-
def __init__(self, manager):
35+
def __init__(self, manager: HathorManager) -> None:
3236
self.manager = manager
3337

34-
def render_POST(self, request):
38+
def render_POST(self, request: Request) -> bytes:
3539
""" Add p2p peers
3640
It expects a list of peers, in the format protocol://host:port (tcp://172.121.212.12:40403)
3741
"""
3842
request.setHeader(b'content-type', b'application/json; charset=utf-8')
3943
set_cors(request, 'POST')
4044

45+
assert request.content is not None
4146
raw_data = request.content.read()
4247
if raw_data is None:
4348
return json_dumpb({'success': False, 'message': 'No post data'})
@@ -73,12 +78,14 @@ def already_connected(connection_string: str) -> bool:
7378
filtered_peers = [connection_string for connection_string in peers if not already_connected(connection_string)]
7479

7580
pd = BootstrapPeerDiscovery(filtered_peers)
76-
pd.discover_and_connect(self.manager.connections.connect_to)
81+
# this fires and forget the coroutine, which is compatible with the original behavior
82+
coro = pd.discover_and_connect(self.manager.connections.connect_to)
83+
Deferred.fromCoroutine(coro)
7784

7885
ret = {'success': True, 'peers': filtered_peers}
7986
return json_dumpb(ret)
8087

81-
def render_OPTIONS(self, request):
88+
def render_OPTIONS(self, request: Request) -> int:
8289
return render_options(request)
8390

8491

0 commit comments

Comments
 (0)