rearrange slightly
This commit is contained in:
parent
e5fcc6a8c8
commit
5e1690cad8
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user