0

I am trying to integrate with LiChess Stream API documented here: https://lichess.org/api#operation/streamGame

As far as I can tell, when you make a GET call to above endpoint, the server keeps the connection open indefinitely and keeps sending JSON responses when the requested game updates. I tried invoking the API via Spring RestTemplate:

    RestTemplate template = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    HttpEntity<String> entity = new HttpEntity<>("UsQjErWM", headers);
    headers.setBearerAuth("<TOKEN>");
    ResponseEntity<String> response = template.exchange("https://lichess.org/api/bot/game/stream/B4ClFYzJ", HttpMethod.GET, entity, String.class);
    System.out.println(response);

But this hangs indefinitely. I am assuming that the RestTemplate is waiting for end of the response which wouldn't arrive. Is there a way to read the stream continuously(perhaps using some low-level Http classes from Java)?

oneCoderToRuleThemAll
  • 834
  • 2
  • 12
  • 33
  • 1
    I guess template.exchange is buffering for an EOF which never comes. You can try this: https://stackoverflow.com/a/39417120/1898563 to get an input stream, then you can buffer it yourself into lines – Michael Sep 18 '21 at 18:32
  • Holding the HTTP connection open is very unusual though. I guess it's been around a while. Nowadays, this would just be a websocket. – Michael Sep 18 '21 at 18:34
  • @Michael Thanks! Following solution from the same question ended up working for me: https://stackoverflow.com/a/64844439/1541964 – oneCoderToRuleThemAll Sep 18 '21 at 19:51

1 Answers1

-2

Not quite sure if you can do that with RestTemplate but for sure you can use WebClient and generate Flux out of it, maybe try it out? @Edit So I just got bashed for not posting it as a comment. FYI You cant use comments till some reputation mark so thanks for distancing me from that ability XD @Edit 2 Spring 5 Web Reactive - How can we use WebClient to retrieve streamed data in a Flux? here's Example how to subscribe to such stream using webclient.

Sir3nka
  • 65
  • 4