11from typing import Dict
22
3+ from .exceptions import ArgumentOutOfRangeException
34from .qpid .proton ._data import PythonAMQPData
45from .qpid .proton ._delivery import Delivery
56from .qpid .proton ._events import Event
7+ from .utils import validate_annotations
68
7- '''
9+ """
810DeliveryContext is a class that is used to accept, reject, requeue or requeue with annotations a message.
911It is an helper to set the default values needed for manually accepting and settling messages.
10- '''
12+ """
1113
1214
1315class DeliveryContext :
@@ -39,32 +41,37 @@ def discard(self, event: Event) -> None:
3941 or dead-letter it if it is configured.
4042 Application-specific annotation keys must start with the <code>x-opt-</code> prefix.
4143 Annotation keys the broker understands starts with <code>x-</code>, but not with <code>x-opt-
42-
43- This maps to the AMQP 1.0
44+ This maps to the AMQP 1.0
4445 modified{delivery-failed = true, undeliverable-here = true}</code> outcome.
4546 <param name="annotations"> annotations message annotations to combine with existing ones </param>
4647 <a
4748 href="https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-modified">AMQP
48- 1.0 <code>modified</code> outcome</a>
49-
49+ 1.0 <code>modified</code> outcome</a>
5050 The annotations can be used only with Quorum queues, see https://www.rabbitmq.com/docs/amqp#modified-outcome
5151 """
5252
5353 def discard_with_annotations (
54- self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
54+ self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
5555 ) -> None :
5656 dlv = event .delivery
5757 dlv .local .annotations = annotations
5858 dlv .local .failed = True
5959 dlv .local .undeliverable = True
6060
61+ validated = validate_annotations (annotations .keys ())
62+
63+ if validated is False :
64+ raise ArgumentOutOfRangeException (
65+ "Message annotation key must start with 'x-'"
66+ )
67+
6168 dlv .update (Delivery .MODIFIED )
6269 dlv .settle ()
6370
6471 """
65- Requeue the message (AMQP 1.0 <code>released</code> outcome).
72+ Requeue the message (AMQP 1.0 <code>released</code> outcome).
6673 This means the message has not been processed and the broker can requeue it and deliver it
67- to the same or a different consumer.
74+ to the same or a different consumer.
6875 """
6976
7077 def requeue (self , event : Event ) -> None :
@@ -74,31 +81,34 @@ def requeue(self, event: Event) -> None:
7481
7582 """
7683 Requeue the message with annotations to combine with the existing message annotations.
77-
7884 This means the message has not been processed and the broker can requeue it and deliver it
7985 to the same or a different consumer.
8086 Application-specific annotation keys must start with the <code>x-opt-</code> prefix.
8187 Annotation keys the broker understands starts with <code>x-</code>, but not with <code>x-opt-
8288 </code>.
83-
8489 This maps to the AMQP 1.0 <code>
8590 modified{delivery-failed = false, undeliverable-here = false}</code> outcome.
86-
8791 <param name="annotations"> annotations message annotations to combine with existing ones </param>
8892 <a
8993 href="https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-modified">AMQP
9094 1.0 <code>modified</code> outcome</a>
91-
9295 The annotations can be used only with Quorum queues, see https://www.rabbitmq.com/docs/amqp#modified-outcome
9396 """
9497
9598 def requeue_with_annotations (
96- self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
99+ self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
97100 ) -> None :
98101 dlv = event .delivery
99102 dlv .local .annotations = annotations
100103 dlv .local .failed = False
101104 dlv .local .undeliverable = False
102105
106+ validated = validate_annotations (annotations .keys ())
107+
108+ if validated is False :
109+ raise ArgumentOutOfRangeException (
110+ "Message annotation key must start with 'x-'"
111+ )
112+
103113 dlv .update (Delivery .MODIFIED )
104114 dlv .settle ()
0 commit comments