add list-channels API to relay, use it in receiver

This commit is contained in:
Brian Warner 2015-02-14 18:45:29 -08:00
parent e2e2206159
commit 0474cc18d5
3 changed files with 21 additions and 2 deletions

View File

@ -1,12 +1,14 @@
from __future__ import print_function
import time, json
from wormhole.transcribe import Receiver
from wormhole.transcribe import Receiver, list_channels
from wormhole.codes import input_code_with_completion
APPID = "lothar.com/wormhole/text-xfer"
# we're receiving
code = input_code_with_completion("Enter receive-text wormhole code: ", [])
channel_ids = list_channels()
code = input_code_with_completion("Enter receive-text wormhole code: ",
channel_ids)
start = time.time()
data = json.dumps({"message": "ok"}).encode("utf-8")
r = Receiver(APPID, data, code)

View File

@ -68,6 +68,13 @@ class Allocated(resource.Resource):
request.setHeader("content-type", "application/json; charset=utf-8")
return json.dumps({"channel-id": self.channel_id})+"\n"
class ChannelList(resource.Resource):
def __init__(self, channel_ids):
resource.Resource.__init__(self)
self.channel_ids = channel_ids
def render_GET(self, request):
request.setHeader("content-type", "application/json; charset=utf-8")
return json.dumps({"channel-ids": self.channel_ids})+"\n"
class Relay(resource.Resource):
def __init__(self):
@ -85,6 +92,9 @@ class Relay(resource.Resource):
log.msg("allocated %d, now have %d channels" %
(channel_id, len(self.channels)))
return Allocated(channel_id)
if path == "list":
channel_ids = sorted(self.channels.keys())
return ChannelList(channel_ids)
if not re.search(r'^\d+$', path):
return resource.ErrorPage(http.BAD_REQUEST,
"invalid channel id",

View File

@ -115,6 +115,13 @@ class Initiator(Common):
self._deallocate()
return inbound_data
def list_channels(relay=RELAY):
r = requests.get(relay + "list")
r.raise_for_status()
channel_ids = r.json()["channel-ids"]
return channel_ids
class Receiver(Common):
def __init__(self, appid, data, code, relay=RELAY):
self.appid = appid