-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
127 lines (106 loc) · 3.36 KB
/
main.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
import endpoints
from endpoints import message_types
from endpoints import messages
from endpoints import remote
from google.appengine.ext import ndb
class ReturnUser(messages.Message):
user = messages.StringField(1)
age = messages.StringField(2, required=False)
class ReturnUsers(messages.Message):
users = messages.MessageField(ReturnUser, 1, repeated=True)
class RequestUser(messages.Message):
user = messages.StringField(1)
class RequestUsers(messages.Message):
user = message_types.VoidMessage
class OkStatusResponse(messages.Message):
status = message_types.VoidMessage
quota_limits = [
endpoints.LimitDefinition(
"put-users-metric",
"Metric for PUT users",
5)
]
@endpoints.api(name='users',
version='v1',
limit_definitions=quota_limits)
class UsersAPI(remote.Service):
@endpoints.method(
#request
RequestUsers,
#response
ReturnUsers,
path='users',
http_method='GET',
name='getUsers')
def get_users(self, request):
entities = User.query_users()
user_list = [ReturnUser(user=entity.username) for entity in entities]
return ReturnUsers(users=user_list)
@endpoints.method(
RequestUser,
ReturnUser,
path='users/{user}',
http_method='GET',
name='getUser',
api_key_required=True)
def get_user(self, request):
queried_user=User.query_user(user=request.user)
return ReturnUser(user=queried_user[0].username, age='20')
@endpoints.method(
# This is actually the request user for this method, reusing message
ReturnUser,
OkStatusResponse,
path='users/{user}',
http_method='PUT',
name='addUser',
metric_costs={"put-users-metric": 1})
def add_user(self, request):
user_to_add = request.user
print(user_to_add)
try:
user_age = request.age
except:
user_age=None
finally:
# Create entity
result = User.create_user(user_to_add, user_age)
if result == "User exists":
raise endpoints.BadRequestException("User {} already exists".format(user_to_add))
return OkStatusResponse()
class User(ndb.Model):
username = ndb.StringProperty()
age = ndb.IntegerProperty()
@classmethod
def query_users(cls):
'''
Query all user under Users entity kind
'''
query = User.query()
users = query.fetch()
return users
@classmethod
def query_user(cls, user):
'''
Query a single user from the input, under Users entity kind
'''
query=User.query(User.username==user)
user = query.fetch()
return user
@classmethod
def create_user(cls, user, age=None):
query = User.query(User.username==user).fetch()
try:
# If it succeeds, it means that the user exists
queried_user = query[0].username
except IndexError:
if age is None:
new_user = User(username=user)
else:
new_user = User(username=user, age=int(age))
new_user.put()
return "ok"
return "User exists"
@classmethod
def delete_user(cls, user):
pass
api = endpoints.api_server([UsersAPI])