def put(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 instance_data = get_json_request_as_dict(self.request.body) 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) try: if not instance_exists(self.query_params): try: schema = schema_resource.get_cached_schema( self.query_params) except SchemaNotFound: schema = None if schema is None: msg = _(u"Class {0} doesn't exist in graph {1}.") raise HTTPError(404, log_message=msg.format( self.query_params["class_uri"], self.query_params["graph_uri"])) 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) else: edit_instance(self.query_params, instance_data) status = 200 except InstanceError as ex: raise HTTPError(400, log_message=str(ex)) except SchemaNotFound as ex: raise HTTPError(404, log_message=str(ex)) cache.purge_an_instance(self.query_params['instance_uri']) self.query_params["expand_object_properties"] = "1" instance_data = get_instance(self.query_params) if instance_data and settings.NOTIFY_BUS: self.query_params["instance_uri"] = instance_data["@id"] self._notify_bus(action="PUT", instance_data=instance_data) self.finalize(status)
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)
def put(self, query_id): validate_headers(self.request.headers) client_id = self.request.headers.get(CLIENT_ID_HEADER) client_id_dict = {"client_id": client_id} json_payload_object = get_json_request_as_dict(self.request.body) validate_json_schema(json_payload_object, query_crud_schema) json_payload_object.update(client_id_dict) # TODO return instance data when editing it? status = store_query(json_payload_object, query_id, client_id) self.finalize(status)
def post(self, context_name, class_name): valid_params = CLASS_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, **valid_params) del context_name del class_name try: schema = schema_resource.get_cached_schema(self.query_params) except SchemaNotFound: schema = None if schema is None: class_uri = self.query_params["class_uri"] graph_uri = self.query_params["graph_uri"] raise HTTPError( 404, log_message=_( u"Class {0} doesn't exist in context {1}.").format( class_uri, graph_uri)) instance_data = get_json_request_as_dict(self.request.body) instance_data = normalize_all_uris_recursively(instance_data) try: (instance_uri, instance_id) = create_instance(self.query_params, instance_data) except InstanceError as ex: raise HTTPError(500, log_message=str(ex)) instance_url = self.build_resource_url(instance_id) self.set_header("location", instance_url) self.set_header("X-Brainiak-Resource-URI", instance_uri) self.query_params["instance_uri"] = instance_uri self.query_params["instance_id"] = instance_id self.query_params["expand_object_properties"] = "1" instance_data = get_instance(self.query_params) if settings.NOTIFY_BUS: self._notify_bus(action="POST", instance_data=instance_data) self.finalize(201)
def post(self): valid_params = PAGING_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, **valid_params) raw_body_params = get_json_request_as_dict(self.request.body) body_params = normalize_all_uris_recursively(raw_body_params) if '@context' in body_params: del body_params['@context'] validate_json_schema(body_params, SUGGEST_PARAM_SCHEMA) response = do_suggest(self.query_params, body_params) if self.query_params['expand_uri'] == "0": response = normalize_all_uris_recursively(response, mode=SHORTEN) self.finalize(response)
def test_get_valid_json(self): valid_json_string = '{"items": []}' response = get_json_request_as_dict(valid_json_string) self.assertEqual(response["items"], [])
def test_json_with_escaped_quotes(self): expected = 'SELECT ?o ?label_o FROM <http://semantica.globo.com/esportes/> WHERE { ?s a esportes:MateriaEsporte . ?s base:permalink "http://globoesporte.globo.com/futebol/times/fluminense/noticia/2011/04/conca-x-200-timidez-brincadeiras-genialidade-e-gols-pelo-fluminense.html" . ?s ?p ?o . ?p a owl:ObjectProperty . ?o rdfs:label ?label_o . ' json_request_body = '''{ "sparql_template": "SELECT ?o ?label_o FROM <http://semantica.globo.com/esportes/> WHERE { ?s a esportes:MateriaEsporte . ?s base:permalink \\"http://globoesporte.globo.com/futebol/times/fluminense/noticia/2011/04/conca-x-200-timidez-brincadeiras-genialidade-e-gols-pelo-fluminense.html\\" . ?s ?p ?o . ?p a owl:ObjectProperty . ?o rdfs:label ?label_o . ", "description": "a" }''' result_dict = get_json_request_as_dict(json_request_body) self.assertEqual(result_dict["sparql_template"], expected)