1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
# pylint: disable=too-many-lines
import logging
from uamqp import c_uamqp, constants, errors, utils
_logger = logging.getLogger(__name__)
class Message(object):
"""An AMQP message.
When sending, if body type information is not provided,
then depending on the nature of the data,
different body encoding will be used. If the data is str or bytes,
a single part DataBody will be sent. If the data is a list of str/bytes,
a multipart DataBody will be sent. Any other type of list or any other
type of data will be sent as a ValueBody.
An empty payload will also be sent as a ValueBody.
If body type information is provided, then the Message will use the given
body type to encode the data or raise error if the data doesn't match the body type.
:ivar on_send_complete: A custom callback to be run on completion of
the send operation of this message. The callback must take two parameters,
a result (of type `MessageSendResult`) and an error (of type
Exception). The error parameter may be None if no error ocurred or the error
information was undetermined.
:vartype on_send_complete: callable[~uamqp.constants.MessageSendResult, Exception]
:param body: The data to send in the message.
:type body: Any Python data type.
:param properties: Properties to add to the message.
:type properties: ~uamqp.message.MessageProperties
:param application_properties: Service specific application properties.
:type application_properties: dict
:param annotations: Service specific message annotations. Keys in the dictionary
must be `types.AMQPSymbol` or `types.AMQPuLong`.
:type annotations: dict
:param header: The message header.
:type header: ~uamqp.message.MessageHeader
:param msg_format: A custom message format. Default is 0.
:type msg_format: int
:param message: Internal only. This is used to wrap an existing message
that has been received from an AMQP service. If specified, all other
parameters will be ignored.
:type message: uamqp.c_uamqp.cMessage
:param settler: Internal only. This is used when wrapping an existing message
that has been received from an AMQP service. Should only be specified together
with `message` and is to settle the message.
:type settler: callable[~uamqp.errors.MessageResponse]
:param delivery_no: Internal only. This is used when wrapping an existing message
that has been received from an AMQP service. Should only be specified together
with `message` and specifies the messages client delivery number.
:param encoding: The encoding to use for parameters supplied as strings.
Default is 'UTF-8'
:type encoding: str
:param body_type: The AMQP body type used to specify the type of the body section of an amqp message.
By default is None which means depending on the nature of the data,
different body encoding will be used. If the data is str or bytes,
a single part DataBody will be sent. If the data is a list of str/bytes,
a multipart DataBody will be sent. Any other type of list or any other
type of data will be sent as a ValueBody. An empty payload will also be sent as a ValueBody.
Please check class ~uamqp.MessageBodyType for usage information of each body type.
:type body_type: ~uamqp.MessageBodyType
:param footer: The message footer.
:type footer: dict
:param delivery_annotations: Service specific delivery annotations.
:type delivery_annotations: dict
"""
def __init__(
self,
body=None,
properties=None,
application_properties=None,
annotations=None,
header=None,
msg_format=None,
message=None,
settler=None,
delivery_no=None,
encoding='UTF-8',
body_type=None,
footer=None,
delivery_annotations=None
):
self.state = constants.MessageState.WaitingToBeSent
self.idle_time = 0
self.retries = 0
self._response = None
self._settler = None
self._encoding = encoding
self.delivery_no = delivery_no
self.delivery_tag = None
self.on_send_complete = None
self._properties = None
self._application_properties = None
self._annotations = None
self._header = None
self._footer = None
self._delivery_annotations = None
self._need_further_parse = False
if message:
if settler:
self.state = constants.MessageState.ReceivedUnsettled
self._response = None
else:
self.state = constants.MessageState.ReceivedSettled
self._response = errors.MessageAlreadySettled()
self._settler = settler
self._parse_message_body(message)
else:
self._message = c_uamqp.create_message()
# if body_type is not given, we detect the body type by checking the type of the object
if not body_type:
self._auto_set_body(body)
else:
self._set_body_by_body_type(body, body_type)
if msg_format:
self._message.message_format = msg_format
self._properties = properties
self._application_properties = application_properties
self._annotations = annotations
self._delivery_annotations = delivery_annotations
self._header = header
self._footer = footer
def __getstate__(self):
state = self.__dict__.copy()
state["state"] = self.state.value
state["_message"] = None
state["_body_type"] = self._body.type.value if self._body else None
if isinstance(self._body, (DataBody, SequenceBody)):
state["_body"] = list(self._body.data)
elif isinstance(self._body, ValueBody):
state["_body"] = self._body.data
return state
def __setstate__(self, state):
state["state"] = constants.MessageState(state.get("state"))
self.__dict__.update(state)
body = state.get("_body")
body_type = constants.BODY_TYPE_C_PYTHON_MAP.get(state.get("_body_type"))
self._message = c_uamqp.create_message()
if body:
if not body_type:
self._auto_set_body(body)
else:
self._set_body_by_body_type(body, body_type)
@property
def properties(self):
if self._need_further_parse:
self._parse_message_properties()
return self._properties
@properties.setter
def properties(self, value):
if value and not isinstance(value, MessageProperties):
raise TypeError("Properties must be a MessageProperties.")
self._properties = value
@property
def header(self):
if self._need_further_parse:
self._parse_message_properties()
return self._header
@header.setter
def header(self, value):
if value and not isinstance(value, MessageHeader):
raise TypeError("Header must be a MessageHeader.")
self._header = value
@property
def footer(self):
if self._need_further_parse:
self._parse_message_properties()
return self._footer
@footer.setter
def footer(self, value):
if value and not isinstance(value, dict):
raise TypeError("Footer must be a dictionary")
footer_props = c_uamqp.create_footer(
utils.data_factory(value, encoding=self._encoding)
)
self._message.footer = footer_props
self._footer = value
@property
def application_properties(self):
if self._need_further_parse:
self._parse_message_properties()
return self._application_properties
@application_properties.setter
def application_properties(self, value):
if value and not isinstance(value, dict):
raise TypeError("Application properties must be a dictionary.")
self._application_properties = value
@property
def annotations(self):
if self._need_further_parse:
self._parse_message_properties()
return self._annotations
@annotations.setter
def annotations(self, value):
if value and not isinstance(value, dict):
raise TypeError("Message annotations must be a dictionary.")
self._annotations = value
@property
def message_annotations(self):
return self.annotations
@property
def delivery_annotations(self):
if self._need_further_parse:
self._parse_message_properties()
return self._delivery_annotations
@delivery_annotations.setter
def delivery_annotations(self, value):
self._delivery_annotations = value
@property
def data(self):
if not self._message or not self._body:
return None
# pylint: disable=protected-access
if self._body.type == c_uamqp.MessageBodyType.DataType:
return self._body.data
return None
@property
def sequence(self):
try:
if not self._message or not self._body:
return None
except AttributeError:
return None
# pylint: disable=protected-access
if self._body.type == c_uamqp.MessageBodyType.SequenceType:
return self._body.data
return None
@property
def value(self):
try:
if not self._message or not self._body:
return None
except AttributeError:
return None
# pylint: disable=protected-access
if self._body.type == c_uamqp.MessageBodyType.ValueType:
return self._body.data
return None
@classmethod
def decode_from_bytes(cls, data):
"""Decode an AMQP message from a bytearray.
The returned message will not have a delivery context and
therefore will be considered to be in an "already settled" state.
:param data: The AMQP wire-encoded bytes to decode.
:type data: bytes or bytearray
"""
decoded_message = c_uamqp.decode_message(len(data), data)
return cls(message=decoded_message)
def __str__(self):
if not self._message:
return ""
return str(self._body)
def _parse_message_properties(self):
if self._need_further_parse:
_props = self._message.properties
if _props:
_logger.debug(
"Parsing received message properties %r.", self.delivery_no
)
self._properties = MessageProperties(
properties=_props, encoding=self._encoding
)
_header = self._message.header
if _header:
_logger.debug("Parsing received message header %r.", self.delivery_no)
self._header = MessageHeader(header=_header)
_footer = self._message.footer
if _footer:
_logger.debug("Parsing received message footer %r.", self.delivery_no)
self._footer = _footer.map
_app_props = self._message.application_properties
if _app_props:
_logger.debug(
"Parsing received message application properties %r.",
self.delivery_no,
)
self._application_properties = _app_props.map
_ann = self._message.message_annotations
if _ann:
_logger.debug(
"Parsing received message annotations %r.", self.delivery_no
)
self._annotations = _ann.map
_delivery_ann = self._message.delivery_annotations
if _delivery_ann:
_logger.debug(
"Parsing received message delivery annotations %r.",
self.delivery_no,
)
self._delivery_annotations = _delivery_ann.map
self._need_further_parse = False
def _parse_message_body(self, message):
"""Parse a message received from an AMQP service.
:param message: The received C message.
:type message: uamqp.c_uamqp.cMessage
"""
_logger.debug("Parsing received message %r.", self.delivery_no)
self._message = message
delivery_tag = self._message.delivery_tag
if delivery_tag:
self.delivery_tag = delivery_tag.value
body_type = message.body_type
if body_type == c_uamqp.MessageBodyType.NoneType:
self._body = None
elif body_type == c_uamqp.MessageBodyType.DataType:
self._body = DataBody(self._message)
elif body_type == c_uamqp.MessageBodyType.SequenceType:
self._body = SequenceBody(self._message)
else:
self._body = ValueBody(self._message)
self._need_further_parse = True
def _can_settle_message(self):
if self.state not in constants.RECEIVE_STATES:
raise TypeError("Only received messages can be settled.")
if self.settled:
return False
return True
def _auto_set_body(self, body):
"""
Automatically detect the MessageBodyType and set data when no body type information is provided.
We categorize object of type list/list of lists into ValueType (not into SequenceType) due to
compatibility with old uamqp version.
"""
if isinstance(body, (str, bytes)):
self._body = DataBody(self._message)
self._body.append(body)
elif isinstance(body, list) and all([isinstance(b, (str, bytes)) for b in body]):
self._body = DataBody(self._message)
for value in body:
self._body.append(value)
else:
self._body = ValueBody(self._message)
self._body.set(body)
def _set_body_by_body_type(self, body, body_type):
if body_type == constants.MessageBodyType.Data:
self._body = DataBody(self._message)
if isinstance(body, (str, bytes)):
self._body.append(body)
elif isinstance(body, list) and all([isinstance(b, (str, bytes)) for b in body]):
for value in body:
self._body.append(value)
else:
raise TypeError(
"For MessageBodyType.Data, the body"
" must be str or bytes or list of str or bytes.")
elif body_type == constants.MessageBodyType.Sequence:
self._body = SequenceBody(self._message)
if isinstance(body, list) and all([isinstance(b, list) for b in body]):
for value in body:
self._body.append(value)
elif isinstance(body, list):
self._body.append(body)
else:
raise TypeError(
"For MessageBodyType.Sequence, the body"
" must be list or list of lists.")
elif body_type == constants.MessageBodyType.Value:
self._body = ValueBody(self._message)
self._body.set(body)
else:
raise ValueError("Unsupported MessageBodyType: {}".format(body_type))
def _populate_message_attributes(self, c_message):
if self.properties:
c_message.properties = self.properties.get_properties_obj()
if self.application_properties:
if not isinstance(self.application_properties, dict):
raise TypeError("Application properties must be a dictionary.")
amqp_props = utils.data_factory(
self.application_properties, encoding=self._encoding
)
c_message.application_properties = amqp_props
if self.annotations:
if not isinstance(self.annotations, dict):
raise TypeError("Message annotations must be a dictionary.")
ann_props = c_uamqp.create_message_annotations(
utils.data_factory(self.annotations, encoding=self._encoding)
)
c_message.message_annotations = ann_props
if self.delivery_annotations:
if not isinstance(self.delivery_annotations, dict):
raise TypeError("Delivery annotations must be a dictionary.")
delivery_ann_props = c_uamqp.create_delivery_annotations(
utils.data_factory(self.delivery_annotations, encoding=self._encoding)
)
c_message.delivery_annotations = delivery_ann_props
if self.header:
c_message.header = self.header.get_header_obj()
if self.footer:
if not isinstance(self.footer, dict):
raise TypeError("Footer must be a dictionary.")
footer = c_uamqp.create_footer(
utils.data_factory(self.footer, encoding=self._encoding)
)
c_message.footer = footer
@property
def settled(self):
"""Whether the message transaction for this message has been completed.
If this message is to be sent, the message will be `settled=True` once a
disposition has been received from the service.
If this message has been received, the message will be `settled=True` once
a disposition has been sent to the service.
:rtype: bool
"""
if self._response:
return True
return False
def get_message_encoded_size(self):
"""Pre-emptively get the size of the message once it has been encoded
to go over the wire so we can raise an error if the message will be
rejected for being to large.
This method is not available for messages that have been received.
:rtype: int
"""
if not self._message:
raise ValueError("No message data to encode.")
cloned_data = self._message.clone()
self._populate_message_attributes(cloned_data)
encoded_data = []
return c_uamqp.get_encoded_message_size(cloned_data, encoded_data)
def encode_message(self):
"""Encode message to AMQP wire-encoded bytearray.
:rtype: bytearray
"""
if not self._message:
raise ValueError("No message data to encode.")
cloned_data = self._message.clone()
self._populate_message_attributes(cloned_data)
encoded_data = []
c_uamqp.get_encoded_message_size(cloned_data, encoded_data)
return b"".join(encoded_data)
def get_data(self):
"""Get the body data of the message. The format may vary depending
on the body type.
:rtype: generator
"""
if not self._message or not self._body:
return None
return self._body.data
def gather(self):
"""Return all the messages represented by this object.
This will always be a list of a single message.
:rtype: list[~uamqp.message.Message]
"""
if self.state in constants.RECEIVE_STATES:
raise TypeError("Only new messages can be gathered.")
if not self._message:
raise ValueError("Message data already consumed.")
try:
raise self._response
except TypeError:
pass
return [self]
def get_message(self):
"""Get the underlying C message from this object.
:rtype: uamqp.c_uamqp.cMessage
"""
if not self._message:
return None
self._populate_message_attributes(self._message)
return self._message
def accept(self):
"""Send a response disposition to the service to indicate that
a received message has been accepted. If the client is running in PeekLock
mode, the service will wait on this disposition. Otherwise it will
be ignored. Returns `True` is message was accepted, or `False` if the message
was already settled.
:rtype: bool
:raises: TypeError if the message is being sent rather than received.
"""
if self._can_settle_message():
self._response = errors.MessageAccepted()
self._settler(self._response)
self.state = constants.MessageState.ReceivedSettled
return True
return False
def reject(self, condition=None, description=None, info=None):
"""Send a response disposition to the service to indicate that
a received message has been rejected. If the client is running in PeekLock
mode, the service will wait on this disposition. Otherwise it will
be ignored. A rejected message will increment the messages delivery count.
Returns `True` is message was rejected, or `False` if the message
was already settled.
:param condition: The AMQP rejection code. By default this is `amqp:internal-error`.
:type condition: bytes or str
:param description: A description/reason to accompany the rejection.
:type description: bytes or str
:param info: Information about the error condition.
:type info: dict
:rtype: bool
:raises: TypeError if the message is being sent rather than received.
"""
if self._can_settle_message():
self._response = errors.MessageRejected(
condition=condition,
description=description,
info=info,
encoding=self._encoding,
)
self._settler(self._response)
self.state = constants.MessageState.ReceivedSettled
return True
return False
def release(self):
"""Send a response disposition to the service to indicate that
a received message has been released. If the client is running in PeekLock
mode, the service will wait on this disposition. Otherwise it will
be ignored. A released message will not incremenet the messages
delivery count. Returns `True` is message was released, or `False` if the message
was already settled.
:rtype: bool
:raises: TypeError if the message is being sent rather than received.
"""
if self._can_settle_message():
self._response = errors.MessageReleased()
self._settler(self._response)
self.state = constants.MessageState.ReceivedSettled
return True
return False
def modify(self, failed, deliverable, annotations=None):
"""Send a response disposition to the service to indicate that
a received message has been modified. If the client is running in PeekLock
mode, the service will wait on this disposition. Otherwise it will
be ignored. Returns `True` is message was modified, or `False` if the message
was already settled.
:param failed: Whether this delivery of this message failed. This does not
indicate whether subsequence deliveries of this message would also fail.
:type failed: bool
:param deliverable: Whether this message will be deliverable to this client
on subsequent deliveries - i.e. whether delivery is retryable.
:type deliverable: bool
:param annotations: Annotations to attach to response.
:type annotations: dict
:rtype: bool
:raises: TypeError if the message is being sent rather than received.
"""
if self._can_settle_message():
self._response = errors.MessageModified(
failed, deliverable, annotations=annotations, encoding=self._encoding
)
self._settler(self._response)
self.state = constants.MessageState.ReceivedSettled
return True
return False
class BatchMessage(Message):
"""A Batched AMQP message.
This batch message encodes multiple message bodies into a single message
to increase through-put over the wire. It requires server-side support
to unpackage the batched messages and so will not be universally supported.
:ivar on_send_complete: A custom callback to be run on completion of
the send operation of this message. The callback must take two parameters,
a result (of type ~uamqp.constants.MessageSendResult) and an error (of type
Exception). The error parameter may be None if no error ocurred or the error
information was undetermined.
:vartype on_send_complete: callable[~uamqp.constants.MessageSendResult, Exception]
:ivar batch_format: The is the specific message format to inform the service the
the body should be interpreted as multiple messages. The value is 0x80013700.
:vartype batch_format: int
:ivar max_message_length: The maximum data size in bytes to allow in a single message.
By default this is 256kb. If sending a single batch message, an error will be raised
if the supplied data exceeds this maximum. If sending multiple batch messages, this
value will be used to divide the supplied data between messages.
:vartype max_message_length: int
:param data: An iterable source of data, where each value will be considered the
body of a single message in the batch.
:type data: iterable
:param properties: Properties to add to the message. If multiple messages are created
these properties will be applied to each message.
:type properties: ~uamqp.message.MessageProperties
:param application_properties: Service specific application properties. If multiple messages
are created these properties will be applied to each message.
:type application_properties: dict
:param annotations: Service specific message annotations. If multiple messages are created
these properties will be applied to each message. Keys in the dictionary
must be `types.AMQPSymbol` or `types.AMQPuLong`.
:type annotations: dict
:param header: The message header. This header will be applied to each message in the batch.
:type header: ~uamqp.message.MessageHeader
:param multi_messages: Whether to send the supplied data across multiple messages. If set to
`False`, all the data will be sent in a single message, and an error raised if the message
is too large. If set to `True`, the data will automatically be divided across multiple messages
of an appropriate size. The default is `False`.
:type multi_messages: bool
:param encoding: The encoding to use for parameters supplied as strings.
Default is 'UTF-8'
:type encoding: str
:raises: ValueError if data is sent in a single message and that message exceeds the max size.
"""
batch_format = 0x80013700
max_message_length = constants.MAX_MESSAGE_LENGTH_BYTES
size_offset = 0
def __init__(
self,
data=None,
properties=None,
application_properties=None,
annotations=None,
header=None,
multi_messages=False,
encoding="UTF-8",
):
# pylint: disable=super-init-not-called
self._multi_messages = multi_messages
self._body_gen = data
self._encoding = encoding
self.on_send_complete = None
self._properties = properties
self._application_properties = application_properties
self._annotations = annotations
self._header = header
self._need_further_parse = False
def __getstate__(self):
state = self.__dict__.copy()
return state
def __setstate__(self, state):
self.__dict__.update(state)
def _create_batch_message(self):
"""Create a ~uamqp.message.Message for a value supplied by the data
generator. Applies all properties and annotations to the message.
:rtype: ~uamqp.message.Message
"""
return Message(
body=[],
properties=self.properties,
annotations=self.annotations,
msg_format=self.batch_format,
header=self.header,
encoding=self._encoding,
)
def _multi_message_generator(self):
"""Generate multiple ~uamqp.message.Message objects from a single data
stream that in total may exceed the maximum individual message size.
Data will be continuously added to a single message until that message
reaches a max allowable size, at which point it will be yielded and
a new message will be started.
:rtype: generator[~uamqp.message.Message]
"""
unappended_message_bytes = None
while True:
new_message = self._create_batch_message()
message_size = new_message.get_message_encoded_size() + self.size_offset
body_size = 0
if unappended_message_bytes:
new_message._body.append( # pylint: disable=protected-access
unappended_message_bytes
)
body_size += len(unappended_message_bytes)
try:
for data in self._body_gen:
message_bytes = None
try:
# try to get the internal uamqp Message
internal_uamqp_message = data.message
except AttributeError:
# no inernal message, data could be uamqp Message or raw data
internal_uamqp_message = data
try:
# uamqp Message
if (
not internal_uamqp_message.application_properties
and self.application_properties
):
internal_uamqp_message.application_properties = (
self.application_properties
)
message_bytes = internal_uamqp_message.encode_message()
except AttributeError: # raw data
wrap_message = Message(
body=internal_uamqp_message,
application_properties=self.application_properties,
)
message_bytes = wrap_message.encode_message()
body_size += len(message_bytes)
if (body_size + message_size) > self.max_message_length:
new_message.on_send_complete = self.on_send_complete
unappended_message_bytes = message_bytes
yield new_message
raise StopIteration()
new_message._body.append( # pylint: disable=protected-access
message_bytes
)
except StopIteration:
_logger.debug("Sent partial message.")
continue
else:
new_message.on_send_complete = self.on_send_complete
yield new_message
_logger.debug("Sent all batched data.")
break
@property
def data(self):
"""Returns an iterable source of data, where each value will be considered the
body of a single message in the batch.
:rtype: iterable
"""
return self._body_gen
def gather(self):
"""Return all the messages represented by this object. This will convert
the batch data into individual Message objects, which may be one
or more if multi_messages is set to `True`.
:rtype: list[~uamqp.message.Message]
"""
if self._multi_messages:
return self._multi_message_generator()
new_message = self._create_batch_message()
message_size = new_message.get_message_encoded_size() + self.size_offset
body_size = 0
for data in self._body_gen:
message_bytes = None
try:
# try to get the internal uamqp Message
internal_uamqp_message = data.message
except AttributeError:
# no inernal message, data could be uamqp Message or raw data
internal_uamqp_message = data
try:
# uamqp Message
if (
not internal_uamqp_message.application_properties
and self.application_properties
):
internal_uamqp_message.application_properties = (
self.application_properties
)
message_bytes = internal_uamqp_message.encode_message()
except AttributeError: # raw data
wrap_message = Message(
body=internal_uamqp_message,
application_properties=self.application_properties,
)
message_bytes = wrap_message.encode_message()
body_size += len(message_bytes)
if (body_size + message_size) > self.max_message_length:
raise errors.MessageContentTooLarge()
new_message._body.append(message_bytes) # pylint: disable=protected-access
new_message.on_send_complete = self.on_send_complete
return [new_message]
class MessageProperties(object):
"""Message properties.
The properties that are actually used will depend on the service implementation.
Not all received messages will have all properties, and not all properties
will be utilized on a sent message.
:ivar message_id: Message-id, if set, uniquely identifies a message within the message system.
The message producer is usually responsible for setting the message-id in such a way that it
is assured to be globally unique. A broker MAY discard a message as a duplicate if the value
of the message-id matches that of a previously received message sent to the same node.
:vartype message_id: str or bytes or uuid.UUID or ~uamqp.types.AMQPType
:ivar user_id: The identity of the user responsible for producing the message. The client sets
this value, and it MAY be authenticated by intermediaries.
:vartype user_id: str or bytes
:ivar to: The to field identifies the node that is the intended destination of the message.
On any given transfer this might not be the node at the receiving end of the link.
:vartype to: str or bytes
:ivar subject:
:vartype subject:
:ivar reply_to:
:vartype reply_to:
:ivar correlation_id:
:vartype correlation_id:
:ivar content_type:
:vartype content_type:
:ivar content_encoding:
:vartype content_encoding:
:ivar absolute_expiry_time:
:vartype absolute_expiry_time:
:ivar creation_time:
:vartype creation_time:
:ivar group_id:
:vartype group_id:
:ivar group_sequence:
:vartype group_sequence:
:ivar reply_to_group_id:
:vartype reply_to_group_id:
"""
def __init__(
self,
message_id=None,
user_id=None,
to=None,
subject=None,
reply_to=None,
correlation_id=None,
content_type=None,
content_encoding=None,
absolute_expiry_time=None,
creation_time=None,
group_id=None,
group_sequence=None,
reply_to_group_id=None,
properties=None,
encoding="UTF-8",
):
self._encoding = encoding
if properties:
self._message_id = properties.message_id
self._user_id = properties.user_id
self._to = properties.to
self._subject = properties.subject
self._reply_to = properties.reply_to
self._correlation_id = properties.correlation_id
self._content_type = properties.content_type
self._content_encoding = properties.content_encoding
self._absolute_expiry_time = properties.absolute_expiry_time
self._creation_time = properties.creation_time
self._group_id = properties.group_id
self._group_sequence = properties.group_sequence
self._reply_to_group_id = properties.reply_to_group_id
else:
self.message_id = message_id
self.user_id = user_id
self.to = to
self.subject = subject
self.reply_to = reply_to
self.correlation_id = correlation_id
self.content_type = content_type
self.content_encoding = content_encoding
self.absolute_expiry_time = absolute_expiry_time
self.creation_time = creation_time
self.group_id = group_id
self.group_sequence = group_sequence
self.reply_to_group_id = reply_to_group_id
def __str__(self):
return str(
{
"message_id": self.message_id,
"user_id": self.user_id,
"to": self.to,
"subject": self.subject,
"reply_to": self.reply_to,
"correlation_id": self.correlation_id,
"content_type": self.content_type,
"content_encoding": self.content_encoding,
"absolute_expiry_time": self.absolute_expiry_time,
"creation_time": self.creation_time,
"group_id": self.group_id,
"group_sequence": self.group_sequence,
"reply_to_group_id": self.reply_to_group_id,
}
)
def __getstate__(self):
state = self._get_properties_dict()
state["_encoding"] = self._encoding
return state
def __setstate__(self, state):
self._encoding = state.pop("_encoding")
for key, val in state.items():
self.__setattr__(key, val)
@property
def message_id(self):
if self._message_id:
return self._message_id.value
return None
@message_id.setter
def message_id(self, value):
if value is None:
self._message_id = None
else:
self._message_id = utils.data_factory(value, encoding=self._encoding)
@property
def user_id(self):
try:
if self._user_id is not None:
return self._user_id.value
return None
except AttributeError:
return self._user_id
@user_id.setter
def user_id(self, value):
if isinstance(value, str):
value = value.encode(self._encoding)
elif value is not None and not isinstance(value, bytes):
raise TypeError("user_id must be bytes or str.")
# user_id is type of binary according to the spec.
# convert byte string into bytearray then wrap the data into c_uamqp.BinaryValue.
if value is not None:
self._user_id = utils.data_factory(
bytearray(value), encoding=self._encoding
)
else:
self._user_id = None
@property
def to(self):
if self._to:
return self._to.value
return None
@to.setter
def to(self, value):
if value is None:
self._to = None
else:
self._to = utils.data_factory(value, encoding=self._encoding)
@property
def subject(self):
return self._subject
@subject.setter
def subject(self, value):
if isinstance(value, str):
value = value.encode(self._encoding)
elif value is not None and not isinstance(value, bytes):
raise TypeError("subject must be bytes or str.")
self._subject = value
@property
def reply_to(self):
if self._reply_to is not None:
return self._reply_to.value
return None
@reply_to.setter
def reply_to(self, value):
if value is None:
self._reply_to = None
else:
self._reply_to = utils.data_factory(value, encoding=self._encoding)
@property
def correlation_id(self):
if self._correlation_id is not None:
return self._correlation_id.value
return None
@correlation_id.setter
def correlation_id(self, value):
if value is None:
self._correlation_id = None
else:
self._correlation_id = utils.data_factory(value, encoding=self._encoding)
@property
def content_type(self):
return self._content_type
@content_type.setter
def content_type(self, value):
if isinstance(value, str):
value = value.encode(self._encoding)
elif value is not None and not isinstance(value, bytes):
raise TypeError("content_type must be bytes or str.")
self._content_type = value
@property
def content_encoding(self):
return self._content_encoding
@content_encoding.setter
def content_encoding(self, value):
if isinstance(value, str):
value = value.encode(self._encoding)
elif value is not None and not isinstance(value, bytes):
raise TypeError("content_encoding must be bytes or str.")
self._content_encoding = value
@property
def absolute_expiry_time(self):
return self._absolute_expiry_time
@absolute_expiry_time.setter
def absolute_expiry_time(self, value):
if value is not None and not isinstance(value, int):
raise TypeError("absolute_expiry_time must be an integer.")
self._absolute_expiry_time = value
@property
def creation_time(self):
return self._creation_time
@creation_time.setter
def creation_time(self, value):
if value is not None and not isinstance(value, int):
raise TypeError("creation_time must be an integer.")
self._creation_time = value
@property
def group_id(self):
return self._group_id
@group_id.setter
def group_id(self, value):
if isinstance(value, str):
value = value.encode(self._encoding)
elif value is not None and not isinstance(value, bytes):
raise TypeError("group_id must be bytes or str.")
self._group_id = value
@property
def group_sequence(self):
return self._group_sequence
@group_sequence.setter
def group_sequence(self, value):
if value is not None and not isinstance(value, int):
raise TypeError("group_sequence must be an integer.")
self._group_sequence = value
@property
def reply_to_group_id(self):
return self._reply_to_group_id
@reply_to_group_id.setter
def reply_to_group_id(self, value):
if isinstance(value, str):
value = value.encode(self._encoding)
elif value is not None and not isinstance(value, bytes):
raise TypeError("reply_to_group_id must be bytes or str.")
self._reply_to_group_id = value
def _set_attr(self, attr, properties):
attr_value = getattr(self, "_" + attr)
if attr_value is not None:
setattr(properties, attr, attr_value)
def _get_properties_dict(self):
return {
"message_id": self.message_id,
"user_id": self.user_id,
"to": self.to,
"subject": self.subject,
"reply_to": self.reply_to,
"correlation_id": self.correlation_id,
"content_type": self.content_type,
"content_encoding": self.content_encoding,
"absolute_expiry_time": self.absolute_expiry_time,
"creation_time": self.creation_time,
"group_id": self.group_id,
"group_sequence": self.group_sequence,
"reply_to_group_id": self.reply_to_group_id,
}
def get_properties_obj(self):
"""Get the underlying C reference from this object.
:rtype: uamqp.c_uamqp.cProperties
"""
properties = c_uamqp.cProperties()
self._set_attr("message_id", properties)
self._set_attr("user_id", properties)
self._set_attr("to", properties)
self._set_attr("subject", properties)
self._set_attr("reply_to", properties)
self._set_attr("correlation_id", properties)
self._set_attr("content_type", properties)
self._set_attr("content_encoding", properties)
self._set_attr("absolute_expiry_time", properties)
self._set_attr("creation_time", properties)
self._set_attr("group_id", properties)
self._set_attr("group_sequence", properties)
self._set_attr("reply_to_group_id", properties)
return properties
class MessageBody(object):
"""Base class for an AMQP message body. This should
not be used directly.
"""
def __init__(self, c_message, encoding="UTF-8"):
self._message = c_message
self._encoding = encoding
@property
def type(self):
return self._message.body_type
@property
def data(self):
raise NotImplementedError("Only MessageBody subclasses have data.")
class DataBody(MessageBody):
"""An AMQP message body of type Data. This represents
a list of bytes sections.
:ivar type: The body type. This should always be `DataType`.
:vartype type: uamqp.c_uamqp.MessageBodyType
:ivar data: The data contained in the message body. This returns
a generator to iterate over each section in the body, where
each section will be a byte string.
:vartype data: Generator[bytes]
"""
def __str__(self):
return "".join(d.decode(self._encoding) for d in self.data)
def __unicode__(self):
return u"".join(d.decode(self._encoding) for d in self.data)
def __bytes__(self):
return b"".join(self.data)
def __len__(self):
return self._message.count_body_data()
def __getitem__(self, index):
if index >= len(self):
raise IndexError("Index is out of range.")
data = self._message.get_body_data(index)
return data.value
def append(self, data):
"""Append a section to the body.
:param data: The data to append.
:type data: str or bytes
"""
if isinstance(data, str):
self._message.add_body_data(data.encode(self._encoding))
elif isinstance(data, bytes):
self._message.add_body_data(data)
@property
def data(self):
for i in range(len(self)):
yield self._message.get_body_data(i)
class ValueBody(MessageBody):
"""An AMQP message body of type Value. This represents
a single encoded object.
:ivar type: The body type. This should always be ValueType
:vartype type: uamqp.c_uamqp.MessageBodyType
:ivar data: The data contained in the message body. The value
of the encoded object
:vartype data: object
"""
def __str__(self):
data = self.data
if not data:
return ""
if isinstance(data, bytes):
return data.decode(self._encoding)
return str(data)
def __unicode__(self):
data = self.data
if not data:
return u""
if isinstance(data, bytes):
return data.decode(self._encoding)
return unicode(data) # pylint: disable=undefined-variable
def __bytes__(self):
data = self.data
if not data:
return b""
return bytes(data)
def set(self, value):
"""Set a value as the message body. This can be any
Python data type and it will be automatically encoded
into an AMQP type. If a specific AMQP type is required, a
`types.AMQPType` can be used.
:param data: The data to send in the body.
:type data: ~uamqp.types.AMQPType
"""
value = utils.data_factory(value)
self._message.set_body_value(value)
@property
def data(self):
_value = self._message.get_body_value()
if _value:
return _value.value
return None
class SequenceBody(MessageBody):
"""An AMQP message body of type Sequence. This represents
a list of sequence sections.
:ivar type: The body type. This should always be SequenceType
:vartype type: uamqp.c_uamqp.MessageBodyType
:ivar data: The data contained in the message body. This returns
a generator to iterate over each section in the body, where
each section will be a list of objects.
:vartype data: Generator[List[object]]
"""
def __str__(self):
output_str = ""
for sequence_section in self.data:
for d in sequence_section:
if isinstance(d, bytes):
output_str += d.decode(self._encoding)
else:
output_str += str(d)
return output_str
def __unicode__(self):
output_unicode = u""
for sequence_section in self.data:
for d in sequence_section:
if isinstance(d, bytes):
output_unicode += d.decode(self._encoding)
else:
output_unicode += unicode(d) # pylint: disable=undefined-variable
return output_unicode
def __bytes__(self):
return b"".join(bytes(d) for sequence_section in self.data for d in sequence_section)
def __len__(self):
return self._message.count_body_sequence()
def __getitem__(self, index):
if index >= len(self):
raise IndexError("Index is out of range.")
data = self._message.get_body_sequence(index)
return data.value
def append(self, data):
"""Append a sequence section to the body. The data
should be a list of objects. The object in the list
can be any Python data type and it will be automatically
encoded into an AMQP type. If a specific AMQP type
is required, a `types.AMQPType` can be used.
:param data: The list of objects to append.
:type data: list[~uamqp.types.AMQPType]
"""
data = utils.data_factory(data)
self._message.add_body_sequence(data)
@property
def data(self):
for i in range(len(self)):
yield self._message.get_body_sequence(i).value
class MessageHeader(object):
"""The Message header. This is only used on received message, and not
set on messages being sent. The properties set on any given message
will depend on the Service and not all messages will have all properties.
:ivar delivery_count: The number of unsuccessful previous attempts to deliver
this message. If this value is non-zero it can be taken as an indication that the
delivery might be a duplicate. On first delivery, the value is zero. It is
incremented upon an outcome being settled at the sender, according to rules
defined for each outcome.
:vartype delivery_count: int
:ivar time_to_live: Duration in milliseconds for which the message is to be considered "live".
If this is set then a message expiration time will be computed based on the time of arrival
at an intermediary. Messages that live longer than their expiration time will be discarded
(or dead lettered). When a message is transmitted by an intermediary that was received
with a ttl, the transmitted message's header SHOULD contain a ttl that is computed as the
difference between the current time and the formerly computed message expiration time,
i.e., the reduced ttl, so that messages will eventually die if they end up in a delivery loop.
:vartype time_to_live: int
:ivar durable: Durable messages MUST NOT be lost even if an intermediary is unexpectedly terminated
and restarted. A target which is not capable of fulfilling this guarantee MUST NOT accept messages
where the durable header is set to `True`: if the source allows the rejected outcome then the
message SHOULD be rejected with the precondition-failed error, otherwise the link MUST be detached
by the receiver with the same error.
:vartype durable: bool
:ivar first_acquirer: If this value is `True`, then this message has not been acquired
by any other link. If this value is `False`, then this message MAY have previously
been acquired by another link or links.
:vartype first_acquirer: bool
:ivar priority: This field contains the relative message priority. Higher numbers indicate higher
priority messages. Messages with higher priorities MAY be delivered before those with lower priorities.
:vartype priority: int
:param header: Internal only. This is used to wrap an existing message header
that has been received from an AMQP service.
:type header: uamqp.c_uamqp.cHeader
"""
def __init__(self, header=None):
self.delivery_count = 0
self.time_to_live = None
self.first_acquirer = None
self.durable = None
self.priority = None
if header:
self.delivery_count = header.delivery_count or 0
self.time_to_live = header.time_to_live
self.first_acquirer = header.first_acquirer
self.durable = header.durable
self.priority = header.priority
def __str__(self):
return str(
{
"delivery_count": self.delivery_count,
"time_to_live": self.time_to_live,
"first_acquirer": self.first_acquirer,
"durable": self.durable,
"priority": self.priority,
}
)
@property
def ttl(self):
"""
Alias for time_to_live.
:rtype: int
"""
return self.time_to_live
def get_header_obj(self):
"""Get the underlying C reference from this object.
:rtype: uamqp.c_uamqp.cHeader
"""
header = c_uamqp.create_header()
header.delivery_count = self.delivery_count or 0
if self.time_to_live is not None:
header.time_to_live = self.time_to_live
if self.first_acquirer is not None:
header.first_acquirer = self.first_acquirer
if self.durable is not None:
header.durable = self.durable
if self.priority is not None:
header.priority = self.priority
return header
|