Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:ivar http_status: The HTTP status code returned
:ivar uri: The URI queried
"""
def __init__(self, message, http_status=None, uri=None):
super(HTTPError, self).__init__(message)
self.http_status = http_status
self.uri = uri
class InvalidRequestError(GeoIP2Error):
"""The request was invalid."""
class OutOfQueriesError(GeoIP2Error):
"""Your account is out of funds for the service queried."""
class PermissionRequiredError(GeoIP2Error):
"""Your account does not have permission to access this service."""
def _handle_success(self, response, uri):
try:
return response.json()
except ValueError as ex:
raise GeoIP2Error(
'Received a 200 response for %(uri)s'
' but could not decode the response as '
'JSON: ' % locals() + ', '.join(ex.args), 200, uri)
This class represents a generic error. It extends :py:exc:`RuntimeError`
and does not add any additional attributes.
"""
class AddressNotFoundError(GeoIP2Error):
"""The address you were looking up was not found."""
class AuthenticationError(GeoIP2Error):
"""There was a problem authenticating the request."""
class HTTPError(GeoIP2Error):
"""There was an error when making your HTTP request.
This class represents an HTTP transport error. It extends
:py:exc:`GeoIP2Error` and adds attributes of its own.
:ivar http_status: The HTTP status code returned
:ivar uri: The URI queried
"""
def __init__(self, message, http_status=None, uri=None):
super(HTTPError, self).__init__(message)
self.http_status = http_status
self.uri = uri
class InvalidRequestError(GeoIP2Error):
def get_country_code(ip_address):
if geoip_reader and ip_address:
try:
return geoip_reader.country(ip_address).country.iso_code
except AddressNotFoundError:
pass
except GeoIP2Error as exc:
log.warning(exc)
pass
return None
device_type = self.model.BOT
else:
device_type = self.model.UNKNOWN
city = {}
# Get the IP address and so the geographical info, if available.
ip_address, _ = get_client_ip(request) or ''
if not ip_address:
logger.debug(
'Could not determine IP address for request %s', request)
else:
geo = GeoIP2()
try:
city = geo.city(ip_address)
except (GeoIP2Error, GeoIP2Exception):
logger.exception(
'Unable to determine geolocation for address %s',
ip_address
)
tracker = self.model.objects.create(
content_object=content_object,
ip_address=ip_address,
ip_country=city.get('country_code', '') or '',
ip_region=city.get('region', '') or '',
ip_city=city.get('city', '') or '',
referrer=request.META.get('HTTP_REFERER', ''),
device_type=device_type,
device=request.user_agent.device.family,
browser=request.user_agent.browser.family[:30],
browser_version=request.user_agent.browser.version_string,
"""
def __init__(self, message, http_status=None, uri=None):
super(HTTPError, self).__init__(message)
self.http_status = http_status
self.uri = uri
class InvalidRequestError(GeoIP2Error):
"""The request was invalid."""
class OutOfQueriesError(GeoIP2Error):
"""Your account is out of funds for the service queried."""
class PermissionRequiredError(GeoIP2Error):
"""Your account does not have permission to access this service."""