Skip to content

Commit

Permalink
Merge pull request #2050 from maxammann/master
Browse files Browse the repository at this point in the history
Add 'expand' flag to the web plugin
  • Loading branch information
sampsyo committed Jun 26, 2016
2 parents ce88a78 + cfd70c2 commit 77a869d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
26 changes: 20 additions & 6 deletions beetsplug/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _rep(obj, expand=False):
out = dict(obj)

if isinstance(obj, beets.library.Item):
del out['path']
out['path'] = obj.destination(fragment=True)

# Get the size (in bytes) of the backing file. This is useful
# for the Tomahawk resolver API.
Expand All @@ -55,11 +55,13 @@ def _rep(obj, expand=False):
return out


def json_generator(items, root):
def json_generator(items, root, expand=False):
"""Generator that dumps list of beets Items or Albums as JSON
:param root: root key for JSON
:param items: list of :class:`Item` or :class:`Album` to dump
:param expand: If true every :class:`Album` contains its items in the json
representation
:returns: generator that yields strings
"""
yield '{"%s":[' % root
Expand All @@ -69,10 +71,16 @@ def json_generator(items, root):
first = False
else:
yield ','
yield json.dumps(_rep(item))
yield json.dumps(_rep(item, expand=expand))
yield ']}'


def is_expand():
"""Returns whether the current request is for an expanded response."""

return flask.request.args.get('expand') is not None


def resource(name):
"""Decorates a function to handle RESTful HTTP requests for a resource.
"""
Expand All @@ -82,7 +90,7 @@ def responder(ids):
entities = [entity for entity in entities if entity]

if len(entities) == 1:
return flask.jsonify(_rep(entities[0]))
return flask.jsonify(_rep(entities[0], expand=is_expand()))
elif entities:
return app.response_class(
json_generator(entities, root=name),
Expand All @@ -101,7 +109,10 @@ def resource_query(name):
def make_responder(query_func):
def responder(queries):
return app.response_class(
json_generator(query_func(queries), root='results'),
json_generator(
query_func(queries),
root='results', expand=is_expand()
),
mimetype='application/json'
)
responder.__name__ = 'query_{0}'.format(name)
Expand All @@ -116,7 +127,7 @@ def resource_list(name):
def make_responder(list_all):
def responder():
return app.response_class(
json_generator(list_all(), root=name),
json_generator(list_all(), root=name, expand=is_expand()),
mimetype='application/json'
)
responder.__name__ = 'all_{0}'.format(name)
Expand Down Expand Up @@ -310,6 +321,9 @@ def func(lib, opts, args):
self.config['port'] = int(args.pop(0))

app.config['lib'] = lib
# Normalizes json output
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False

# Enable CORS if required.
if self.config['cors']:
self._log.info(u'Enabling CORS with origin: {0}',
Expand Down
9 changes: 8 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ Changelog
1.3.20 (in development)
-----------------------

Changelog goes here!
New features:

* :doc:`/plugins/web`: Added an option to show the items of an album and a
'path' tag to the json outpu of a file which shows the relative path to the
file. :bug:`2050`

Other fixes:

* :doc:`/plugins/web`: Normalized the json output

1.3.19 (June 25, 2016)
----------------------
Expand Down
1 change: 1 addition & 0 deletions docs/plugins/web.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ The interface and response format is similar to the item API, except replacing
the encapsulation key ``"items"`` with ``"albums"`` when requesting ``/album/``
or ``/album/5,7``. In addition we can request the cover art of an album with
``GET /album/5/art``.
You can also add the '?expand' flag to get the individual items of an album.


``GET /stats``
Expand Down

0 comments on commit 77a869d

Please sign in to comment.