mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-17 11:49:29 +00:00
Added some better cache handling and error reporting, also added verbose information to model
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
import logging
|
||||||
import urllib, urllib2
|
import urllib, urllib2
|
||||||
from hashlib import sha1
|
from hashlib import sha1
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
@@ -8,7 +9,6 @@ from django.conf import settings
|
|||||||
from eve_proxy.exceptions import *
|
from eve_proxy.exceptions import *
|
||||||
|
|
||||||
# You generally never want to change this unless you have a very good reason.
|
# You generally never want to change this unless you have a very good reason.
|
||||||
|
|
||||||
try:
|
try:
|
||||||
API_URL = getattr(settings, 'EVE_API_URL')
|
API_URL = getattr(settings, 'EVE_API_URL')
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@@ -52,16 +52,19 @@ class CachedDocumentManager(models.Manager):
|
|||||||
the query: userID=1&characterID=xxxxxxxx
|
the query: userID=1&characterID=xxxxxxxx
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
logger = logging.getLogger('eve_proxy.CachedDocument')
|
||||||
|
|
||||||
url = self.construct_url(url_path, params)
|
url = self.construct_url(url_path, params)
|
||||||
doc_key = sha1(url).hexdigest()
|
doc_key = sha1(url).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
doc = super(CachedDocumentManager, self).get_query_set().get(pk=doc_key)
|
doc = super(CachedDocumentManager, self).get_query_set().get(pk=doc_key)
|
||||||
|
created = False
|
||||||
except self.model.DoesNotExist:
|
except self.model.DoesNotExist:
|
||||||
doc = CachedDocument(pk=doc_key, url_path=url)
|
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 = urllib2.Request(url)
|
||||||
req.add_header('CCP-Contact', 'matalok@pleaseignore.com')
|
req.add_header('CCP-Contact', 'matalok@pleaseignore.com')
|
||||||
@@ -71,12 +74,17 @@ class CachedDocumentManager(models.Manager):
|
|||||||
else:
|
else:
|
||||||
conn = urllib2.urlopen(req, timeout=timeout)
|
conn = urllib2.urlopen(req, timeout=timeout)
|
||||||
except urllib2.HTTPError, e:
|
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)
|
raise DocumentRetrievalError(e.code)
|
||||||
except urllib2.URLError, e:
|
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)
|
raise DocumentRetrievalError(e.reason)
|
||||||
|
|
||||||
|
else:
|
||||||
doc.body = unicode(conn.read(), 'utf-8')
|
doc.body = unicode(conn.read(), 'utf-8')
|
||||||
doc.time_retrieved = datetime.utcnow()
|
doc.time_retrieved = datetime.utcnow()
|
||||||
|
|
||||||
@@ -96,6 +104,8 @@ class CachedDocumentManager(models.Manager):
|
|||||||
if not error or not error in ROLLBACK_ERRORS:
|
if not error or not error in ROLLBACK_ERRORS:
|
||||||
doc.save()
|
doc.save()
|
||||||
doc = self.get(pk=doc.pk)
|
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 this is user related, write a log instance
|
||||||
if params and (params.get('userid', None) or params.get('keyid', None)):
|
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.
|
This is a cached XML document from the EVE API.
|
||||||
"""
|
"""
|
||||||
doc_key = models.CharField(max_length=40, primary_key=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(max_length=255)
|
url_path = models.CharField("URL Path", max_length=255, help_text="The full EVE API url path of this document")
|
||||||
body = models.TextField()
|
body = models.TextField("Body", help_text="The raw XML document from the EVE API")
|
||||||
time_retrieved = models.DateTimeField(blank=True, null=True)
|
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)
|
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.
|
# The custom manager handles the querying.
|
||||||
objects = CachedDocumentManager()
|
objects = CachedDocumentManager()
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return u'%s - %s' % (self.doc_key, self.time_retrieved)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = 'Cached Document'
|
verbose_name = 'Cached Document'
|
||||||
verbose_name_plural = 'Cached Documents'
|
verbose_name_plural = 'Cached Documents'
|
||||||
ordering = ['time_retrieved']
|
ordering = ['-time_retrieved']
|
||||||
|
|
||||||
class ApiAccessLog(models.Model):
|
class ApiAccessLog(models.Model):
|
||||||
"""
|
"""
|
||||||
Provides a list of API accesses made by applications or Auth
|
Provides a list of API accesses made by applications or Auth
|
||||||
"""
|
"""
|
||||||
userid = models.IntegerField()
|
userid = models.IntegerField("User ID", help_text="The API User ID related to this log")
|
||||||
service = models.CharField(max_length=255)
|
service = models.CharField("Service Name", max_length=255, help_text="The service name that requested the document")
|
||||||
time_access = models.DateTimeField()
|
time_access = models.DateTimeField("Date/Time Accessed", help_text="The date/time the document was requested")
|
||||||
document = models.CharField(max_length=255)
|
document = models.CharField("Document Path", max_length=255, help_text="The path to the requested document")
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = 'API Access Log'
|
verbose_name = 'API Access Log'
|
||||||
verbose_name_plural = 'API Access Logs'
|
verbose_name_plural = 'API Access Logs'
|
||||||
ordering = ['time_access']
|
ordering = ['-time_access']
|
||||||
|
|||||||
Reference in New Issue
Block a user