Simplify serialization of peer squeak download (#2195)

This commit is contained in:
Jonathan Zernik 2022-05-03 15:39:13 -07:00 committed by GitHub
parent ad779182d7
commit c09ab3f209
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 21 deletions

View file

@ -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()

View file

@ -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):

View file

@ -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)