コード例 #1
0
ファイル: handlers.py プロジェクト: bmentges/brainiak_api
    def get(self, context_name, class_name, instance_id):
        optional_params = INSTANCE_PARAMS
        with safe_params(optional_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **optional_params)

        response = memoize(self.query_params,
                           get_instance,
                           key=build_instance_key(self.query_params),
                           function_arguments=self.query_params)

        if response is None:
            error_message = u"Instance ({0}) of class ({1}) in graph ({2}) was not found.".format(
                self.query_params['instance_uri'],
                self.query_params['class_uri'],
                self.query_params['graph_uri'])
            raise HTTPError(404, log_message=error_message)

        response_meta = response['meta']
        response = response['body']

        if self.query_params["expand_uri"] == "0":
            response = normalize_all_uris_recursively(response, mode=SHORTEN)

        self.add_cache_headers(response_meta)
        self.finalize(response)
コード例 #2
0
    def get(self, context_name, class_name, instance_id):
        optional_params = INSTANCE_PARAMS
        with safe_params(optional_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **optional_params)

        response = memoize(self.query_params,
                           get_instance,
                           key=build_instance_key(self.query_params),
                           function_arguments=self.query_params)

        if response is None:
            error_message = u"Instance ({0}) of class ({1}) in graph ({2}) was not found.".format(
                self.query_params['instance_uri'],
                self.query_params['class_uri'],
                self.query_params['graph_uri'])
            raise HTTPError(404, log_message=error_message)

        response_meta = response['meta']
        response = response['body']

        if self.query_params["expand_uri"] == "0":
            response = normalize_all_uris_recursively(response, mode=SHORTEN)

        self.add_cache_headers(response_meta)
        self.finalize(response)
コード例 #3
0
    def test_memoize_cache_enabled_and_hit(self, settings):
        def clean_up():
            return {"status": "Laundry done"}

        params = Mock(request=MockRequest(uri="/home"))
        answer = memoize(params, clean_up)
        self.assertEqual(answer['status'], "Dishes cleaned up")
        self.assertEqual(answer['meta']['cache'], "HIT")
コード例 #4
0
    def test_memoize_cache_enabled_and_hit_with_different_key(self, settings):
        def ressurect():
            return {"status": "Ressurected"}

        params = {'request': MockRequest(uri="/home")}
        answer = memoize(params, ressurect, key="/grave")
        self.assertEqual(answer["status"], "Sleeping")
        self.assertEqual(answer['meta']['cache'], "HIT")
コード例 #5
0
    def test_memoize_cache_enabled_and_hit(self, strict_redis, redis_get,
                                           redis_set, settings):
        def clean_up():
            return {"status": "Laundry done"}

        params = Mock(request=MockRequest(uri="/home"))
        answer = memoize(params, clean_up)
        self.assertEqual(answer['status'], "Dishes cleaned up")
        self.assertEqual(redis_get.call_count, 1)
コード例 #6
0
    def test_memoize_cache_enabled_and_hit_with_different_key(self, settings):

        def ressurect():
            return {"status": "Ressurected"}

        params = {'request': MockRequest(uri="/home")}
        answer = memoize(params, ressurect, key="/grave")
        self.assertEqual(answer["status"], "Sleeping")
        self.assertEqual(answer['meta']['cache'], "HIT")
コード例 #7
0
    def test_memoize_cache_enabled_and_hit(self, settings):

        def clean_up():
            return {"status": "Laundry done"}

        params = Mock(request=MockRequest(uri="/home"))
        answer = memoize(params, clean_up)
        self.assertEqual(answer['status'], "Dishes cleaned up")
        self.assertEqual(answer['meta']['cache'], "HIT")
コード例 #8
0
    def test_memoize_cache_enabled_and_hit(self, strict_redis, redis_get, redis_set, settings):

        def clean_up():
            return {"status": "Laundry done"}

        params = Mock(request=MockRequest(uri="/home"))
        answer = memoize(params, clean_up)
        self.assertEqual(answer['status'], "Dishes cleaned up")
        self.assertEqual(redis_get.call_count, 1)
