68 lines
2.8 KiB
Java
68 lines
2.8 KiB
Java
package com.moyi.controlplane.task;
|
|
|
|
import java.util.Map;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestHeader;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
|
|
|
@RestController
|
|
@RequestMapping("/v1")
|
|
public class TaskController {
|
|
private final AsyncTaskService tasks;
|
|
|
|
public TaskController(AsyncTaskService tasks) {
|
|
this.tasks = tasks;
|
|
}
|
|
|
|
@PostMapping("/{type:3d|image|audio}/tasks")
|
|
public ResponseEntity<Map<String, Object>> create3d(
|
|
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
|
|
@RequestHeader(value = "Authorization", required = false) String authorization,
|
|
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
|
@RequestBody Map<String, Object> input,
|
|
HttpServletRequest request
|
|
) {
|
|
String taskType = type == null ? String.valueOf(input == null ? "3d" : input.getOrDefault("taskType", "3d")) : type;
|
|
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input, idempotencyKey,
|
|
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
|
}
|
|
|
|
|
|
@PostMapping("/video/tasks")
|
|
public ResponseEntity<Map<String, Object>> createVideo(
|
|
@RequestHeader(value = "Authorization", required = false) String authorization,
|
|
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
|
@RequestBody Map<String, Object> input,
|
|
HttpServletRequest request
|
|
) {
|
|
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input, idempotencyKey,
|
|
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
|
}
|
|
|
|
@GetMapping("/tasks/{id}")
|
|
public Map<String, Object> get(
|
|
@RequestHeader(value = "Authorization", required = false) String authorization,
|
|
@PathVariable String id
|
|
) {
|
|
return Map.of("task", tasks.get(authorization, id));
|
|
}
|
|
|
|
@GetMapping("/tasks")
|
|
public Map<String, Object> list(
|
|
@RequestHeader(value = "Authorization", required = false) String authorization,
|
|
@RequestParam(defaultValue = "50") int limit
|
|
) {
|
|
return Map.of("tasks", tasks.list(authorization, limit));
|
|
}
|
|
|
|
}
|