Debian Codegraph @

etcd3gw/lease.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from etcd3gw.utils import _decode
14
15
16 class Lease:
17 def __init__(self, id, client=None):
18 """Lease object for expiring keys
19
20 :param id:
21 :param client:
22 """
23 self.id = id
24 self.client = client
25
26 def revoke(self):
27 """LeaseRevoke revokes a lease.
28
29 All keys attached to the lease will expire and be deleted.
30 This method makes a synchronous HTTP request by default. To make an
31 asynchronous HTTP request, please define a `callback` function
32 to be invoked when receiving the response.
33
34 :return:
35 """
36 self.client.post(self.client.get_url("/kv/lease/revoke"),
37 json={"ID": self.id})
38 return True
39
40 def ttl(self):
41 """LeaseTimeToLive retrieves lease information.
42
43 This method makes a synchronous HTTP request by default. To make an
44 asynchronous HTTP request, please define a `callback` function
45 to be invoked when receiving the response.
46
47 :return:
48 """
49 result = self.client.post(self.client.get_url("/kv/lease/timetolive"),
50 json={"ID": self.id})
51 return int(result['TTL'])
52
53 def refresh(self):
54 """LeaseKeepAlive keeps the lease alive
55
56 By streaming keep alive requests from the client to the server and
57 streaming keep alive responses from the server to the client.
58 This method makes a synchronous HTTP request by default.
59
60 :return: returns new TTL for lease. If lease was already expired then
61 TTL field is absent in response and the function returns -1
62 according to etcd documentation.
63 https://etcd.io/docs/v3.5/dev-guide/apispec/swagger/rpc.swagger.json
64 """
65 result = self.client.post(self.client.get_url("/lease/keepalive"),
66 json={"ID": self.id})
67 return int(result['result'].get('TTL', -1))
68
69 def keys(self):
70 """Get the keys associated with this lease.
71
72 :return:
73 """
74 result = self.client.post(self.client.get_url("/kv/lease/timetolive"),
75 json={"ID": self.id,
76 "keys": True})
77 keys = result['keys'] if 'keys' in result else []
78 return [_decode(key) for key in keys]