Exemplo n.º 1
0
 def on_post(self, req, resp):
     req_data = req.media or {}
     resp_data, valid = Status_Request_Schema(
         method=HTTP_Method.POST).validate(data=req_data)
     if valid:
         now = datetime_to_str()
         id = req_data.get('id')
         self.data['id'] = id
         self.data['last_heartbeat'] = now
         self.log.notice(f'Hearbeating from CB at {now}')
         resp_data, valid = Status_Response_Schema(
             method=HTTP_Method.POST).validate(data=self.data)
         if valid:
             Content_Response(self.data).apply(resp)
         else:
             resp_data.apply(resp)
     else:
         resp_data.apply(resp)
Exemplo n.º 2
0
 def on_post(self, req, resp):
     req_data = req.media or {}
     resp_data, valid = Config_Request_Schema(
         many=is_list(req_data),
         method=HTTP_Method.POST).validate(data=req_data)
     if valid:
         req_data_wrap = wrap(req_data)
         if len(req_data_wrap) > 0:
             for config in req_data_wrap:
                 for cfg, cfg_list in config.items():
                     for data in wrap(cfg_list):
                         output = {}
                         if cfg == 'actions':
                             output = self.__actions(data)
                             schema = Config_Action_Response_Schema
                         elif cfg == 'parameters':
                             output = self.__parameters(data)
                             schema = Config_Parameter_Response_Schema
                         elif cfg == 'resources':
                             output = self.__resources(data)
                             schema = Config_Resource_Response_Schema
                         if isinstance(output, Base_Response):
                             output.add(resp)
                         else:
                             output_data = data.copy()
                             id = output_data.pop('id', None)
                             output.update(id=id,
                                           data=output_data,
                                           timestamp=datetime_to_str())
                             resp_data, valid = schema(
                                 many=False,
                                 method=HTTP_Method.POST,
                                 unknown='INCLUDE').validate(data=output)
                             if valid:
                                 Content_Response(output).add(resp)
                             else:
                                 resp_data.add(resp)
         else:
             msg = 'No content to apply configurations with the {{request}}'
             No_Content_Response(msg, request=req_data).apply(resp)
     else:
         resp_data.apply(resp)
Exemplo n.º 3
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)
Exemplo n.º 4
0
class Content_Response_Schema(Ok_Response_Schema):
    status = Constant(Content_Response.status())
    error = Constant(Content_Response.error)
    code = Constant(Content_Response.code)
Exemplo n.º 5
0
from marshmallow import Schema, validate
from marshmallow.fields import Bool, Constant, Integer, Nested, Raw, Str

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,
Exemplo n.º 6
0
 def on_get(self, req, resp):
     Content_Response(self.data).apply(resp)