-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (100 loc) · 3.93 KB
/
main.py
File metadata and controls
136 lines (100 loc) · 3.93 KB
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
import db_client
import json
from flask import Flask, request, Blueprint, Response, jsonify, redirect, abort
app = Flask(__name__)
################################################################################
# RESTful Endpoints
def urls(url_id):
if request.method == 'GET':
db_response, url = db_client.get_url(url_id)
if db_response:
response = redirect(url, code=301)
return response
else:
abort(404)
else: #Delete
db_response = db_client.delete_url(url_id)
if db_response:
return Response(status=200)
else:
abort(404)
def register_user_urls(user_id):
try:
json_data = request.get_json(force=True)
url = json_data['url']
if url.find("http://") != 0 and url.find("https://") != 0:
url = 'http://' + url
partial_short_url = 'http://' + request.host
db_response, stats = db_client.create_new_url(url, partial_short_url, user_id)
if db_response:
response = jsonify(stats)
response.status_code = 201
return response
else:
abort(409)
except KeyError as err:
print('Could not parse JSON data')
abort(400)
def user_stats(user_id): #Specific user URL stats
db_response, stats = db_client.get_user_stats(user_id)
if db_response:
response = jsonify(stats)
response.status_code = 200
return response
else:
abort(404)
def stats(url_id=None):
if url_id is None: #Global
response = jsonify(db_client.get_global_stats())
response.status_code = 200
return response
else: #Specific
db_response, stats = db_client.get_url_stats(url_id)
if db_response:
response = jsonify(stats)
response.status_code = 200
return response
else:
abort(404)
def register_users():
try:
json_data = request.get_json(force=True)
user_id = json_data['id']
db_response = db_client.create_new_user(user_id)
if db_response:
response = jsonify({'id' : user_id})
response.status_code = 201
return response
else:
abort(409)
except KeyError as err:
print('Could not parse JSON data')
abort(400)
#DELETE /user/:userId was the specification. Maybe they meant to utilize 'users'
#like the other URLs?
def delete_user(user_id):
db_response = db_client.delete_user(user_id)
if db_response:
response = Response(status=200)
return response
else:
abort(404)
def coffee(): #Identifies the server as a teapot (not coffee machine)
abort(418)
################################################################################
# Routing rules
users_bp = Blueprint('users_blueprint', __name__, url_prefix='/users/<string:user_id>')
stats_bp = Blueprint('stats_blueprint', __name__, url_prefix='/stats')
users_bp.add_url_rule('/urls', view_func=register_user_urls, methods=['POST'], strict_slashes=False)
users_bp.add_url_rule('/stats', view_func=user_stats, methods=['GET'], strict_slashes=False)
stats_bp.add_url_rule('/', view_func=stats, methods=['GET']) #Returns global stats
stats_bp.add_url_rule('/<int:url_id>', view_func=stats, methods=['GET'], strict_slashes=False) #Returns stats from a specific URL
app.add_url_rule('/urls/<int:url_id>', view_func=urls, methods=['GET', 'DELETE'], strict_slashes=False)
app.add_url_rule('/users', view_func=register_users, methods=['POST'], strict_slashes=False)
app.add_url_rule('/user/<string:user_id>', view_func=delete_user, methods=['DELETE'], strict_slashes=False)
app.add_url_rule('/coffee', view_func=coffee, methods=['GET'], strict_slashes=False) #This is just a little joke :)
app.register_blueprint(users_bp)
app.register_blueprint(stats_bp)
if __name__ == '__main__':
app.config['DEBUG'] = True
app.run(host='0.0.0.0', port=5000)