Ejemplo n.º 1
0
    def parse(self):
        _parsed_data = []
        records_obj = self.tree_obj.findall("record")

        for record_obj in records_obj:
            record_id = record_obj.get("id")
            if not record_id:
                abort(XMLResponse(code="30", message=f"Can not find required record id", status=400))
            if not record_id.isnumeric():
                abort(XMLResponse(code="30", message=f"Record id has incorrect data type", status=400))

            parsed_record = {
                "recordId": int(record_id),
                "failed_message": []
            }

            for field in self.RECORD_FIELDS:
                element = record_obj.find(f".//{field.f_name}", namespaces=self.namespaces)
                if element is None or not element.text:
                    parsed_record["failed_message"].append(f'{field.f_name} is required')
                else:
                    try:
                        validated_value = field.f_type(element.text)  # try to convert element to required type
                    except ValueError:
                        parsed_record["failed_message"].append(f'{field.f_name} has incorrect data type')
                    else:
                        parsed_record[field.f_name] = validated_value
            _parsed_data.append(parsed_record)

        return _parsed_data
Ejemplo n.º 2
0
    def parse(self):
        _parsed_data = []
        records_obj = self.tree_obj.findall("record")
        if not records_obj:
            abort(XMLResponse(code="80", message=f"Empty PRTrans Data xml", status=400))
        for record in records_obj:

            parsed_record = {}

            for field in self.RECORD_FIELDS:
                field_name = field.f_name
                element = record.find(f".//{field_name}", namespaces=self.namespaces)
                if element is None or not element.text:
                    abort(XMLResponse(code="30", message=f"'{field_name}' is required", status=400))

                try:
                    validated_value = field.f_type(element.text)  # try to convert element to required type
                except ValueError:
                    abort(XMLResponse(code="30", message=f'{field.f_name} has incorrect data type', status=400))
                else:
                    parsed_record[field.f_name] = validated_value

                    if field_name == "doc_status":
                        self.validate_doc_status_value(validated_value, field_name)

            _parsed_data.append(parsed_record)
        return _parsed_data
Ejemplo n.º 3
0
def encode_data_to_base64(data):
    try:
        return b64encode(data)
    except Exception as e:
        err_msg = f"Data base64 error: {e}"
        app.app.logger.warning(err_msg)
        abort(XMLResponse(code="80", message=err_msg, status=400))
Ejemplo n.º 4
0
 def create(cls, method_type):
     method_handler = cls.method_types.get(method_type)
     if method_handler is None:
         return abort(
             XMLResponse(code="40",
                         message=f"Invalid method: {method_type}",
                         status=400))
     return method_handler
Ejemplo n.º 5
0
 def __init__(self, data):
     self.data = data
     try:
         self.tree_obj = etree.fromstring(data)
     except Exception as e:
         err_msg = f"Invalid request xml: {e}"
         app.app.logger.warning(err_msg)
         abort(XMLResponse(code="80", message=err_msg, status=400))
Ejemplo n.º 6
0
    def run(self):
        parsed_data = self.parsed_data

        process_transaction.delay(
            transactions_data=parsed_data,
            source=self._data.decode(errors="ignore"),
            message_id=self.message_id
        )
        logger.info(f"Sent to processing data: {parsed_data}")

        return XMLResponse(code="0", message="Sent to processing")
Ejemplo n.º 7
0
def main():
    xml_parser = RequestFields(request.data)
    fields = xml_parser.parse()
    if not verify_auth(fields.UserLogin, fields.UserPassword):
        return XMLResponse(code="10",
                           message="Invalid login or password",
                           status=403)
    handler = MethodFactory.create(fields.MethodName)
    logger.info(f'DataSign: {fields.DataSign}')
    result = handler(fields.Data, fields.MessageId).run()
    return result
Ejemplo n.º 8
0
    def parse(self):
        for field in fields(self):
            field_name = field.name
            element = self.tree_obj.find(f".//{field_name}", namespaces=self.namespaces)
            if element is None or not element.text:
                abort(XMLResponse(code="30", message=f"'{field_name}' is required", status=400))

            try:
                method = getattr(self, f"import_{field_name}")
            except AttributeError:
                setattr(self, field_name, element.text)
            else:
                method(element.text)
        return self
Ejemplo n.º 9
0
 def validate_doc_status_value(value, field_name):
     if value not in (0, -1):
         abort(XMLResponse(code="30", message=f"'{field_name}' should be 0 or -1", status=400))