コード例 #9
0
    def patch(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        patch_list = get_json_request_as_dict(self.request.body)

        # Retrieve original data
        instance_data = memoize(self.query_params,
                                get_instance,
                                key=build_instance_key(self.query_params),
                                function_arguments=self.query_params)

        if instance_data is not None:
            # Editing an instance
            instance_data = instance_data['body']
            instance_data.pop(
                'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', None)

            # compute patch
            changed_data = apply_patch(instance_data, patch_list)

            # Try to put
            edit_instance(self.query_params, changed_data)
            status = 200

            # Clear cache
            cache.purge_an_instance(self.query_params['instance_uri'])

            self.finalize(status)
        else:
            # Creating a new instance from patch list
            instance_data = get_instance_data_from_patch_list(patch_list)
            instance_data = normalize_all_uris_recursively(instance_data)

            rdf_type_error = is_rdf_type_invalid(self.query_params,
                                                 instance_data)
            if rdf_type_error:
                raise HTTPError(400, log_message=rdf_type_error)

            instance_uri, instance_id = create_instance(
                self.query_params, instance_data,
                self.query_params["instance_uri"])
            resource_url = self.request.full_url()
            status = 201
            self.set_header("location", resource_url)
            self.set_header("X-Brainiak-Resource-URI", instance_uri)

            self.finalize(status)
コード例 #10
0
ファイル: get_class.py プロジェクト: bmentges/brainiak_api
def get_cached_schema(query_params, include_meta=False):
    schema_key = build_key_for_class(query_params)
    class_object = memoize(query_params, get_schema, query_params, key=schema_key)
    if class_object is None or not class_object["body"]:
        msg = _(u"The class definition for {0} was not found in graph {1}")
        raise SchemaNotFound(msg.format(query_params['class_uri'], query_params['graph_uri']))
    if include_meta:
        return class_object
    else:
        return class_object['body']
コード例 #11
0
    def test_memoize_cache_disabled(self, strict_redis, redis_get, redis_set,
                                    settings):
        def clean_up():
            return {"status": "Laundry done"}

        params = {'request': MockRequest(uri="/home")}
        answer = memoize(params, clean_up)

        self.assertEqual(answer["body"], {"status": "Laundry done"})
        self.assertTrue(answer["meta"])
        self.assertEqual(redis_get.call_count, 0)
        self.assertEqual(redis_set.call_count, 0)
コード例 #12
0
    def test_memoize_cache_disabled(self, strict_redis, redis_get, redis_set, settings):

        def clean_up():
            return {"status": "Laundry done"}

        params = {'request': MockRequest(uri="/home")}
        answer = memoize(params, clean_up)

        self.assertEqual(answer["body"], {"status": "Laundry done"})
        self.assertTrue(answer["meta"])
        self.assertEqual(redis_get.call_count, 0)
        self.assertEqual(redis_set.call_count, 0)
コード例 #13
0
    def get(self):
        valid_params = PAGING_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self, **valid_params)
        response = memoize(self.query_params,
                           list_all_contexts,
                           function_arguments=self.query_params,
                           key=cache.build_key_for_root(self.query_params))
        if response is None:
            raise HTTPError(404, log_message=_("Failed to retrieve list of graphs"))

        self.add_cache_headers(response['meta'])
        self.finalize(response['body'])
コード例 #14
0
    def test_memoize_cache_disabled_delegatee_receive_params(self, strict_redis, redis_get, redis_set, settings):

        def clean_up(param_value):
            return {"status": param_value}

        params = {'request': MockRequest(uri="/home")}
        param_to_clean_up = 'It was received by the wrapped function'
        answer = memoize(params, clean_up, function_arguments=param_to_clean_up)

        self.assertEqual(answer["body"], {"status": param_to_clean_up})
        self.assertTrue(answer["meta"])
        self.assertEqual(redis_get.call_count, 0)
        self.assertEqual(redis_set.call_count, 0)
コード例 #15
0
    def test_memoize_cache_disabled_delegatee_receive_params(self, strict_redis, redis_get, redis_set, settings):

        def clean_up(param_value):
            return {"status": param_value}

        params = {'request': MockRequest(uri="/home")}
        param_to_clean_up = 'It was received by the wrapped function'
        answer = memoize(params, clean_up, function_arguments=param_to_clean_up)

        self.assertEqual(answer["body"], {"status": param_to_clean_up})
        self.assertTrue(answer["meta"])
        self.assertEqual(redis_get.call_count, 0)
        self.assertEqual(redis_set.call_count, 0)
コード例 #16
0
ファイル: handlers.py プロジェクト: bmentges/brainiak_api
    def get(self):
        valid_params = PAGING_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self, **valid_params)
        response = memoize(self.query_params,
                           list_all_contexts,
                           function_arguments=self.query_params,
                           key=cache.build_key_for_root(self.query_params))
        if response is None:
            raise HTTPError(404, log_message=_("Failed to retrieve list of graphs"))

        self.add_cache_headers(response['meta'])
        self.finalize(response['body'])
コード例 #17
0
    def get(self):
        with safe_params():
            self.query_params = ParamDict(self)

        response = memoize(self.query_params,
                           root_schema,
                           key=self.get_cache_path())
        if response is None:
            raise HTTPError(404,
                            log_message=_("Failed to retrieve json-schema"))

        self.add_cache_headers(response['meta'])
        self.finalize(response['body'])
