iqrfpy.utils.validators

Validators module.

This module classes for validation of DPA and JSON messages and their parameters.

 1"""Validators module.
 2
 3This module classes for validation of DPA and JSON messages and their parameters.
 4"""
 5
 6from iqrfpy.exceptions import DpaConfirmationPacketError, DpaConfirmationPacketLengthError, \
 7    DpaResponsePacketLengthError, MessageNotReceivedError
 8from iqrfpy.utils.common import Common
 9import iqrfpy.utils.dpa as dpa_constants
10from iqrfpy.utils.dpa import ResponseCodes
11
12
13__all__ = [
14    'DpaValidator',
15    'JsonValidator'
16]
17
18
19class DpaValidator:
20    """DPA message validator class."""
21
22    @staticmethod
23    def base_response_length(dpa: bytes) -> None:
24        """Check if DPA response length is at least equal to general response containing no data.
25
26        Args:
27            dpa (bytes): DPA response
28        Raises:
29            DpaResponsePacketLengthError: Raised if DPA response is shorter than general response containing no data
30        """
31        if len(dpa) < dpa_constants.RESPONSE_GENERAL_LEN:
32            raise DpaResponsePacketLengthError('DPA response packet too short.')
33
34    @staticmethod
35    def confirmation_length(dpa: bytes) -> None:
36        """Check if DPA confirmation message length is correct.
37
38        Args:
39            dpa (bytes): DPA confirmation
40        Raises:
41            DpaConfirmationPacketLengthError: Raised if DPA confirmation message length is not correct.
42        """
43        if len(dpa) != dpa_constants.CONFIRMATION_PACKET_LEN:
44            raise DpaConfirmationPacketLengthError('Invalid DPA confirmation packet length.')
45
46    @staticmethod
47    def confirmation_code(dpa: bytes) -> None:
48        """Check if DPA confirmation message code is correct.
49
50        Args:
51            dpa (bytes): DPA confirmation
52        Raises:
53            DpaConfirmationPacketError: Raised if DPA confirmation code is incorrect.
54        """
55        if dpa[dpa_constants.ResponsePacketMembers.RCODE] != ResponseCodes.CONFIRMATION:
56            raise DpaConfirmationPacketError('Invalid DPA confirmation packet error code.')
57
58    @staticmethod
59    def response_length(dpa: bytes, expected_len: int) -> None:
60        """Check if DPA response length equals expected length.
61
62        Args:
63            dpa (bytes): DPA response
64            expected_len (int): Expected response length
65        Raises
66            DpaResponsePacketLengthError: Raised if DPA response length does not equal expected length
67        """
68        if len(dpa) != expected_len:
69            raise DpaResponsePacketLengthError(f'DPA response packet length invalid, '
70                                               f'expected payload of {expected_len}B, got payload of {len(dpa)}B.')
71
72
73class JsonValidator:
74    """JSON API message validator class."""
75
76    @staticmethod
77    def response_received(json: dict) -> None:
78        """Check if DPA response has been received.
79
80        While DPA response may not be sent or received for one reason or another,
81        JSON API will always send a response and it is necessary to check for DPA response data.
82
83        Args:
84            json (dict): JSON API response
85        Raises:
86            MessageNotReceivedError: Raised if JSON API response has been received, but DPA response has not been received
87        """
88        status = Common.status_from_json(json)
89        msgid = Common.msgid_from_json(json)
90        if status < dpa_constants.ResponseCodes.OK:
91            raise MessageNotReceivedError('Response message not received.', msgid=msgid)
class DpaValidator:
20class DpaValidator:
21    """DPA message validator class."""
22
23    @staticmethod
24    def base_response_length(dpa: bytes) -> None:
25        """Check if DPA response length is at least equal to general response containing no data.
26
27        Args:
28            dpa (bytes): DPA response
29        Raises:
30            DpaResponsePacketLengthError: Raised if DPA response is shorter than general response containing no data
31        """
32        if len(dpa) < dpa_constants.RESPONSE_GENERAL_LEN:
33            raise DpaResponsePacketLengthError('DPA response packet too short.')
34
35    @staticmethod
36    def confirmation_length(dpa: bytes) -> None:
37        """Check if DPA confirmation message length is correct.
38
39        Args:
40            dpa (bytes): DPA confirmation
41        Raises:
42            DpaConfirmationPacketLengthError: Raised if DPA confirmation message length is not correct.
43        """
44        if len(dpa) != dpa_constants.CONFIRMATION_PACKET_LEN:
45            raise DpaConfirmationPacketLengthError('Invalid DPA confirmation packet length.')
46
47    @staticmethod
48    def confirmation_code(dpa: bytes) -> None:
49        """Check if DPA confirmation message code is correct.
50
51        Args:
52            dpa (bytes): DPA confirmation
53        Raises:
54            DpaConfirmationPacketError: Raised if DPA confirmation code is incorrect.
55        """
56        if dpa[dpa_constants.ResponsePacketMembers.RCODE] != ResponseCodes.CONFIRMATION:
57            raise DpaConfirmationPacketError('Invalid DPA confirmation packet error code.')
58
59    @staticmethod
60    def response_length(dpa: bytes, expected_len: int) -> None:
61        """Check if DPA response length equals expected length.
62
63        Args:
64            dpa (bytes): DPA response
65            expected_len (int): Expected response length
66        Raises
67            DpaResponsePacketLengthError: Raised if DPA response length does not equal expected length
68        """
69        if len(dpa) != expected_len:
70            raise DpaResponsePacketLengthError(f'DPA response packet length invalid, '
71                                               f'expected payload of {expected_len}B, got payload of {len(dpa)}B.')

