From 5e1690cad887706009c0df1ef868f1a72b658d79 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Fri, 24 Jul 2015 16:22:02 -0700 Subject: [PATCH] rearrange slightly --- src/wormhole/blocking/transcribe.py | 24 ++++++++++---------- src/wormhole/twisted/transcribe.py | 34 ++++++++++++++--------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/wormhole/blocking/transcribe.py b/src/wormhole/blocking/transcribe.py index 1ca1264..f19c18c 100644 --- a/src/wormhole/blocking/transcribe.py +++ b/src/wormhole/blocking/transcribe.py @@ -87,6 +87,13 @@ class Wormhole: if "error" in welcome: raise ServerError(welcome["error"], self.relay) + def _post_data(self, data): + post_data = json.dumps({"message": hexlify(data).decode("ascii")}) + r = requests.post(self._url("post", "data"), data=post_data) + r.raise_for_status() + other_msgs = r.json()["messages"] + return other_msgs + def _get_messages(self, old_msgs, verb, msgnum): # For now, server errors cause the client to fail. TODO: don't. This # will require changing the client to re-post messages when the @@ -113,6 +120,11 @@ class Wormhole: f.close() return msgs + def _get_data(self, other_msgs): + msgs = self._get_messages(other_msgs, "poll", "data") + data = unhexlify(msgs[0].encode("ascii")) + return data + def _allocate_channel(self): r = requests.post(self.relay + "allocate/%s" % self.side) r.raise_for_status() @@ -222,18 +234,6 @@ class Wormhole: data = box.decrypt(encrypted) return data - def _post_data(self, data): - post_data = json.dumps({"message": hexlify(data).decode("ascii")}) - r = requests.post(self._url("post", "data"), data=post_data) - r.raise_for_status() - other_msgs = r.json()["messages"] - return other_msgs - - def _get_data(self, other_msgs): - msgs = self._get_messages(other_msgs, "poll", "data") - data = unhexlify(msgs[0].encode("ascii")) - return data - def _deallocate(self): r = requests.post(self._url("deallocate")) r.raise_for_status() diff --git a/src/wormhole/twisted/transcribe.py b/src/wormhole/twisted/transcribe.py index a50672e..58f60d8 100644 --- a/src/wormhole/twisted/transcribe.py +++ b/src/wormhole/twisted/transcribe.py @@ -67,6 +67,23 @@ class SymmetricWormhole: d.addCallback(_got_channel_id) return d + def _post_json(self, url, post_json=None): + # TODO: retry on failure, with exponential backoff. We're guarding + # against the rendezvous server being temporarily offline. + p = None + if post_json: + data = json.dumps(post_json).encode("utf-8") + p = DataProducer(data) + d = self.agent.request("POST", url, bodyProducer=p) + def _check_error(resp): + if resp.code != 200: + raise web_error.Error(resp.code, resp.phrase) + return resp + d.addCallback(_check_error) + d.addCallback(web_client.readBody) + d.addCallback(lambda data: json.loads(data)) + return d + def _allocate_channel(self): url = self.relay + "allocate/%s" % self.side d = self._post_json(url) @@ -155,23 +172,6 @@ class SymmetricWormhole: url += "/" + msgnum return url - def _post_json(self, url, post_json=None): - # TODO: retry on failure, with exponential backoff. We're guarding - # against the rendezvous server being temporarily offline. - p = None - if post_json: - data = json.dumps(post_json).encode("utf-8") - p = DataProducer(data) - d = self.agent.request("POST", url, bodyProducer=p) - def _check_error(resp): - if resp.code != 200: - raise web_error.Error(resp.code, resp.phrase) - return resp - d.addCallback(_check_error) - d.addCallback(web_client.readBody) - d.addCallback(lambda data: json.loads(data)) - return d - def _get_messages(self, old_msgs, verb, msgnum): # fire with a list of messages that match verb/msgnum, which either # came from old_msgs, or from an EventSource that we attached to the