package demo;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

/** The remote API all three clients call. Runs in the same JVM on a random port. */
@RestController
public class HttpClientsServerController {

    @GetMapping("/users/{id}")
    HttpClientsUser user(@PathVariable("id") int id) {
        if (id == 404) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no user " + id);
        }
        return new HttpClientsUser(id, "user-" + id);
    }

    @GetMapping("/fail")
    String fail() {
        throw new IllegalStateException("boom");
    }

    @GetMapping("/slow")
    HttpClientsUser slow(@RequestParam("ms") long ms) throws InterruptedException {
        Thread.sleep(ms);
        return new HttpClientsUser(0, "slow-" + ms);
    }
}
