generated from ibm-developer-skills-network/coding-project-template
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathroutes.py
122 lines (93 loc) · 3.52 KB
/
routes.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
"""
Controller for routes
"""
from flask import jsonify, url_for, abort
from service import app
from service.common import status
COUNTER = {}
############################################################
# Health Endpoint
############################################################
@app.route("/health")
def health():
"""Health Status"""
return jsonify(dict(status="OK")), status.HTTP_200_OK
############################################################
# Index page
############################################################
@app.route("/")
def index():
"""Returns information abut the service"""
app.logger.info("Request for Base URL")
return jsonify(
status=status.HTTP_200_OK,
message="Hit Counter Service",
version="1.0.0",
url=url_for("list_counters", _external=True),
)
############################################################
# List counters
############################################################
@app.route("/counters", methods=["GET"])
def list_counters():
"""Lists all counters"""
app.logger.info("Request to list all counters...")
counters = [dict(name=count[0], counter=count[1]) for count in COUNTER.items()]
return jsonify(counters)
############################################################
# Create counters
############################################################
@app.route("/counters/<name>", methods=["POST"])
def create_counters(name):
"""Creates a new counter"""
app.logger.info("Request to Create counter: %s...", name)
if name in COUNTER:
return abort(status.HTTP_409_CONFLICT, f"Counter {name} already exists")
COUNTER[name] = 0
location_url = url_for("read_counters", name=name, _external=True)
return (
jsonify(name=name, counter=0),
status.HTTP_201_CREATED,
{"Location": location_url},
)
############################################################
# Read counters
############################################################
@app.route("/counters/<name>", methods=["GET"])
def read_counters(name):
"""Reads a single counter"""
app.logger.info("Request to Read counter: %s...", name)
if name not in COUNTER:
return abort(status.HTTP_404_NOT_FOUND, f"Counter {name} does not exist")
counter = COUNTER[name]
return jsonify(name=name, counter=counter)
############################################################
# Update counters
############################################################
@app.route("/counters/<name>", methods=["PUT"])
def update_counters(name):
"""Updates a counter"""
app.logger.info("Request to Update counter: %s...", name)
if name not in COUNTER:
return abort(status.HTTP_404_NOT_FOUND, f"Counter {name} does not exist")
COUNTER[name] += 1
counter = COUNTER[name]
return jsonify(name=name, counter=counter)
############################################################
# Delete counters
############################################################
@app.route("/counters/<name>", methods=["DELETE"])
def delete_counters(name):
"""Deletes a counter"""
app.logger.info("Request to Delete counter: %s...", name)
if name in COUNTER:
COUNTER.pop(name)
return "", status.HTTP_204_NO_CONTENT
############################################################
# Utility for testing
############################################################
def reset_counters():
"""Removes all counters while testing"""
global COUNTER # pylint: disable=global-statement
if app.testing:
COUNTER = {}