DPA message validator class.

@staticmethod
def base_response_length(dpa: bytes) -> None:
23    @staticmethod
24    def base_response_length(dpa: bytes) -> None:
25        """Check if DPA response length is at least equal to general response containing no data.
26
27        Args:
28            dpa (bytes): DPA response
29        Raises:
30            DpaResponsePacketLengthError: Raised if DPA response is shorter than general response containing no data
31        """
32        if len(dpa) < dpa_constants.RESPONSE_GENERAL_LEN:
33            raise DpaResponsePacketLengthError('DPA response packet too short.')

Check if DPA response length is at least equal to general response containing no data.

Arguments:
  • dpa (bytes): DPA response
Raises:
  • DpaResponsePacketLengthError: Raised if DPA response is shorter than general response containing no data
@staticmethod
def confirmation_length(dpa: bytes) -> None:
35    @staticmethod
36    def confirmation_length(dpa: bytes) -> None:
37        """Check if DPA confirmation message length is correct.
38
39        Args:
40            dpa (bytes): DPA confirmation
41        Raises:
42            DpaConfirmationPacketLengthError: Raised if DPA confirmation message length is not correct.
43        """
44        if len(dpa) != dpa_constants.CONFIRMATION_PACKET_LEN:
45            raise DpaConfirmationPacketLengthError('Invalid DPA confirmation packet length.')

Check if DPA confirmation message length is correct.

Arguments:
  • dpa (bytes): DPA confirmation
Raises:
  • DpaConfirmationPacketLengthError: Raised if DPA confirmation message length is not correct.
@staticmethod
def confirmation_code(dpa: bytes) -> None:
47    @staticmethod
48    def confirmation_code(dpa: bytes) -> None:
49        """Check if DPA confirmation message code is correct.
50
51        Args:
52            dpa (bytes): DPA confirmation
53        Raises:
54            DpaConfirmationPacketError: Raised if DPA confirmation code is incorrect.
55        """
56        if dpa[dpa_constants.ResponsePacketMembers.RCODE] != ResponseCodes.CONFIRMATION:
57            raise DpaConfirmationPacketError('Invalid DPA confirmation packet error code.')

Check if DPA confirmation message code is correct.

Arguments:
  • dpa (bytes): DPA confirmation
Raises:
  • DpaConfirmationPacketError: Raised if DPA confirmation code is incorrect.
@staticmethod
def response_length(dpa: bytes, expected_len: int) -> None:
59    @staticmethod
60    def response_length(dpa: bytes, expected_len: int) -> None:
61        """Check if DPA response length equals expected length.
62
63        Args:
64            dpa (bytes): DPA response
65            expected_len (int): Expected response length
66        Raises
67            DpaResponsePacketLengthError: Raised if DPA response length does not equal expected length
68        """
69        if len(dpa) != expected_len:
70            raise DpaResponsePacketLengthError(f'DPA response packet length invalid, '
71                                               f'expected payload of {expected_len}B, got payload of {len(dpa)}B.')

Check if DPA response length equals expected length.

Arguments:
  • dpa (bytes): DPA response
  • expected_len (int): Expected response length

Raises DpaResponsePacketLengthError: Raised if DPA response length does not equal expected length

class JsonValidator:
74class JsonValidator:
75    """JSON API message validator class."""
76
77    @staticmethod
78    def response_received(json: dict) -> None:
79        """Check if DPA response has been received.
80
81        While DPA response may not be sent or received for one reason or another,
82        JSON API will always send a response and it is necessary to check for DPA response data.
83
84        Args:
85            json (dict): JSON API response
86        Raises:
87            MessageNotReceivedError: Raised if JSON API response has been received, but DPA response has not been received
88        """
89        status = Common.status_from_json(json)
90        msgid = Common.msgid_from_json(json)
91        if status < dpa_constants.ResponseCodes.OK:
92            raise MessageNotReceivedError('Response message not received.', msgid=msgid)

JSON API message validator class.

@staticmethod
def response_received(json: dict) -> None:
77    @staticmethod
78    def response_received(json: dict) -> None:
79        """Check if DPA response has been received.
80
81        While DPA response may not be sent or received for one reason or another,
82        JSON API will always send a response and it is necessary to check for DPA response data.
83
84        Args:
85            json (dict): JSON API response
86        Raises:
87            MessageNotReceivedError: Raised if JSON API response has been received, but DPA response has not been received
88        """
89        status = Common.status_from_json(json)
90        msgid = Common.msgid_from_json(json)
91        if status < dpa_constants.ResponseCodes.OK:
92            raise MessageNotReceivedError('Response message not received.', msgid=msgid)

Check if DPA response has been received.

While DPA response may not be sent or received for one reason or another, JSON API will always send a response and it is necessary to check for DPA response data.

Arguments:
  • json (dict): JSON API response
Raises:
  • MessageNotReceivedError: Raised if JSON API response has been received, but DPA response has not been received