Skip to content

Commit 85363a3

Browse files
committed
Changed the endpoints : session by session, session with random id and one time session
1 parent 62676e9 commit 85363a3

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

JShellAPI/src/main/java/org/togetherjava/jshellapi/rest/JShellController.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,24 @@ public class JShellController {
2121
public JShellResult eval(@PathVariable String id, @RequestBody String code) throws DockerException {
2222
validateId(id);
2323
if(code == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Code is null");
24-
return service.sessionById(id).eval(code).orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running"));
24+
return service.session(id).eval(code).orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running"));
2525
}
2626
@PostMapping("/eval")
2727
public JShellResultWithId eval(@RequestBody String code) throws DockerException {
28-
JShellService jShellService = service.oneTimeSession();
28+
JShellService jShellService = service.session();
2929
return new JShellResultWithId(jShellService.id(), jShellService.eval(code).orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running")));
3030
}
31+
@PostMapping("/single-eval")
32+
public JShellResult singleEval(@RequestBody String code) throws DockerException {
33+
JShellService jShellService = service.oneTimeSession();
34+
return jShellService.eval(code).orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running"));
35+
}
3136

3237
@GetMapping("/snippets/{id}")
3338
public List<String> snippets(@PathVariable String id) throws DockerException {
3439
validateId(id);
3540
if(!service.hasSession(id)) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Id " + id + " not found");
36-
return service.sessionById(id).snippets().orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running"));
41+
return service.session(id).snippets().orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT, "An operation is already running"));
3742
}
3843

3944
@DeleteMapping("/{id}")

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ public boolean hasSession(String id) {
4545
return jshellSessions.containsKey(id);
4646
}
4747

48-
public JShellService sessionById(String id) throws DockerException {
48+
public JShellService session(String id) throws DockerException {
4949
if(!hasSession(id)) {
5050
return createSession(id, config.regularSessionTimeoutSeconds(), true, config.evalTimeoutSeconds());
5151
}
5252
return jshellSessions.get(id);
5353
}
54+
public JShellService session() throws DockerException {
55+
return createSession(UUID.randomUUID().toString(), config.regularSessionTimeoutSeconds(), false, config.evalTimeoutSeconds());
56+
}
5457
public JShellService oneTimeSession() throws DockerException {
5558
return createSession(UUID.randomUUID().toString(), config.oneTimeSessionTimeoutSeconds(), false, config.evalTimeoutSeconds());
5659
}

0 commit comments

Comments
 (0)