Added some better cache handling and error reporting, also added verbose information to model

This commit is contained in:
2011-06-16 12:38:22 +01:00
parent ec0e08573b
commit 1ddd7ba0e5

View File

@@ -1,4 +1,5 @@
import sys
import logging
import urllib, urllib2
from hashlib import sha1
from datetime import datetime, timedelta
@@ -8,7 +9,6 @@ from django.conf import settings
from eve_proxy.exceptions import *
# You generally never want to change this unless you have a very good reason.
try:
API_URL = getattr(settings, 'EVE_API_URL')
except AttributeError:
@@ -52,16 +52,19 @@ class CachedDocumentManager(models.Manager):
the query: userID=1&characterID=xxxxxxxx
"""
logger = logging.getLogger('eve_proxy.CachedDocument')
url = self.construct_url(url_path, params)
doc_key = sha1(url).hexdigest()
try:
doc = super(CachedDocumentManager, self).get_query_set().get(pk=doc_key)
created = False
except self.model.DoesNotExist:
doc = CachedDocument(pk=doc_key, url_path=url)
created = True
if not doc.cached_until or datetime.utcnow() > doc.cached_until or no_cache:
if created or not doc.cached_until or datetime.utcnow() > doc.cached_until or no_cache:
req = urllib2.Request(url)
req.add_header('CCP-Contact', 'matalok@pleaseignore.com')
@@ -71,12 +74,17 @@ class CachedDocumentManager(models.Manager):
else:
conn = urllib2.urlopen(req, timeout=timeout)
except urllib2.HTTPError, e:
print "HTTP Error Code: %s" % e.code
if not created:
pass
logger.error('HTTP Error Code: %s' % e.code, exc_info=sys.exc_info(), extra={'data': {'api-url': url}})
raise DocumentRetrievalError(e.code)
except urllib2.URLError, e:
print "URLError: %s" % e.reason
if not created:
pass
logger.error('URL Error: %s' % e, exc_info=sys.exc_info(), extra={'data': {'api-url': url}})
raise DocumentRetrievalError(e.reason)
else:
doc.body = unicode(conn.read(), 'utf-8')
doc.time_retrieved = datetime.utcnow()
@@ -96,6 +104,8 @@ class CachedDocumentManager(models.Manager):
if not error or not error in ROLLBACK_ERRORS:
doc.save()
doc = self.get(pk=doc.pk)
elif error in ROLLBACK_ERRORS and not created:
doc = self.get(pk=doc.pk)
# If this is user related, write a log instance
if params and (params.get('userid', None) or params.get('keyid', None)):
@@ -116,30 +126,33 @@ class CachedDocument(models.Model):
"""
This is a cached XML document from the EVE API.
"""
doc_key = models.CharField(max_length=40, primary_key=True)
url_path = models.CharField(max_length=255)
body = models.TextField()
time_retrieved = models.DateTimeField(blank=True, null=True)
cached_until = models.DateTimeField(blank=True, null=True)
doc_key = models.CharField("Document Key", max_length=40, primary_key=True, help_text="A unique SHA1 hash of the request")
url_path = models.CharField("URL Path", max_length=255, help_text="The full EVE API url path of this document")
body = models.TextField("Body", help_text="The raw XML document from the EVE API")
time_retrieved = models.DateTimeField(blank=True, null=True, help_text="UTC date/time of when the document was retreived from the EVE API")
cached_until = models.DateTimeField(blank=True, null=True, help_text="UTC date/time specifying when this document should be cached until")
# The custom manager handles the querying.
objects = CachedDocumentManager()
def __unicode__(self):
return u'%s - %s' % (self.doc_key, self.time_retrieved)
class Meta:
verbose_name = 'Cached Document'
verbose_name_plural = 'Cached Documents'
ordering = ['time_retrieved']
ordering = ['-time_retrieved']
class ApiAccessLog(models.Model):
"""
Provides a list of API accesses made by applications or Auth
"""
userid = models.IntegerField()
service = models.CharField(max_length=255)
time_access = models.DateTimeField()
document = models.CharField(max_length=255)
userid = models.IntegerField("User ID", help_text="The API User ID related to this log")
service = models.CharField("Service Name", max_length=255, help_text="The service name that requested the document")
time_access = models.DateTimeField("Date/Time Accessed", help_text="The date/time the document was requested")
document = models.CharField("Document Path", max_length=255, help_text="The path to the requested document")
class Meta:
verbose_name = 'API Access Log'
verbose_name_plural = 'API Access Logs'
ordering = ['time_access']
ordering = ['-time_access']