1
1
from typing import Dict
2
2
3
+ from .exceptions import ArgumentOutOfRangeException
3
4
from .qpid .proton ._data import PythonAMQPData
4
5
from .qpid .proton ._delivery import Delivery
5
6
from .qpid .proton ._events import Event
7
+ from .utils import validate_annotations
6
8
7
- '''
9
+ """
8
10
DeliveryContext is a class that is used to accept, reject, requeue or requeue with annotations a message.
9
11
It is an helper to set the default values needed for manually accepting and settling messages.
10
- '''
12
+ """
11
13
12
14
13
15
class DeliveryContext :
@@ -39,32 +41,37 @@ def discard(self, event: Event) -> None:
39
41
or dead-letter it if it is configured.
40
42
Application-specific annotation keys must start with the <code>x-opt-</code> prefix.
41
43
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
44
45
modified{delivery-failed = true, undeliverable-here = true}</code> outcome.
45
46
<param name="annotations"> annotations message annotations to combine with existing ones </param>
46
47
<a
47
48
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>
50
50
The annotations can be used only with Quorum queues, see https://www.rabbitmq.com/docs/amqp#modified-outcome
51
51
"""
52
52
53
53
def discard_with_annotations (
54
- self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
54
+ self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
55
55
) -> None :
56
56
dlv = event .delivery
57
57
dlv .local .annotations = annotations
58
58
dlv .local .failed = True
59
59
dlv .local .undeliverable = True
60
60
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
+
61
68
dlv .update (Delivery .MODIFIED )
62
69
dlv .settle ()
63
70
64
71
"""
65
- Requeue the message (AMQP 1.0 <code>released</code> outcome).
72
+ Requeue the message (AMQP 1.0 <code>released</code> outcome).
66
73
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.
68
75
"""
69
76
70
77
def requeue (self , event : Event ) -> None :
@@ -74,31 +81,34 @@ def requeue(self, event: Event) -> None:
74
81
75
82
"""
76
83
Requeue the message with annotations to combine with the existing message annotations.
77
-
78
84
This means the message has not been processed and the broker can requeue it and deliver it
79
85
to the same or a different consumer.
80
86
Application-specific annotation keys must start with the <code>x-opt-</code> prefix.
81
87
Annotation keys the broker understands starts with <code>x-</code>, but not with <code>x-opt-
82
88
</code>.
83
-
84
89
This maps to the AMQP 1.0 <code>
85
90
modified{delivery-failed = false, undeliverable-here = false}</code> outcome.
86
-
87
91
<param name="annotations"> annotations message annotations to combine with existing ones </param>
88
92
<a
89
93
href="https://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-modified">AMQP
90
94
1.0 <code>modified</code> outcome</a>
91
-
92
95
The annotations can be used only with Quorum queues, see https://www.rabbitmq.com/docs/amqp#modified-outcome
93
96
"""
94
97
95
98
def requeue_with_annotations (
96
- self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
99
+ self , event : Event , annotations : Dict [str , "PythonAMQPData" ]
97
100
) -> None :
98
101
dlv = event .delivery
99
102
dlv .local .annotations = annotations
100
103
dlv .local .failed = False
101
104
dlv .local .undeliverable = False
102
105
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
+
103
113
dlv .update (Delivery .MODIFIED )
104
114
dlv .settle ()
0 commit comments