コード例 #18
0
ファイル: handlers.py プロジェクト: bmentges/brainiak_api
    def get(self):
        with safe_params():
            self.query_params = ParamDict(self)

        response = memoize(
            self.query_params,
            root_schema,
            key=self.get_cache_path()
        )
        if response is None:
            raise HTTPError(404, log_message=_("Failed to retrieve json-schema"))

        self.add_cache_headers(response['meta'])
        self.finalize(response['body'])
コード例 #19
0
def get_cached_schema(query_params, include_meta=False):
    schema_key = build_key_for_class(query_params)
    class_object = memoize(query_params,
                           get_schema,
                           query_params,
                           key=schema_key)
    if class_object is None or not class_object["body"]:
        msg = _(u"The class definition for {0} was not found in graph {1}")
        raise SchemaNotFound(
            msg.format(query_params['class_uri'], query_params['graph_uri']))
    if include_meta:
        return class_object
    else:
        return class_object['body']
コード例 #20
0
    def get(self):
        with safe_params():
            self.query_params = ParamDict(self)

        response = memoize(self.query_params,
                           root_schema,
                           key=self.get_cache_path())
        if response is None:
            raise HTTPError(404,
                            log_message=_("Failed to retrieve json-schema"))

        self.add_cache_headers(response['meta'])
        # FIXME: handle cache policy uniformly
        self.set_header("Cache-control", "private")
        self.set_header("max-age", "0")

        self.finalize(response['body'])
コード例 #21
0
    def test_memoize_cache_enabled_but_without_cache(self, strict_redis, redis_get, redis_set, settings, isoformat):

        def clean_up():
            return {"status": "Laundry done"}

        params = Mock(request=MockRequest(uri="/home"))
        answer = memoize(params, clean_up)

        expected = {
            'body': {"status": "Laundry done"},
            'meta': {
                'cache': 'MISS',
                'last_modified': 'Fri, 11 May 1984 20:00:00 -0300'
            }
        }
        self.assertEqual(answer, expected)
        self.assertEqual(redis_get.call_count, 1)
        self.assertEqual(redis_set.call_count, 1)
コード例 #22
0
    def test_memoize_cache_enabled_but_without_cache(self, strict_redis, redis_get, redis_set, settings, isoformat):

        def clean_up():
            return {"status": "Laundry done"}

        params = Mock(request=MockRequest(uri="/home"))
        answer = memoize(params, clean_up)

        expected = {
            'body': {"status": "Laundry done"},
            'meta': {
                'cache': 'MISS',
                'last_modified': 'Fri, 11 May 1984 20:00:00 -0300'
            }
        }
        self.assertEqual(answer, expected)
        self.assertEqual(redis_get.call_count, 1)
        self.assertEqual(redis_set.call_count, 1)
コード例 #23
0
    def patch(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            patch_list = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400,
                            log_message=_("No JSON object could be decoded"))

        # Retrieve original data
        instance_data = memoize(self.query_params,
                                get_instance,
                                key=build_instance_key(self.query_params),
                                function_arguments=self.query_params)
        try:
            instance_data = instance_data['body']
        except TypeError:
            raise HTTPError(404, log_message=_("Inexistent instance"))

        instance_data.pop('http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
                          None)

        # compute patch
        changed_data = apply_patch(instance_data, patch_list)

        # Try to put
        edit_instance(self.query_params, changed_data)
        status = 200

        # Clear cache
        cache.purge_an_instance(self.query_params['instance_uri'])

        self.finalize(status)
コード例 #24
0
ファイル: handlers.py プロジェクト: bmentges/brainiak_api
    def patch(self, context_name, class_name, instance_id):
        valid_params = INSTANCE_PARAMS
        with safe_params(valid_params):
            self.query_params = ParamDict(self,
                                          context_name=context_name,
                                          class_name=class_name,
                                          instance_id=instance_id,
                                          **valid_params)
        del context_name
        del class_name
        del instance_id

        try:
            patch_list = json.loads(self.request.body)
        except ValueError:
            raise HTTPError(400, log_message=_("No JSON object could be decoded"))

        # Retrieve original data
        instance_data = memoize(self.query_params,
                           get_instance,
                           key=build_instance_key(self.query_params),
                           function_arguments=self.query_params)
        try:
            instance_data = instance_data['body']
        except TypeError:
            raise HTTPError(404, log_message=_("Inexistent instance"))

        instance_data.pop('http://www.w3.org/1999/02/22-rdf-syntax-ns#type', None)

        # compute patch
        changed_data = apply_patch(instance_data, patch_list)

        # Try to put
        edit_instance(self.query_params, changed_data)
        status = 200

        # Clear cache
        cache.purge_an_instance(self.query_params['instance_uri'])

        self.finalize(status)