[O] More cleanup, return 400 for bad requests

This commit is contained in:
Azalea 2024-03-26 23:03:40 -04:00
parent 305d1cea94
commit c99d8e7e75
7 changed files with 24 additions and 53 deletions

View File

@ -1,9 +1,9 @@
package ext
import icu.samnyan.aqua.net.utils.ApiException
import icu.samnyan.aqua.sega.general.BaseHandler
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.compression.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.coroutines.Dispatchers
@ -68,6 +68,9 @@ operator fun Int.minus(message: String): Nothing {
ApiException.log.info("> Error $this: $message")
throw ApiException(this, message)
}
fun <R> parsing(block: () -> R) = try { block() }
catch (e: ApiException) { throw e }
catch (e: Exception) { 400 - e.message.toString() }
// Email validation
// https://www.baeldung.com/java-email-validation-regex

View File

@ -110,7 +110,7 @@ class Maimai2ServletController(
} }
val getGameEvent = BaseHandler {
val type = (it["type"] as Number).toInt()
val type = parsing { (it["type"] as Number).toInt() }
mapOf(
"type" to type,
"gameEventList" to repos.gameEvent.findByEnable(true) // Maimai only request for type 1
@ -118,7 +118,7 @@ class Maimai2ServletController(
}
val getUserRivalData = UserReqHandler { req, userId ->
val rivalId = (req["rivalId"] as Number).toLong()
val rivalId = parsing { (req["rivalId"] as Number).toLong() }
mapOf(
"userId" to userId,
@ -155,7 +155,7 @@ class Maimai2ServletController(
"isLogin" to false,
"isExistSellingCard" to false
)
} }
} ?: (404 - "User not found") }
val getUserPreview = UserReqHandler { _, userId ->
val d = repos.userData.findByCardExtId(userId)() ?: (404 - "User not found")
@ -253,7 +253,7 @@ class Maimai2ServletController(
@API("/{api}")
fun handle(@PathVariable api: String, @RequestBody request: Map<String, Any>): Any {
logger.info("Mai2 < $api : $request")
logger.info("Mai2 < $api : ${request.toJson()}") // TODO: Optimize logging
if (api in noopEndpoint) {
logger.info("Mai2 > $api no-op")
@ -266,7 +266,8 @@ class Maimai2ServletController(
}
return handlers[api]?.handle(request)?.let { if (it is String) it else it.toJson() }?.also {
if (api !in setOf("GetUserItemApi")) logger.info("Mai2 > $api : $it")
if (api !in setOf("GetUserItemApi", "GetGameEventApi"))
logger.info("Mai2 > $api : $it")
} ?: {
logger.warn("Mai2 > $api not found")
"""{"returnCode":1,"apiName":"com.sega.maimai2servlet.api.$api"}"""

View File

@ -1,16 +0,0 @@
package icu.samnyan.aqua.sega.maimai2.handler;
import com.fasterxml.jackson.core.JsonProcessingException;
import icu.samnyan.aqua.sega.general.BaseHandler;
import java.util.Map;
public interface UserReqHandler extends BaseHandler {
@Override
default Object handle(Map<String, Object> request) throws JsonProcessingException {
var userId = ((Number) request.get("userId")).longValue();
return handleThis(request, userId);
}
Object handleThis(Map<String, Object> request, Long userId) throws JsonProcessingException;
}

View File

@ -0,0 +1,13 @@
package icu.samnyan.aqua.sega.maimai2.handler
import ext.parsing
import icu.samnyan.aqua.sega.general.BaseHandler
fun interface UserReqHandler : BaseHandler {
override fun handle(request: Map<String, Any>): Any {
val userId = parsing { (request["userId"] as Number).toLong() }
return handleThis(request, userId)
}
fun handleThis(request: Map<String, Any>, userId: Long): Any
}

View File

@ -1,17 +0,0 @@
package icu.samnyan.aqua.sega.maimai2.model.response.data;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author samnyan (privateamusement@protonmail.com)
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserRecommendRateMusic {
private int musicId;
private int level;
private int averageAchievement;
}

View File

@ -1,13 +0,0 @@
package icu.samnyan.aqua.sega.maimai2.model.response.data;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserRivalData {
private long rivalId;
private String rivalName;
}

View File

@ -35,7 +35,7 @@ public class IntegerListConverter implements AttributeConverter<List<Integer>, S
@Override
public List<Integer> convertToEntityAttribute(String string) {
if (string != null && !string.isEmpty()) {
List<Integer> iList = new ArrayList<Integer>();
List<Integer> iList = new ArrayList<>();
for (String s : string.split(SPLIT_CHAR)) {
iList.add(Integer.parseInt(s));
}