mirror of
https://github.com/rembo10/headphones.git
synced 2026-05-06 03:39:51 +01:00
Basic api structure in place, removed the static api files - moved to a class in api.py'
This commit is contained in:
@@ -109,7 +109,6 @@ def main():
|
||||
'http_root': headphones.HTTP_ROOT,
|
||||
'http_username': headphones.HTTP_USERNAME,
|
||||
'http_password': headphones.HTTP_PASSWORD,
|
||||
'api': headphones.API_ENABLED,
|
||||
})
|
||||
|
||||
logger.info('Starting Headphones on port: %i' % http_port)
|
||||
|
||||
11
apireference
Normal file
11
apireference
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
General structure:
|
||||
http://localhost:8181 + HTTP_ROOT + /api?apikey=$apikey&cmd=$command
|
||||
|
||||
Optional parameters:
|
||||
format : json, xml
|
||||
|
||||
|
||||
Commands:
|
||||
|
||||
findArtist?name=$artistname&type={album,artist}[&limit=$limit]
|
||||
100
headphones/api.py
Normal file
100
headphones/api.py
Normal file
@@ -0,0 +1,100 @@
|
||||
import headphones
|
||||
|
||||
from headphones import db, mb, logger
|
||||
|
||||
import lib.simplejson as simplejson
|
||||
|
||||
cmd_list = [ 'findArtist']
|
||||
|
||||
class Api(object):
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.apikey = None
|
||||
self.cmd = None
|
||||
self.format = 'json'
|
||||
self.id = None
|
||||
|
||||
self.kwargs = None
|
||||
|
||||
self.rawdata = None
|
||||
self.data = None
|
||||
|
||||
def checkParams(self,*args,**kwargs):
|
||||
|
||||
if not headphones.API_ENABLED:
|
||||
self.data = 'API not enabled'
|
||||
return
|
||||
if not headphones.API_KEY:
|
||||
self.data = 'API key not generated'
|
||||
return
|
||||
if len(headphones.API_KEY) != 32:
|
||||
self.data = 'API key not generated correctly'
|
||||
return
|
||||
|
||||
if 'apikey' not in kwargs:
|
||||
self.data = 'Missing api key'
|
||||
return
|
||||
|
||||
if kwargs['apikey'] != headphones.API_KEY:
|
||||
self.data = 'Incorrect API key'
|
||||
return
|
||||
else:
|
||||
self.apikey = kwargs.pop('apikey')
|
||||
|
||||
if 'cmd' not in kwargs:
|
||||
self.data = 'Missing parameter: cmd'
|
||||
return
|
||||
|
||||
if kwargs['cmd'] not in cmd_list:
|
||||
self.data = 'Unknown command: %s' % kwargs['cmd']
|
||||
return
|
||||
else:
|
||||
self.cmd = kwargs.pop('cmd')
|
||||
|
||||
if 'format' not in kwargs:
|
||||
self.format = 'json'
|
||||
else:
|
||||
if kwargs['format'] not in ['json', 'xml']:
|
||||
self.data = 'Unknown format: %s' % kwargs['format']
|
||||
return
|
||||
else:
|
||||
self.format = kwargs.pop('format')
|
||||
|
||||
self.kwargs = kwargs
|
||||
self.data = 'OK'
|
||||
|
||||
def formatData(self):
|
||||
|
||||
self.data = '%s' % self.data
|
||||
|
||||
def fetchData(self):
|
||||
|
||||
if self.cmd == 'findArtist':
|
||||
self.findArtist(**self.kwargs)
|
||||
|
||||
return simplejson.dumps(self.data)
|
||||
|
||||
def findArtist(self, **kwargs):
|
||||
if 'type' not in kwargs:
|
||||
self.data = 'Missing parameter: type'
|
||||
return
|
||||
if 'name' not in kwargs:
|
||||
self.data = 'Missing parameter: name'
|
||||
return
|
||||
if kwargs['type'] not in ['artist','album']:
|
||||
self.data = 'Incorrect type: %s' % kwargs['type']
|
||||
return
|
||||
if 'limit' in kwargs:
|
||||
limit = kwargs['limit']
|
||||
else:
|
||||
limit=50
|
||||
if kwargs['type'] == 'artist':
|
||||
self.data = mb.findArtist(kwargs['name'], limit)
|
||||
else:
|
||||
self.data = mb.findRelease(kwargs['name'], limit)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -552,3 +552,17 @@ class WebInterface(object):
|
||||
raise cherrypy.HTTPRedirect("extras")
|
||||
|
||||
updateCloud.exposed = True
|
||||
|
||||
def api(self, *args, **kwargs):
|
||||
|
||||
from headphones.api import Api
|
||||
|
||||
a = Api()
|
||||
|
||||
a.checkParams(*args, **kwargs)
|
||||
|
||||
data = a.fetchData()
|
||||
|
||||
return data
|
||||
|
||||
api.exposed = True
|
||||
|
||||
@@ -43,9 +43,6 @@ def initialize(options={}):
|
||||
'tools.staticfile.filename': "images/favicon.ico"
|
||||
}
|
||||
}
|
||||
|
||||
if options['api']:
|
||||
conf['/api'] = {'tools.staticdir.on': True, 'tools.staticdir.filename': "api"}
|
||||
|
||||
if options['http_password'] != "":
|
||||
conf['/'].update({
|
||||
|
||||
Reference in New Issue
Block a user