# Path: rowan/utils/subdicts.py
# class ChangeTrackingDict(dict):
#     """
#     A dictionary subclass that has a field for tracking if the
#     dictionary is dirty and needs saving.
#     """
#     def __init__(self, *args, **kws):
#         super(ChangeTrackingDict, self).__init__(*args, **kws)
#         self.dirty = False
# 
#     def __setitem__(self, key, value):
#         """
#         Sets a value associated with a key, automatically updating the
#         record of whether this dictionary is dirty.
#         """
#         if self.dirty:
#             # Quick version if we're already dirty.
#             super(ChangeTrackingDict, self).__setitem__(key, value)
#         else:
#             old_value = self[key]
#             super(ChangeTrackingDict, self).__setitem__(key, value)
#             self.dirty = (old_value != value)
# 
#     def clean(self):
#         """
#         Notifies this dictionary that its data should be clean
#         (i.e. saved).
#         """
#         self.dirty = False


from time import time
from rowan.utils.subdicts import ChangeTrackingDict
import urlparse
import uuid
import base
import rowan.http as http
    """
    Wraps another controller, making sure any incoming requests have a
    valid API key for the associated location.
    """
    def __call__(self, request):
        assert request.db, "Create a database before checking for the API key."

        # Early out if we already have an api
        if hasattr(request, 'api'):
            return self.controller(request)

        # API keys are given either in the query params or POST data.
        api_key = request.body_params.get('api_key')
        if api_key:
            api_key = api_key[0].strip().lower()

            # We must have a location, to check if the request came
            # from the correct place.
            from_location = request.body_params.get('from')
            if from_location:
                from_location_domain = urlparse.urlparse(from_location).netloc

                # Find the api key in the database matching the given
                # domain. So we can fail by either not having the key, or
                # not having the domain.
                api_data = request.db.mongo.api.find_one(
                    {"id": api_key, "domains": from_location_domain}
                    )
                if api_data:
                    assert api_data['type'] == "api"

# Path: rowan/utils/subdicts.py
# class ChangeTrackingDict(dict):
#     """
#     A dictionary subclass that has a field for tracking if the
#     dictionary is dirty and needs saving.
#     """
#     def __init__(self, *args, **kws):
#         super(ChangeTrackingDict, self).__init__(*args, **kws)
#         self.dirty = False
# 
#     def __setitem__(self, key, value):
#         """
#         Sets a value associated with a key, automatically updating the
#         record of whether this dictionary is dirty.
#         """
#         if self.dirty:
#             # Quick version if we're already dirty.
#             super(ChangeTrackingDict, self).__setitem__(key, value)
#         else:
#             old_value = self[key]
#             super(ChangeTrackingDict, self).__setitem__(key, value)
#             self.dirty = (old_value != value)
# 
#     def clean(self):
#         """
#         Notifies this dictionary that its data should be clean
#         (i.e. saved).
#         """
#         self.dirty = False


# Path: rowan/controllers/middleware.py
from time import time
from rowan.utils.subdicts import ChangeTrackingDict
import urlparse
import uuid
import base
import rowan.http as http

    """
    Wraps another controller, making sure any incoming requests have a
    valid API key for the associated location.
    """
    def __call__(self, request):
        assert request.db, "Create a database before checking for the API key."

        # Early out if we already have an api
        if hasattr(request, 'api'):
            return self.controller(request)

        # API keys are given either in the query params or POST data.
        api_key = request.body_params.get('api_key')
        if api_key:
            api_key = api_key[0].strip().lower()

            # We must have a location, to check if the request came
            # from the correct place.
            from_location = request.body_params.get('from')
            if from_location:
                from_location_domain = urlparse.urlparse(from_location).netloc

                # Find the api key in the database matching the given
                # domain. So we can fail by either not having the key, or
                # not having the domain.
                api_data = request.db.mongo.api.find_one(
                    {"id": api_key, "domains": from_location_domain}
                    )
                if api_data:
                    assert api_data['type'] == "api"

                    request.api = ChangeTrackingDict(api_data)