Example #1
0
 def from_doc(document, id, label, resp):
     try:
         return document.get(id=id)
     except Exception as e:
         msg = f'{label} with id={id} not found.'
         LCP.__log().exception(msg, e)
         Not_Found_Response(msg, exception=e).add(resp)
         return None
Example #2
0
 def from_catalog(catalog, id, label, resp):
     def __filter_id(x):
         return x.id == id
     ret = list(filter(__filter_id, catalog))
     if len(ret) == 1:
         return ret[0]
     else:
         msg = f'{label} with id={id} not found.'
         Not_Found_Response(msg).add(resp)
         return None
Example #3
0
 def on_base_delete(self, req, resp, id=None):
     req_data = req.media or {}
     qrs = Query_Request_Schema(method=HTTP_Method.DELETE)
     resp_data, valid = qrs.validate(data=req_data, id=id)
     if resp:
         try:
             qr = Query_Reader(index=self.doc.Index.name)
             s = qr.parse(query=req_data, id=id)
             hits = s.execute()
             if len(hits) > 0:
                 for hit in hits:
                     try:
                         obj = self.doc.get(id=hit.meta.id)
                         msg = f'{self.name.capitalize()} with the id={hit.meta.id} correctly deleted'
                         resp_data_lcp = []
                         resp_data = Reset_Content_Response(msg)
                         hndl = self.get_lcp_handler(HTTP_Method.DELETE)
                         hndl(instance=obj, req=hit, resp=resp_data_lcp)
                         if len(resp_data_lcp) > 0:
                             for rdl in resp_data_lcp:
                                 if rdl['error']:
                                     msg = f'Not possible to delete the {self.name} with the id={hit.meta.id}'
                                     resp_data = Unprocessable_Entity_Response(
                                         msg)
                                     break
                             resp_data.update(lcp_response=resp_data_lcp)
                         force = req_data.get('force', False)
                         if not resp_data.error or force:
                             obj.delete()
                             if force:
                                 msg = f'Some errors occur but the {self.name} with the id={hit.meta.id} forcedly deleted'
                                 resp_data = Unprocessable_Entity_Response(
                                     msg)
                         resp_data.add(resp)
                     except Exception as e:
                         msg = f'Not possible to delete the {self.name} with the id={hit.meta.id}'
                         Unprocessable_Entity_Response(
                             msg, exception=e).add(resp)
             else:
                 msg = f'{self.names.capitalize()} based on the request {{query}} not found'
                 Not_Found_Response(msg, query=req_data).apply(resp)
         except Exception as e:
             msg = f'Not possible to delete {self.names} with the request {{query}}'
             Unprocessable_Entity_Response(msg, exception=e,
                                           query=req_data).apply(resp)
     else:
         resp_data.apply(resp)
Example #4
0
 def __resources(self, data):
     path = data.get('path', None)
     content = data.get('content', None)
     output = {'type': 'resource'}
     try:
         fix_path = expand_user(path)
         with open(fix_path, "w") as file:
             file.write(content)
         output.update(path=path, content=content)
         return output
     except FileNotFoundError as e:
         msg = f'Path {path} not found'
         self.log.exception(msg, e)
         return Not_Found_Response(msg, e, type='resource', data=data)
     except Exception as e:
         msg = f'Path {path} not accessible'
         self.log.exception(msg, e)
         return Bad_Request_Response(e,
                                     message=msg,
                                     type='resource',
                                     data=data)
Example #5
0
 def __parameters(self, data):
     schema = data.get('schema', None)
     source = data.get('source', None)
     path = wrap(data.get('path', []))
     value = data.get('value', None)
     output = {'type': 'parameter'}
     try:
         source = expand_user(source)
         output.update(
             self.parsers.get(schema)(schema, source, path, value))
         return output
     except File_Not_Found_Error as e:
         msg = f'Source {source} not found'
         self.log.exception(msg, e)
         return Not_Found_Response(msg, e, type='parameter', data=data)
     except Exception as e:
         msg = f'Source {source} not accessible'
         self.log.exception(msg, e)
         return Bad_Request_Response(e,
                                     message=msg,
                                     type='parameter',
                                     data=data)
Example #6
0
 def on_base_get(self, req, resp, id=None):
     req_data = req.media or {}
     qrs = Query_Request_Schema(method=HTTP_Method.GET, unknown='INCLUDE')
     resp_data, valid = qrs.validate(data=req_data, id=id)
     if valid:
         try:
             qr = Query_Reader(index=self.doc.Index.name)
             s = qr.parse(query=req_data, id=id)
             resp_data = [
                 dict(hit.to_dict(), id=hit.meta.id) for hit in s.execute()
             ]
             if len(resp_data) > 0:
                 Content_Response(resp_data).apply(resp)
             else:
                 msg = f'{self.name.capitalize()} based on the request {{query}} not found'
                 Not_Found_Response(msg, query=req_data).apply(resp)
         except Exception as e:
             msg = f'Not possible to get {self.names} with the request {{query}}'
             Unprocessable_Entity_Response(msg, exception=e,
                                           query=req_data).apply(resp)
     else:
         resp_data.apply(resp)
Example #7
0
class Not_Found_Response_Schema(Base_Response_Schema):
    status = Constant(Not_Found_Response.status())
    error = Constant(Not_Found_Response.error)
    code = Constant(Not_Found_Response.code)
Example #8
0
from lib.response import (
    Bad_Request_Response, Conflict_Response, Content_Response,
    Created_Response, Internal_Server_Error_Response, No_Content_Response,
    Not_Acceptable_Response, Not_Found_Response, Not_Modified_Response,
    Ok_Response, Reset_Content_Response, Unauthorized_Response,
    Unprocessable_Entity_Response, Unsupported_Media_Type_Response)

RESPONSE_STATUS = [
    Bad_Request_Response.status(),
    Conflict_Response.status(),
    Content_Response.status(),
    Created_Response.status(),
    No_Content_Response.status(),
    Not_Acceptable_Response.status(),
    Not_Found_Response.status(),
    Not_Modified_Response.status(),
    Ok_Response.status(),
    Reset_Content_Response.status(),
    Unauthorized_Response.status(),
    Unprocessable_Entity_Response.status(),
    Unsupported_Media_Type_Response.status()
]

RESPONSE_CODES = [
    Bad_Request_Response.code, Conflict_Response.code, Content_Response.code,
    Created_Response.code, No_Content_Response.code,
    Not_Acceptable_Response.code, Not_Found_Response.code,
    Not_Modified_Response.code, Ok_Response.code, Reset_Content_Response.code,
    Unauthorized_Response.code, Unprocessable_Entity_Response.code,
    Unsupported_Media_Type_Response.code