# Path: hyperadmin/apirequests.py
# class HTTPAPIRequest(APIRequest):
#     """
#     Represents an API Request spawned from a Django HTTP Request
#     """
# 
#     get_to_meta_map = {
#         '_HTTP_ACCEPT':'HTTP_ACCEPT',
#         '_CONTENT_TYPE':'CONTENT_TYPE',
#     }
# 
#     def __init__(self, request, **kwargs):
#         self.request = request
#         kwargs.setdefault('path', request.path)
#         super(HTTPAPIRequest, self).__init__(**kwargs)
#         self.populate_session_data_from_request(request)
# 
#     @property
#     def payload(self):
#         if not hasattr(self, '_payload'):
#             media_type = self.get_request_media_type()
#             self._payload = media_type.deserialize()
#         return self._payload
# 
#     @property
#     def method(self):
#         return self.request.method
# 
#     def get_django_request(self):
#         return self.request
# 
#     def get_full_path(self):
#         return self.request.get_full_path()
# 
#     @property
#     def user(self):
#         return self.session_state.get('auth', AnonymousUser())
# 
#     @property
#     def params(self):
#         if not hasattr(self, '_params'):
#             self._params = self.request.GET.copy()
#         return self._params
# 
#     def get_session_data_from_request(self, request):
#         #TODO consult site object
#         data = {'endpoints': {},
#                 'resources': {},
#                 'request': request,
#                 'meta': self.patched_meta(request),
#                 'extra_get_params': self.get_passthrough_params(request), }
#         if hasattr(request, 'user'):
#             data['auth'] = request.user
#         return data
# 
#     def populate_session_data_from_request(self, request):
#         data = self.get_session_data_from_request(request)
#         self.session_state.update(data)
#         #TODO set response type & request type
# 
#     def patched_meta(self, request):
#         meta = dict(request.META)
#         for src, dst in self.get_to_meta_map.iteritems():
#             if src in request.GET:
#                 val = request.GET[src]
#                 meta[dst] = val
#         return meta
# 
#     def get_passthrough_params(self, request):
#         pass_through_params = dict()
#         for src, dst in self.get_to_meta_map.iteritems():
#             if src in request.GET:
#                 pass_through_params[src] = request.GET[src]
#         return pass_through_params
# 
# Path: hyperadmin/contrib/apikey/common.py
# def authenticate(key):
#     try:
#         entry = ApiKey.objects.get(key=key, active=True)
#     except ApiKey.DoesNotExist:
#         return None
#     return entry.user


from hyperadmin.apirequests import HTTPAPIRequest
from hyperadmin.contrib.apikey.common import authenticate


HTTP_HEADER = 'API_KEY'
GET_VARIABLE = '_API_KEY'

class APIKeyMixin(object):
    def get_session_data_from_request(self, request):
        data = super(APIKeyMixin, self).get_session_data_from_request(request)
        if HTTP_HEADER in request.META:
            key = request.META[HTTP_HEADER]
            user = authenticate(key)
            if user:
                data['auth'] = user
        if GET_VARIABLE in request.GET:
            key = request.GET[HTTP_HEADER]

# Path: hyperadmin/apirequests.py
# class HTTPAPIRequest(APIRequest):
#     """
#     Represents an API Request spawned from a Django HTTP Request
#     """
# 
#     get_to_meta_map = {
#         '_HTTP_ACCEPT':'HTTP_ACCEPT',
#         '_CONTENT_TYPE':'CONTENT_TYPE',
#     }
# 
#     def __init__(self, request, **kwargs):
#         self.request = request
#         kwargs.setdefault('path', request.path)
#         super(HTTPAPIRequest, self).__init__(**kwargs)
#         self.populate_session_data_from_request(request)
# 
#     @property
#     def payload(self):
#         if not hasattr(self, '_payload'):
#             media_type = self.get_request_media_type()
#             self._payload = media_type.deserialize()
#         return self._payload
# 
#     @property
#     def method(self):
#         return self.request.method
# 
#     def get_django_request(self):
#         return self.request
# 
#     def get_full_path(self):
#         return self.request.get_full_path()
# 
#     @property
#     def user(self):
#         return self.session_state.get('auth', AnonymousUser())
# 
#     @property
#     def params(self):
#         if not hasattr(self, '_params'):
#             self._params = self.request.GET.copy()
#         return self._params
# 
#     def get_session_data_from_request(self, request):
#         #TODO consult site object
#         data = {'endpoints': {},
#                 'resources': {},
#                 'request': request,
#                 'meta': self.patched_meta(request),
#                 'extra_get_params': self.get_passthrough_params(request), }
#         if hasattr(request, 'user'):
#             data['auth'] = request.user
#         return data
# 
#     def populate_session_data_from_request(self, request):
#         data = self.get_session_data_from_request(request)
#         self.session_state.update(data)
#         #TODO set response type & request type
# 
#     def patched_meta(self, request):
#         meta = dict(request.META)
#         for src, dst in self.get_to_meta_map.iteritems():
#             if src in request.GET:
#                 val = request.GET[src]
#                 meta[dst] = val
#         return meta
# 
#     def get_passthrough_params(self, request):
#         pass_through_params = dict()
#         for src, dst in self.get_to_meta_map.iteritems():
#             if src in request.GET:
#                 pass_through_params[src] = request.GET[src]
#         return pass_through_params
# 
# Path: hyperadmin/contrib/apikey/common.py
# def authenticate(key):
#     try:
#         entry = ApiKey.objects.get(key=key, active=True)
#     except ApiKey.DoesNotExist:
#         return None
#     return entry.user


# Path: hyperadmin/contrib/apikey/apirequests.py
from hyperadmin.apirequests import HTTPAPIRequest
from hyperadmin.contrib.apikey.common import authenticate



HTTP_HEADER = 'API_KEY'
GET_VARIABLE = '_API_KEY'

class APIKeyMixin(object):
    def get_session_data_from_request(self, request):
        data = super(APIKeyMixin, self).get_session_data_from_request(request)
        if HTTP_HEADER in request.META:
            key = request.META[HTTP_HEADER]
            user = authenticate(key)
            if user:
                data['auth'] = user
        if GET_VARIABLE in request.GET:
            key = request.GET[HTTP_HEADER]

            user = authenticate(key)