-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini_redis_api.py
148 lines (140 loc) · 3.72 KB
/
mini_redis_api.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from flask import request, jsonify, Blueprint
from marshmallow import ValidationError
from mini_redis import MiniRedis
from schemas import SetValueSchema, ExpireSchema
class RedisAPI:
blueprint = Blueprint("mini_redis_api", __name__)
redis = MiniRedis()
@staticmethod
@blueprint.route("/", methods=["POST"])
def set_value():
"""
Set a key-value pair
---
parameters:
- name: body
in: body
schema:
type: object
properties:
key:
type: string
value:
type: string
required:
- key
- value
responses:
200:
description: Value set successfully
"""
try:
data = request.json
SetValueSchema().load(data)
key = data["key"]
value = data["value"]
return RedisAPI.redis.set(key, value)
except ValidationError as err:
return jsonify(err.messages), 400
@staticmethod
@blueprint.route("/<key>", methods=["GET"])
def get_value(key):
"""
Get the value of a key
---
parameters:
- name: key
in: path
type: string
required: true
responses:
200:
description: The value of the key
schema:
type: string
example: "name of person"
404:
description: The key is not set
schema:
type: string
example: "Key not found"
"""
value = RedisAPI.redis.get(key)
if value is None:
return jsonify("Key not found"), 404
return jsonify(value)
@staticmethod
@blueprint.route("/<key>", methods=["DELETE"])
def delete_value(key):
"""
Delete a key-value pair
---
parameters:
- name: key
in: path
type: string
required: true
responses:
200:
description: Number of keys deleted
schema:
type: integer
example: 1
"""
deleted_keys = RedisAPI.redis.delete(key)
return jsonify(deleted_keys)
@staticmethod
@blueprint.route("/expire", methods=["POST"])
def expire_key():
"""
Set an expiration on a key
---
parameters:
- name: body
in: body
schema:
type: object
properties:
key:
type: string
seconds:
type: integer
required:
- key
- seconds
responses:
200:
description: Expire set successfully
schema:
type: integer
example: 1
"""
try:
data = request.json
ExpireSchema().load(data)
key = data["key"]
seconds = int(data["seconds"])
result = RedisAPI.redis.expire(key, seconds)
return jsonify(result)
except ValidationError as err:
return jsonify(err.messages), 400
@staticmethod
@blueprint.route("/ttl/<key>", methods=["GET"])
def ttl_key(key):
"""
Get the time-to-live for a key
---
parameters:
- name: key
in: path
type: string
required: true
responses:
200:
description: Time-to-live in seconds
schema:
type: integer
example: 42
"""
result = RedisAPI.redis.ttl(key)
return jsonify(result)