-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh_web.py
109 lines (84 loc) · 3.18 KB
/
gh_web.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
from flask import Flask, abort, jsonify, make_response
from flask_restful import Api, Resource, fields, marshal, reqparse
from flask_httpauth import HTTPBasicAuth
import gh_user
app = Flask(__name__, static_url_path="")
api = Api(app)
auth = HTTPBasicAuth()
@auth.get_password
def get_password(username):
if username == 'eric':
return 'ghuc_proj'
return None
@auth.error_handler
def unauthorized():
return make_response(jsonify({'message': 'Unauthorized access'}), 403)
users = gh_user.Users()
user_fields = {
'username': fields.String,
'contributions': fields.String,
'uri': fields.Url('user')
}
class UserListAPI(Resource):
decorators = [auth.login_required]
def __init__(self):
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('username', type=str, required=True, help='No user provided', location='json')
super(UserListAPI, self).__init__()
# Get user list
@staticmethod
def get():
ulist = users.get_user_list()
return {'users': [marshal(user, user_fields) for user in ulist]}
# Add user
def post(self):
args = self.reqparse.parse_args()
if not users.add_new_user(args['username']):
abort(400)
return {'user': marshal([user for user in users.get_user_list()
if user['username'] == args['username']], user_fields)}, 201
class UserAPI(Resource):
decorators = [auth.login_required]
def __init__(self):
super(UserAPI, self).__init__()
# Get user contributions for the year
@staticmethod
def get(username):
ulist = users.get_user_list()
if len(ulist) == 0:
abort(404)
user = [user for user in ulist if user['username'] == username]
if len(user) == 0:
abort(404)
return {'user': marshal(user[0], user_fields)}
# Update user's contributions
@staticmethod
def put(username):
user = [user for user in users.get_user_list() if user['username'] == username]
if len(user) == 0:
abort(404)
users.get_contributions(username, None, None, True)
return {'user': marshal(user, user_fields)}
# Delete a user
@staticmethod
def delete(username):
user = [user for user in users.get_user_list() if user['username'] == username]
if len(user) == 0:
abort(404)
return {'result': users.remove_user(username)}
class UserDateAPI(Resource):
decorators = [auth.login_required]
def __init__(self):
super(UserDateAPI, self).__init__()
# Get user's contributions from start to end date
@staticmethod
def get(username, start, end):
user = [user for user in users.get_user_list() if user['username'] == username]
if len(user) == 0:
abort(404)
return {'contributions': users.get_contributions(username, start, end)}
api.add_resource(UserListAPI, '/ghuc/api/v1.0/users', endpoint='users')
api.add_resource(UserAPI, '/ghuc/api/v1.0/users/<username>', endpoint='user')
api.add_resource(UserDateAPI, '/ghuc/api/v1.0/users/<username>/<start>:<end>', endpoint='date')
if __name__ == '__main__':
app.run(debug=False)