-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv
executable file
·132 lines (95 loc) · 2.38 KB
/
kv
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
import os
import click
import socketio
import requests
from click.decorators import argument
sio = socketio.Client()
URL = os.getenv("URL", "http://localhost:5000")
def get_value(key):
"""
This function is used to get key value from server.
Parameters:
key: The key for which we need to get value.
Returns:
response: Returns the value of key from key-value store
"""
# Can be taken from ENV
url = URL + '/kv'
query_params = {
'key': key
}
try:
response = requests.get(url, json=query_params)
return response.text
except requests.exceptions.RequestException as error:
raise SystemExit(error)
def put_value(key, value):
"""
This function is used to update key with value in key value store.
Parameters:
key: The key for which we need to update value.
value: The value of key.
Returns:
response: Returns the updated key value pair.
"""
# Can be taken from ENV
url = URL + '/kv'
query_params = {
'key': key,
'value': value
}
try:
response = requests.put(url, json=query_params)
return response.text
except requests.exceptions.RequestException as error:
raise SystemExit(error)
# Click is being used to create a CLI.
@click.group()
def commands():
pass
@click.command()
@click.argument(
'key'
)
def get(key):
"""
It can be used to get value of a particular key.
Example -> kv get exampleKey
"""
response = get_value(key)
click.echo(response)
@click.command()
@click.argument('key')
@click.argument('value')
def put(key, value):
"""
It can be used to set key with value.
Example -> kv get exampleKey exampleValue
"""
response = put_value(key, value)
click.echo(response)
@click.command()
def watch():
"""
This functions connects with server and wait for socket events.
"""
sio.connect(URL)
sio.wait()
# Adding sub commands in kv command.
commands.add_command(get)
commands.add_command(put)
commands.add_command(watch)
# Sockets
@sio.event
def connect():
print('connected to server')
@sio.event
def disconnect():
print('disconnected from server')
# Listens for key_update event from server.
@sio.on('key_update')
def my_broadcast_event(data):
print(data)
if __name__ == '__main__':
commands()