File: txn.py

package info (click to toggle)
python-consul 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 484 kB
  • sloc: python: 2,858; makefile: 197
file content (37 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download | duplicates (2)
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
import json

from consul.callback import CB


class Txn:
    """
    The Transactions endpoints manage updates or fetches of multiple keys
    inside a single, atomic transaction.
    """

    def __init__(self, agent) -> None:
        self.agent = agent

    def put(self, payload):
        """
        Create a transaction by submitting a list of operations to apply to
        the KV store inside of a transaction. If any operation fails, the
        transaction is rolled back and none of the changes are applied.

        *payload* is a list of operations where each operation is a `dict`
        with a single key value pair, with the key specifying operation the
        type. An example payload of operation type "KV" is
        dict::

            {
                "KV": {
                  "Verb": "<verb>",
                  "Key": "<key>",
                  "Value": "<Base64-encoded blob of data>",
                  "Flags": 0,
                  "Index": 0,
                  "Session": "<session id>"
                }
            }
        """
        return self.agent.http.put(CB.json(), "/v1/txn", data=json.dumps(payload))