File: message.py

package info (click to toggle)
python-zaqarclient 4.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 824 kB
  • sloc: python: 4,417; makefile: 18; sh: 2
file content (65 lines) | stat: -rw-r--r-- 2,197 bytes parent folder | download
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
# Copyright (c) 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Implements a message controller that understands Zaqar messages."""

from zaqarclient.queues.v2 import core


class Message(object):
    """A handler for Zaqar server Message resources.

    Attributes are only downloaded once - at creation time.
    """
    def __init__(self, queue, ttl, age, body, href=None, id=None,
                 claim_id=None, claim_count=0, checksum=None):
        self.queue = queue
        self.href = href
        self.ttl = ttl
        self.age = age
        self.body = body
        self.claim_count = claim_count
        self.checksum = checksum

        # NOTE(flaper87): Is this really
        # necessary? Should this be returned
        # by Zaqar?
        # The url has two forms depending on if it has been claimed.
        # /v2/queues/worker-jobs/messages/5c6939a8?claim_id=63c9a592
        # or
        # /v2/queues/worker-jobs/messages/5c6939a8
        if id is None:
            self.id = href.split('/')[-1]
            if '?' in self.id:
                self.id = self.id.split('?')[0]
        else:
            self.id = id

    def __repr__(self):
        return '<Message id:{id} ttl:{ttl}>'.format(id=self.id,
                                                    ttl=self.ttl)

    @property
    def claim_id(self):
        if '=' in self.href:
            return self.href.split('=')[-1]

    def delete(self):
        req, trans = self.queue.client._request_and_transport()
        core.message_delete(trans, req, self.queue._name,
                            self.id, self.claim_id)


def create_object(parent):
    return lambda args: Message(parent, **args)