def on_post(self, req, resp, dataset_id, dataset_dto):
"""Generates a search index to perform data lookups operations.
This task may take long time to complete, so it uses tasks.
:query int n_trees: The number of trees generated
:param id dataset_id: The dataset to insert triples into
:param DTO dataset_dto: The Dataset DTO from dataset_id (from hook)
"""
# Dig for the param on Query Params
n_trees = req.get_param_as_int('n_trees')
# Call to the task
task = async_tasks.build_search_index.delay(dataset_id, n_trees)
# Create the new task
task_dao = data_access.TaskDAO()
task_obj, err = task_dao.add_task_by_uuid(task.id)
if task_obj is None:
raise falcon.HTTPNotFound(description=str(err))
task_obj["next"] = "/datasets/" + dataset_id
task_dao.update_task(task_obj)
msg = "Task {} created successfuly".format(task_obj['id'])
textbody = {"status": 202, "message": msg}
resp.location = "/tasks/" + str(task_obj['id'])
resp.body = json.dumps(textbody)
resp.content_type = 'application/json'
resp.status = falcon.HTTP_202
python类HTTPNotFound()的实例源码
def on_post(self, req, resp, dataset_id, dataset_dto):
"""Generates an autocomplete index with desired lang
This request may take long time to complete, so it uses tasks.
:query list langs: A list with languages to be requested
:param id dataset_id: The dataset to insert triples into
:param DTO dataset_dto: The Dataset DTO from dataset_id (from hook)
"""
try:
body = common_hooks.read_body_as_json(req)
languages = body['langs']
if not isinstance(languages, list):
raise falcon.HTTPInvalidParam(
("A list with languages in ISO 639-1 code was expected"),
"langs")
except KeyError as err:
raise falcon.HTTPMissingParam("langs")
entity_dao = data_access.EntityDAO(dataset_dto.dataset_type,
dataset_id)
# Call to the task
task = async_tasks.build_autocomplete_index.delay(dataset_id,
langs=languages)
# Create the new task
task_dao = data_access.TaskDAO()
task_obj, err = task_dao.add_task_by_uuid(task.id)
if task_obj is None:
raise falcon.HTTPNotFound(description=str(err))
task_obj["next"] = "/datasets/" + dataset_id
task_dao.update_task(task_obj)
msg = "Task {} created successfuly".format(task_obj['id'])
textbody = {"status": 202, "message": msg}
resp.location = "/tasks/" + str(task_obj['id'])
resp.body = json.dumps(textbody)
resp.content_type = 'application/json'
resp.status = falcon.HTTP_202
def on_get(self, req: Request, res: Response, item_id):
with self.make_session() as session:
item = self.get_item(item_id, session)
if item is None:
raise falcon.HTTPNotFound()
put_json_to_context(res, item.to_dict())
def on_patch(self, req: Request, res: Response, item_id):
with self.make_session() as session:
try:
ok = self.update_item(item_id, req.context["doc"], session)
if not ok:
raise falcon.HTTPNotFound()
except IntegrityError as e:
raise falcon.HTTPConflict("Conflict", str(e))
def on_delete(self, req: Request, res: Response, item_id):
with self.make_session() as session:
try:
ok = self.delete_item(item_id, session)
if not ok:
raise falcon.HTTPNotFound()
except IntegrityError as e:
raise falcon.HTTPConflict("Conflict", str(e))
def __call__(self, req, resp, filepath):
resp.content_type = mimetypes.guess_type(filepath)[0]
curr_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(
os.path.join(curr_dir, self.static_path),
filepath
)
if not os.path.exists(file_path):
raise falcon.HTTPNotFound()
else:
resp.stream = open(file_path, 'rb')
resp.stream_len = os.path.getsize(file_path)
def process(self, req, resp):
if req.method == 'OPTIONS':
if self.cors_origin is not False:
self.process_preflight_request(req, resp)
response_body = '\n'
response_body += 'nothing here\n\n'
resp.body = response_body
resp.status = falcon.HTTP_200
return
try:
if self.cors_origin is not False:
self.process_preflight_request(req, resp)
self.dispatch(req, resp)
except Exception as e:
self.log.error_trace('process failed')
error_type = type(e)
error_map = {
falcon.errors.HTTPNotFound: http_falcon_handler,
falcon.errors.HTTPMissingParam: http_falcon_handler,
falcon.errors.HTTPInvalidParam: http_falcon_handler,
falcon.errors.HTTPInternalServerError: http_falcon_handler,
}
if self.custom_error_map:
error_map.update(self.custom_error_map)
error_func = error_map.get(error_type)
if error_func:
error_func(req, resp, e)
else:
default_error_handler(req, resp, e)
def __call__(self, req, resp, filename):
file_ending = filename[filename.rfind('.'):]
try:
resp.content_type = env.mime_types[file_ending]
with open('project/static/' + filename) as f:
resp.body = f.read()
except Exception:
raise falcon.HTTPNotFound(description="404 Not Found")
def on_get(self, req, resp, filename):
suffix = os.path.splitext(req.path)[1]
resp.content_type = mimes.get(suffix, 'application/octet-stream')
filepath = os.path.join(ui_root, self.path, secure_filename(filename))
try:
resp.stream = open(filepath, 'rb')
resp.stream_len = os.path.getsize(filepath)
except IOError:
raise HTTPNotFound()
def on_get(self, req, resp, plan_id):
if plan_id.isdigit():
where = 'WHERE `plan`.`id` = %s'
else:
where = 'WHERE `plan`.`name` = %s AND `plan_active`.`plan_id` IS NOT NULL'
query = single_plan_query + where
connection = db.engine.raw_connection()
cursor = connection.cursor(db.dict_cursor)
cursor.execute(query, plan_id)
plan = cursor.fetchone()
if plan:
step = 0
steps = []
cursor.execute(single_plan_query_steps, plan['id'])
for notification in cursor:
s = notification['step']
if s != step:
l = [notification]
steps.append(l)
step = s
else:
l.append(notification)
plan['steps'] = steps
if plan['tracking_template']:
plan['tracking_template'] = ujson.loads(plan['tracking_template'])
resp.body = ujson.dumps(plan)
connection.close()
else:
connection.close()
raise HTTPNotFound()
def on_get(self, req, resp, template_id):
if template_id.isdigit():
where = 'WHERE `template`.`id` = %s'
else:
where = 'WHERE `template`.`name` = %s AND `template_active`.`template_id` IS NOT NULL'
query = single_template_query + where
connection = db.engine.raw_connection()
cursor = connection.cursor()
cursor.execute(query, template_id)
results = cursor.fetchall()
if results:
r = results[0]
t = {
'id': r[0],
'name': r[1],
'active': r[2],
'creator': r[3],
'created': r[4]
}
content = {}
for r in results:
content.setdefault(r[5], {})[r[6]] = {'subject': r[7], 'body': r[8]}
t['content'] = content
cursor = connection.cursor(db.dict_cursor)
cursor.execute(single_template_query_plans, t['name'])
t['plans'] = cursor.fetchall()
connection.close()
payload = ujson.dumps(t)
else:
raise HTTPNotFound()
resp.status = HTTP_200
resp.body = payload
def on_delete(self, req, resp, username, src_mode_name):
'''
Delete a reprioritization mode for a user's mode setting
**Example request**:
.. sourcecode:: http
DELETE /v0/users/reprioritization/{username}/{src_mode_name} HTTP/1.1
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/json
[]
'''
with db.guarded_session() as session:
affected_rows = session.execute(delete_reprioritization_settings_query, {
'target_name': username,
'mode_name': src_mode_name,
}).rowcount
if affected_rows == 0:
raise HTTPNotFound()
session.commit()
session.close()
resp.status = HTTP_200
resp.body = '[]'
def on_get(self, req, resp):
'''
Healthcheck endpoint. Returns contents of healthcheck file.
**Example request**:
.. sourcecode:: http
GET /v0/healthcheck HTTP/1.1
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: text/plain
GOOD
'''
try:
with open(self.healthcheck_path) as f:
health = f.readline().strip()
except:
raise HTTPNotFound()
resp.status = HTTP_200
resp.content_type = 'text/plain'
resp.body = health
def not_found(message="The requested resource does not exist"):
raise falcon.HTTPNotFound(description=message, code=falcon.HTTP_404)
def on_get(self, req, resp):
if not self.healthcheck_path:
logger.error('Healthcheck path not set')
raise falcon.HTTPNotFound()
try:
with open(self.healthcheck_path) as f:
health = f.readline().strip()
except IOError:
raise falcon.HTTPNotFound()
resp.status = falcon.HTTP_200
resp.content_type = 'text/plain'
resp.body = health
def _get_sink_responder(self, path):
params = {}
for pattern, sink in self._sinks:
m = pattern.match(path)
if m:
params = m.groupdict()
return sink, params, None, None
else:
raise HTTPNotFound()
def do_get(self, req, resp, hostname, asset_type):
"""Render ``unit`` type boot action assets for hostname.
Get the boot action context for ``hostname`` from the database
and render all ``unit`` type assets for the host. Validate host
is providing the correct idenity key in the ``X-Bootaction-Key``
header.
:param req: falcon request object
:param resp: falcon response object
:param hostname: URL path parameter indicating the calling host
:param asset_type: Asset type to include in the response - ``unit``, ``file``, ``pkg_list``, ``all``
"""
try:
ba_ctx = self.state_manager.get_boot_action_context(hostname)
except Exception as ex:
self.logger.error(
"Error locating boot action for %s" % hostname, exc_info=ex)
raise falcon.HTTPNotFound()
if ba_ctx is None:
raise falcon.HTTPNotFound(
description="Error locating boot action for %s" % hostname)
BootactionUtils.check_auth(ba_ctx, req)
asset_type_filter = None if asset_type == 'all' else asset_type
try:
task = self.state_manager.get_task(ba_ctx['task_id'])
design_status, site_design = self.orchestrator.get_effective_site(
task.design_ref)
assets = list()
ba_status_list = self.state_manager.get_boot_actions_for_node(
hostname)
for ba in site_design.bootactions:
if hostname in ba.target_nodes:
ba_status = ba_status_list.get(ba.name, None)
action_id = ba_status.get('action_id')
assets.extend(
ba.render_assets(
hostname,
site_design,
action_id,
type_filter=asset_type_filter))
tarball = BootactionUtils.tarbuilder(asset_list=assets)
resp.set_header('Content-Type', 'application/gzip')
resp.set_header('Content-Disposition',
"attachment; filename=\"%s-%s.tar.gz\"" %
(hostname, asset_type))
resp.data = tarball
resp.status = falcon.HTTP_200
return
except Exception as ex:
self.logger.debug("Exception in boot action API.", exc_info=ex)
raise falcon.HTTPInternalServerError(str(ex))
def on_get(self, req, resp, task_id):
"""Return one task"""
tdao = data_access.TaskDAO()
task_obj, err = tdao.get_task_by_id(task_id)
if task_obj is None:
raise falcon.HTTPNotFound(description=str(err))
t_uuid = celery_server.app.AsyncResult(task_obj['celery_uuid'])
task = {}
task["state"] = t_uuid.state
# task["is_ready"] = t_uuid.ready()
task["id"] = task_obj["id"]
try:
if req.get_param_as_bool('get_debug_info'):
task["debug"] = task_obj
except Exception:
pass
if t_uuid.state == "SUCCESS":
# Look if exists some next
if "next" in task_obj and task_obj["next"] is not None:
print("This task has next {}".format(task_obj["next"]))
try:
if req.get_param_as_bool('no_redirect'):
resp.status = falcon.HTTP_200
else:
resp.status = falcon.HTTP_303
except Exception:
resp.status = falcon.HTTP_303
resp.location = task_obj["next"]
elif t_uuid.state == "STARTED":
# Get task progress and show to the user
celery_uuid = "celery-task-progress-" + task_obj['celery_uuid']
redis = data_access.RedisBackend()
task_progress = redis.get(celery_uuid)
try:
if "progress" in task_progress:
task["progress"] = task_progress["progress"]
except TypeError:
pass
resp.status = falcon.HTTP_200
elif t_uuid.state == "FAILURE":
task["error"] = {"exception": str(t_uuid.result),
"traceback": t_uuid.traceback}
response = {"task": task}
resp.body = json.dumps(response)
resp.content_type = 'application/json'
def on_post(self, req, resp, dataset_id, dataset_dto, gen_triples_param):
"""Generates a task to insert triples on dataset. Async petition.
Reads from body the parameters such as SPARQL queries
{"generate_triples":
{
"graph_pattern": "<SPARQL Query (Where part)>",
"levels": 2,
"batch_size": 30000 # Optional
}
}
:param id dataset_id: The dataset to insert triples into
:param DTO dataset_dto: The Dataset DTO from dataset_id (from hook)
:param dict gen_triples_param: Params to call generate_triples function
(from hook)
"""
try:
batch_size = gen_triples_param.pop("batch_size")
except KeyError:
batch_size = None
# Launch async task
task = async_tasks.generate_dataset_from_sparql.delay(
dataset_id, gen_triples_param.pop("graph_pattern"),
int(gen_triples_param.pop("levels")), batch_size=batch_size)
# Create a new task
task_dao = data_access.TaskDAO()
task_obj, err = task_dao.add_task_by_uuid(task.id)
if task_obj is None:
raise falcon.HTTPNotFound(description=str(err))
task_obj["next"] = "/datasets/" + dataset_id
task_dao.update_task(task_obj)
# Store the task into DatasetDTO
dataset_dao = data_access.DatasetDAO()
dataset_dao.set_task(dataset_id, task_obj['id'])
msg = "Task {} created successfuly".format(task_obj['id'])
textbody = {"status": 202, "message": msg, "task": task_dao.task}
resp.location = "/tasks/" + str(task_obj['id'])
resp.body = json.dumps(textbody)
resp.content_type = 'application/json'
resp.status = falcon.HTTP_202
def dispatch(self, req, resp):
base_before, base_after, base_excp, base_final = self.op_loader.load_base(self.specs)
for uri_regex, spec in self.specs.items():
# try:
route_signature = '/' + req.method.lower() + req.relative_uri
if route_signature.find('?') > 0:
route_signature = route_signature[:route_signature.find('?')]
if type(uri_regex) == str:
continue
spec['route_signature'] = route_signature
req.spec = copy.deepcopy(spec)
match = uri_regex.match(route_signature)
if match:
handler, params, before, after, excp, final, mode = self.op_loader.load(req=req, spec=spec,
matched_uri=match)
handler_return = None
try:
if base_before:
base_before(req=req, resp=resp, **params)
if before:
before(req=req, resp=resp, **params)
if mode == 'raw':
handler_return = handler(req=req, resp=resp)
else:
if mode == 'more':
handler_return = handler(req=req, resp=resp, **params)
else:
handler_return = handler(**params)
content_type = self.produces(spec.get('produces'), self.specs.get('produces'))
self.process_response(req, resp, handler_return, content_type)
if after:
after(req=req, resp=resp, response=handler_return, **params)
if base_after:
base_after(req=req, resp=resp, **params)
except Exception as e:
throw_out = True
if base_excp is not None:
throw_out = base_excp(req=req, resp=resp, error=e)
if excp is not None:
throw_out = excp(req=req, resp=resp, error=e)
if throw_out:
raise e
finally:
if final:
final(req=req, resp=resp, response=handler_return, **params)
if base_final:
base_final(req=req, resp=resp, **params)
return
# except falcon.HTTPInvalidParam as e:
# self.log.error_trace("http invalid param: {}".format(e))
# raise e
# except Exception as e:
# self.log.error_trace("process error: {}".format(e))
# raise falcon.HTTPInternalServerError(title=str(type(e)), description=str(e))
self.log.info("url does not match any route signature or match error: {}".format(route_signature))
raise falcon.HTTPNotFound()