change channel-allocation web API (incompatibly)

This will make it easier to manage channel allocation later, when we
switch to a database.
This commit is contained in:
Brian Warner 2015-04-10 22:03:08 -05:00
parent f7f05b21b8
commit 790ae9a0dc
2 changed files with 15 additions and 14 deletions

View File

@ -40,13 +40,12 @@ class ReceiverWrongPasswordError(WrongPasswordError):
"""
# relay URLs are:
#
# POST /allocate -> {channel-id: INT}
# GET /list -> {channel-ids: [INT..]}
# POST /allocate/SIDE -> {channel-id: INT}
# these return all messages for CHANNEL-ID= and MSGNUM= but SIDE!= :
# POST /CHANNEL-ID/SIDE/post/MSGNUM {message: STR} -> {messages: [STR..]}
# POST /CHANNEL-ID/SIDE/poll/MSGNUM -> {messages: [STR..]}
# GET /CHANNEL-ID/SIDE/poll/MSGNUM (eventsource) -> STR, STR, ..
#
# POST /CHANNEL-ID/SIDE/deallocate -> waiting | deleted
class EventSourceFollower:
@ -148,7 +147,7 @@ class Common:
return msgs
def _allocate(self):
r = requests.post(self.relay + "allocate")
r = requests.post(self.relay + "allocate/%s" % self.side)
r.raise_for_status()
data = r.json()
if "welcome" in data:

View File

@ -57,7 +57,7 @@ WELCOME = {
# relay URLs are:
# GET /list -> {channel-ids: [INT..]}
# POST /allocate -> {channel-id: INT}
# POST /allocate/SIDE -> {channel-id: INT}
# these return all messages for CHANNEL-ID= and MSGNUM= but SIDE!= :
# POST /CHANNEL-ID/SIDE/post/MSGNUM {message: STR} -> {messages: [STR..]}
# POST /CHANNEL-ID/SIDE/poll/MSGNUM -> {messages: [STR..]}
@ -143,14 +143,20 @@ class Channel(resource.Resource):
return json.dumps({"welcome": WELCOME,
"messages": other_messages})+"\n"
class Allocated(resource.Resource):
def __init__(self, channel_id):
class Allocator(resource.Resource):
isLeaf = True
def __init__(self, relay):
resource.Resource.__init__(self)
self.channel_id = channel_id
self.relay = relay
def render_POST(self, request):
side = request.postpath[0]
channel_id = self.relay.allocate_channel_id()
self.relay.channels[channel_id] = Channel(channel_id, self.relay)
log.msg("allocated #%d, now have %d channels" %
(channel_id, len(self.relay.channels)))
request.setHeader("content-type", "application/json; charset=utf-8")
return json.dumps({"welcome": WELCOME,
"channel-id": self.channel_id})+"\n"
"channel-id": channel_id})+"\n"
class ChannelList(resource.Resource):
def __init__(self, channel_ids):
@ -191,11 +197,7 @@ class Relay(resource.Resource):
def getChild(self, path, request):
if path == "allocate":
channel_id = self.allocate_channel_id()
self.channels[channel_id] = Channel(channel_id, self)
log.msg("allocated #%d, now have %d channels" %
(channel_id, len(self.channels)))
return Allocated(channel_id)
return Allocator(self)
if path == "list":
channel_ids = sorted(self.channels.keys())
return ChannelList(channel_ids)