@@ -27,12 +27,14 @@ def init(sanic, loop):
27
27
async def auth_handler (request ):
28
28
"""Handles authentication requests"""
29
29
req = request .json
30
- if not req : raise exc .InvalidUsage ("Bad request" )
30
+ if not req :
31
+ raise exc .InvalidUsage ("Bad request" )
31
32
32
33
# Ensure required data is included in the request
33
34
username = req .get ('username' )
34
35
password = req .get ('password' )
35
- if not (username and password ): raise exc .InvalidUsage ("Bad request" )
36
+ if not (username and password ):
37
+ raise exc .InvalidUsage ("Bad request" )
36
38
37
39
# Ensure user exists in database
38
40
user = await db ['users' ].find_one ({ "username" : username })
@@ -57,19 +59,23 @@ async def auth_handler(request):
57
59
async def new_user_handler (request ):
58
60
"""Handles requests for new users"""
59
61
req = request .json
60
- if not req : raise exc .InvalidUsage ("Bad request" )
62
+ if not req :
63
+ raise exc .InvalidUsage ("Bad request" )
61
64
62
65
# Ensure required data is included in the request
63
66
username = req .get ('username' )
64
67
email = req .get ('email' )
65
68
password = req .get ('password' )
66
- if not (username and email and password ): raise exc .InvalidUsage ("Bad request" )
69
+ if not (username and email and password ):
70
+ raise exc .InvalidUsage ("Bad request" )
67
71
68
72
# Ensure user does not already exist in database
69
73
user = await db ['users' ].find_one ({ "username" : username })
70
- if user is not None : return res .json ({ "message" : "A user with this username already exists" , "status" : 409 })
74
+ if user is not None :
75
+ return res .json ({ "message" : "A user with this username already exists" , "status" : 409 })
71
76
user = await db ['users' ].find_one ({ "email" : email })
72
- if user is not None : return res .json ({ "message" : "A user with this email already exists" , "status" : 409 })
77
+ if user is not None :
78
+ return res .json ({ "message" : "A user with this email already exists" , "status" : 409 })
73
79
74
80
# Hash password
75
81
hashed_pass = ph .hash (password )
@@ -93,14 +99,55 @@ async def new_user_handler(request):
93
99
"token" : token
94
100
})
95
101
102
+ @app .route ('/api/user/<user_id:int>' , methods = ['GET' , 'POST' ])
103
+ async def user_handler (req , user_id ):
104
+ """TODO Handles requests for existing users"""
105
+ if not user_id :
106
+ raise exc .InvalidUsage ("Bad request" )
107
+ #if req.method == 'GET':
108
+ raise exc .NotFound ("Soon™" )
109
+
110
+ @app .route ('/api/repo' , methods = ['POST' ])
111
+ async def new_repo_handler (req ):
112
+ """TODO New repo"""
113
+ raise exc .NotFound ("Soon™" )
114
+
115
+ # Existing repo
116
+ @app .route ('/api/repo/<repo_id:int>' , methods = ['GET' , 'POST' , 'DELETE' ])
117
+ async def repo_handler (req , repo_id ):
118
+ """Handles requests for existing repositories"""
119
+ if not repo_id :
120
+ raise exc .InvalidUsage ("Bad request" )
121
+
122
+ # Get repository
123
+ if req .method == 'GET' :
124
+ # TODO auth check
125
+
126
+ repo = await db ['repos' ].find_one ({ "_id" : repo_id })
127
+ if not repo :
128
+ raise exc .NotFound ("Resource not found" )
129
+
130
+ # Temporary confirmation
131
+ return res .json ({ "message" : f"You've requested repository ID { repo_id } " })
132
+
133
+ # Update repository
134
+ elif req .method == 'POST' :
135
+ repo = await db ['repos' ].find_one ({ "_id" : repo_id })
136
+ if not repo :
137
+ raise exc .Forbidden ("Repository doesn't exist" )
138
+ else :
139
+ # TODO Update repo
140
+ pass
141
+
142
+ # Delete repository
143
+ elif req .method == 'DELETE' :
144
+ repo = await db ['repos' ].find_one ({ "_id" : repo_id })
145
+ if not repo :
146
+ raise exc .Forbidden ("Repository doesn't exist" )
147
+ else :
148
+ return res .json ({ "message" : "testing" })
149
+
96
150
@app .exception (exc .SanicException )
97
151
def errors (request , exception ):
98
152
"""Handles errors"""
99
- return res .json ({ "error" : exception .args [0 ], "status" : exception .status_code })
100
-
101
- if __name__ == "__main__" :
102
- app .run (
103
- host = config .get ('app_host' , '0.0.0.0' ),
104
- port = config .get ('app_port' , 80 ),
105
- debug = True
106
- )
153
+ return res .json ({ "error" : exception .args [0 ], "status" : exception .status_code })
0 commit comments