Permalink
Browse files

improved the logging situation throughout the web app and api. also a…

…dded a library called responses that allows for api calls to be made from within the web app as per #19
  • Loading branch information...
1 parent 0a200c0 commit 5f704e4718d9d02e6d2b0c3291d8aeb8e577901d @MusikPolice committed Nov 16, 2012
Showing with 60 additions and 5 deletions.
  1. +1 −1 musik.py
  2. +14 −0 musik/web/api.py
  3. +44 −4 musik/web/application.py
  4. +1 −0 requirements.txt
View
@@ -62,7 +62,7 @@ def cleanup(signum=None, frame=None):
# this is a blocking call
log.info(u'Starting Web App')
- app = musik.web.application.MusikWebApplication(log=log, threads=threads)
+ app = musik.web.application.MusikWebApplication(threads=threads)
port = int(os.environ.get('PORT', '8080'))
app.start(port=port)
View
@@ -4,6 +4,7 @@
import cherrypy
+from musik import initLogging
from musik.db import Album, Artist, ImportTask, Track, Disc
@@ -50,8 +51,13 @@ def status(self):
# these pairs are assembled into an SQL query. Each term is combined with the AND operator.
# unknown <tag> elements are ignored.
class API:
+ log = None
importmedia = Import()
+ def __init__(self):
+ self.log = initLogging(__name__)
+
+
@cherrypy.expose
def default(self, *params):
@@ -81,6 +87,8 @@ def queryAlbums(self, params):
passed on the url string.
Returns the results of the query sorted by title_sort property
"""
+ self.log.info(u'queryAlbums called with params %s' % unicode(params))
+
q = cherrypy.request.db.query(Album)
for d in params:
@@ -127,6 +135,8 @@ def queryDiscs(self, params):
Returns the results of the query sorted by id property
TODO: sort by album
"""
+ self.log.info(u'queryDiscs called with params %s' % unicode(params))
+
q = cherrypy.request.db.query(Disc)
for d in params:
@@ -156,6 +166,8 @@ def queryArtists(self, params):
passed on the url string.
Returns the results of the query sorted by name_sort property
"""
+ self.log.info(u'queryArtists called with params %s' % unicode(params))
+
q = cherrypy.request.db.query(Artist)
for d in params:
@@ -183,6 +195,8 @@ def queryTracks(self, params):
passed on the url string.
Returns the results of the query sorted by title_sort property
"""
+ self.log.info(u'queryTracks called with params %s' % unicode(params))
+
q = cherrypy.request.db.query(Track)
for d in params:
View
@@ -1,3 +1,4 @@
+import json
import sys
import os
@@ -7,8 +8,11 @@
from mako.template import Template
from mako.lookup import TemplateLookup
+import requests
+
from sqlalchemy.orm import scoped_session, sessionmaker
+from musik import initLogging
from musik.db import DatabaseWrapper
from musik.web import api
@@ -64,8 +68,27 @@ def commit_transaction(self):
# defines the web application that is the default client
class Musik:
+ log = None
api = api.API()
+ def __init__(self):
+ self.log = initLogging(__name__)
+
+
+ def _api_request(self, url):
+ self.log.info(u'_api_request was called with url %s' % url)
+
+ r = requests.get(url)
+
+ if r.status_code != 200:
+ log.error(u'_api_request to url %s returned status code %d' % (url, int(r.status_code)))
+ return False
+ elif r.headers['content-type'] != 'application/json':
+ log.error(u'_api_request to url %s returned an unsupported content-type %s' % (url, r.headers['content-type']))
+ return False
+
+ return json.load(r.content)
+
def _render(self, template_names, **kwargs):
"""Renders the specified template.
@@ -115,6 +138,8 @@ def index(self):
"""Renders the index.html template along with
a page header and footer.
"""
+ self.log.info(u'index was called')
+
return self._render_page("index.html", **{
"title": "Home",
})
@@ -124,40 +149,55 @@ def index(self):
def main(self):
"""Renders the index template without a header or footer.
"""
+ self.log.info(u'main was called')
+
return self._render("index.html")
@cherrypy.expose
def albums(self, id=None):
"""Renders the albums template.
"""
+ if id == None:
+ self.log.info(u'albums was called with no id')
+ else:
+ self.log.info(u'albums was called with id %d' % int(id))
+
return self._render("albums.html", **{
"js_appends": ['jquery.listnav.min-2.1.js'],
})
@cherrypy.expose
- def artists(self):
+ def artists(self, id=None):
"""Renders the artists template.
"""
+ if id == None:
+ self.log.info(u'artists was called with no id')
+ else:
+ self.log.info(u'artists was called with id %d' % int(id))
+
return self._render("artists.html", **{
"js_appends": ['jquery.listnav.min-2.1.js'],
})
@cherrypy.expose
def importmedia(self):
+ self.log.info(u'importmedia was called')
+
return self._render("importmedia.html", **{
"js_appends": ['importmedia.js'],
})
# starts the web app. this call will block until the server goes down
class MusikWebApplication:
- log = threads = None
+ log = None
+ threads = None
- def __init__(self, log, threads):
- self.log = log
+ def __init__(self, threads):
+ self.log = initLogging(__name__)
self.threads = threads
# Subscribe specifically to the 'stop' method passed by cherrypy.
View
@@ -5,4 +5,5 @@ SQLAlchemy==0.7.8
argparse==1.2.1
distribute==0.6.27
mutagen==1.20
+requests==0.14.2
wsgiref==0.1.2

0 comments on commit 5f704e4

Please sign in to comment.