mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-13 12:33:25 +02:00
Simplify serialization of peer squeak download (#2195)
This commit is contained in:
parent
ad779182d7
commit
c09ab3f209
3 changed files with 13 additions and 21 deletions
|
|
@ -99,19 +99,16 @@ class PeerClient:
|
|||
)
|
||||
if r.status_code != requests.codes.ok:
|
||||
return None
|
||||
logger.info(r)
|
||||
squeak_json = r.json()
|
||||
# squeak_bytes = r.content
|
||||
squeak_type = squeak_json['squeak_type']
|
||||
squeak_bytes_hex = squeak_json['squeak_bytes']
|
||||
squeak_bytes = bytes.fromhex(squeak_bytes_hex)
|
||||
logger.info('Client downloaded: {}'.format(squeak_json))
|
||||
if squeak_type == 'squeak':
|
||||
squeak_bytes = r.content
|
||||
try:
|
||||
return CSqueak.deserialize(squeak_bytes)
|
||||
elif squeak_type == 'resqueak':
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
return CResqueak.deserialize(squeak_bytes)
|
||||
else:
|
||||
return None
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def get_secret_key(self, squeak_hash: bytes) -> Optional[bytes]:
|
||||
squeak_hash_str = squeak_hash.hex()
|
||||
|
|
|
|||
|
|
@ -59,13 +59,10 @@ def create_app(handler):
|
|||
@app.route('/squeak/<hash>')
|
||||
def squeak(hash):
|
||||
try:
|
||||
squeak_type, squeak_bytes = handler.handle_get_squeak_bytes(hash)
|
||||
squeak_bytes = handler.handle_get_squeak_bytes(hash)
|
||||
except NotFoundError:
|
||||
return "Not found", 404
|
||||
return jsonify({
|
||||
'squeak_type': squeak_type,
|
||||
'squeak_bytes': squeak_bytes.hex(),
|
||||
})
|
||||
return squeak_bytes
|
||||
|
||||
@app.route('/secretkey/<hash>')
|
||||
def secret_key(hash):
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@
|
|||
import logging
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Tuple
|
||||
|
||||
from squeak.core.keys import SqueakPublicKey
|
||||
|
||||
|
|
@ -55,17 +54,16 @@ class SqueakPeerServerHandler(object):
|
|||
self.node_settings = node_settings
|
||||
self.config = config
|
||||
|
||||
def handle_get_squeak_bytes(self, squeak_hash_str) -> Tuple[str, bytes]:
|
||||
def handle_get_squeak_bytes(self, squeak_hash_str) -> bytes:
|
||||
"""Return a tuple with the squeak type and the squeak bytes.
|
||||
|
||||
Returns: (str, bytes)
|
||||
Returns: bytes
|
||||
"""
|
||||
squeak_hash = bytes.fromhex(squeak_hash_str)
|
||||
squeak = self.squeak_controller.get_squeak(squeak_hash)
|
||||
if not squeak:
|
||||
raise NotFoundError()
|
||||
squeak_type = 'resqueak' if squeak.is_resqueak else 'squeak'
|
||||
return squeak_type, squeak.serialize()
|
||||
return squeak.serialize()
|
||||
|
||||
def handle_get_secret_key(self, squeak_hash_str) -> bytes:
|
||||
squeak_hash = bytes.fromhex(squeak_hash_str)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue