FEATURE: Add register API endpoint

This commit is contained in:
Dominique Barton 2019-02-20 23:56:09 +01:00
parent 0a76fbdcd6
commit ed18ad349b
2 changed files with 49 additions and 7 deletions

View file

@ -10,12 +10,13 @@ import os
from mopidy import config, ext
from .frontend import PummeluffFrontend
from .web import LatestScanHandler
from .web import LatestHandler, RegisterHandler
def app_factory(config, core):
return [
('/latest/', LatestScanHandler, {'core': core})
('/latest/', LatestHandler, {'core': core}),
('/register/', RegisterHandler, {'core': core}),
]

View file

@ -6,7 +6,8 @@ Python module for Mopidy Pummeluff web classes.
from __future__ import absolute_import, unicode_literals
__all__ = (
'LatestScanHandler',
'LatestHandler',
'RegisterHandler',
)
from json import dumps
@ -15,11 +16,12 @@ from logging import getLogger
from tornado.web import RequestHandler
from .frontend import CardReader
from .cards import Card
LOGGER = getLogger(__name__)
class LatestScanHandler(RequestHandler):
class LatestHandler(RequestHandler):
'''
Request handler which returns the latest scanned card.
'''
@ -35,12 +37,51 @@ class LatestScanHandler(RequestHandler):
def get(self):
'''
Handle GET request, which will simply respond with the latest scanned
card.
Handle GET request.
'''
latest = CardReader.latest
LOGGER.debug('Returning latest card with UID %s', latest.get('uid'))
data = {'success': True}
data.update(latest)
self.set_header('Content-type', 'application/json')
self.write(dumps(latest))
self.write(dumps(data))
class RegisterHandler(RequestHandler):
'''
Request handler which registers an RFID card in the registry.
'''
last_scan = {}
def initialize(self, core):
'''
Initialize request handler with Mopidy core.
:param mopidy.core.Core mopidy_core: The mopidy core instance
'''
self.core = core
def post(self):
'''
Handle POST request.
'''
card = Card.register(
uid=self.get_argument('uid'),
parameter=self.get_argument('parameter'),
card_type=self.get_argument('type')
)
self.set_header('Content-type', 'application/json')
self.write({
'success': True,
'card': str(card)
})
def put(self):
'''
Handle PUT request.
'''
self.post()