return error as json when rate limit reached or user not found

This commit is contained in:
hrj 2020-07-03 22:15:27 +05:30
parent 80ff9bfe6d
commit 644a106a31

View File

@ -68,16 +68,17 @@ class Server(port: Int){
host.addContext("/v1/captcha",(req, resp) => {
val accessToken = Option(req.getHeaders().get("access-token")).map(_.toInt)
val access = accessToken.map(t => rateLimiter.validateUser(t) && rateLimiter.checkLimit(t)).getOrElse(false)
val id = if(access){
if(access){
val body = req.getJson()
val json = parse(body)
val param = json.extract[Parameters]
captcha.getChallenge(param)
val id = captcha.getChallenge(param)
resp.getHeaders().add("Content-Type","application/json")
resp.send(200, write(id))
} else {
"Not a valid user or rate limit reached!"
resp.getHeaders().add("Content-Type","application/json")
resp.send(400, write("""{"error": "Not a valid user or rate limit reached!"}"""))
}
resp.getHeaders().add("Content-Type","application/json")
resp.send(200, write(id))
0
},"POST")