Debian Codegraph @

zaqarclient/queues/v2/message.py

raw
1 # Copyright (c) 2013 Red Hat, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Implements a message controller that understands Zaqar messages."""
16
17 from zaqarclient.queues.v2 import core
18
19
20 class Message:
21 """A handler for Zaqar server Message resources.
22
23 Attributes are only downloaded once - at creation time.
24 """
25 def __init__(self, queue, ttl, age, body, href=None, id=None,
26 claim_id=None, claim_count=0, checksum=None):
27 self.queue = queue
28 self.href = href
29 self.ttl = ttl
30 self.age = age
31 self.body = body
32 self.claim_count = claim_count
33 self.checksum = checksum
34
35 # NOTE(flaper87): Is this really
36 # necessary? Should this be returned
37 # by Zaqar?
38 # The url has two forms depending on if it has been claimed.
39 # /v2/queues/worker-jobs/messages/5c6939a8?claim_id=63c9a592
40 # or
41 # /v2/queues/worker-jobs/messages/5c6939a8
42 if id is None:
43 self.id = href.split('/')[-1]
44 if '?' in self.id:
45 self.id = self.id.split('?')[0]
46 else:
47 self.id = id
48
49 def __repr__(self):
50 return '<Message id:{id} ttl:{ttl}>'.format(id=self.id,
51 ttl=self.ttl)
52
53 @property
54 def claim_id(self):
55 if '=' in self.href:
56 return self.href.split('=')[-1]
57
58 def delete(self):
59 req, trans = self.queue.client._request_and_transport()
60 core.message_delete(trans, req, self.queue._name,
61 self.id, self.claim_id)
62
63
64 def create_object(parent):
65 return lambda args: Message(parent, **args)