Skip to content

Commit

Permalink
streamline API responses (#9)
Browse files Browse the repository at this point in the history
The goal of this is to make the API more compliant with REST best practices.
This commit adds the API version to the request and removes it from the response.
The endpoint locations have been unified (i.e. always `/streams/` instead of
sometimes `/stream/` and sometimes `/streams/`.
Responses to requests that do not contain a stream name no longer contain urls.
The app has been removed from requests for stream information.
multiple urls can now be returned. I.e. that we now return both flv and rtmp link.

Co-authored-by: Jonas <[email protected]>
  • Loading branch information
qamodi and linuro authored Apr 5, 2021
1 parent 7ffbccb commit 0fa0fa7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
21 changes: 11 additions & 10 deletions frontend/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@

from zomstream import Zomstream

api_version = "0.1"
api_version = "0.2"
api_base = "/api/v" + api_version

api = flask.Blueprint('api', __name__)
zomstream = Zomstream()

def construct_response(streams):
# Expecting a JSON-serializable list as an argument
# Returning a JSON string with the API response

# Add Version String
r = {"version":api_version,
"streams":streams}
r = {"streams":streams}
return flask.jsonify(r)


@api.route("/api/stream/", methods = ['GET'])
@api.route("/api/streams/", methods = ['GET'])
@api.route(api_base + "/streams/", methods = ['GET'])
def api_list_streams():
return construct_response(zomstream.getStreams())
streams = []
for stream in zomstream.getStreamNames():
streams.append({'app':stream[0],'name':stream[1]})
return construct_response(streams)


@api.route("/api/stream/<app_name>/<stream_name>/", methods = ['GET'])
def api_stream(app_name, stream_name):
@api.route(api_base + "/streams/<stream_name>/", methods = ['GET'])
def api_stream( stream_name):
# Filter for streams with 'name' == stream_name
stream = list(filter(lambda stream: stream['name'] == stream_name, zomstream.getStreams()))
return construct_response(stream)
39 changes: 28 additions & 11 deletions frontend/app/zomstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@


class Stream:
def __init__(self, url, name, streamType, app):
self.url = url
self.name = name
self.streamType = streamType
self.app = app
def __init__(self, app, name, urls):
self.name = name # String
self.app = app # String
self.urls = urls # List of Dictionaries with the keys url and type


class Zomstream:
Expand All @@ -24,6 +23,7 @@ def __init__(self):
print('missing configuration.')
sys.exit(1)
self.streamnames = []

def getStreamNames(self):
self.streamnames = []
# get data from the streaming server
Expand All @@ -49,11 +49,28 @@ def getStreamNames(self):
def getStreams(self):
streams = []
for streamName in self.getStreamNames():
stream_url = '%s://%s/flv?app=%s&stream=%s' % (
self.configuration['web_proto'],
self.configuration['base_url'],
streamName[0],
streamName[1])
stream = Stream(url=stream_url, app=streamName[0], name=streamName[1], streamType='http_flv')
urls = []
app = streamName[0]
name = streamName[1]

flv_url = self.getFlvUrl (app,name)
rtmp_url = self.getRtmpUrl(app,name)

urls.append({'url': flv_url, 'type':'http_flv'})
urls.append({'url': rtmp_url,'type':'rtmp'})

stream = Stream(app=app, name=name, urls=urls)
streams.append(stream.__dict__)
return streams

def getFlvUrl(self,app_name,stream_name):
return '%s://%s/flv?app=%s&stream=%s' % (
self.configuration['web_proto'],
self.configuration['base_url'],
app_name,
stream_name)
def getRtmpUrl(self,app_name,stream_name):
return "rtmp://%s/%s/%s" % (
self.configuration['rtmp_base'],
app_name,
stream_name)

0 comments on commit 0fa0fa7

Please sign in to comment.