• # Java 11

    Posté par . En réponse au journal recherche-totoz en JavaScript. Évalué à 5.

     public static void searchTotoz(String query) {
     HttpClient client = HttpClient.newHttpClient();
     HttpRequest request = HttpRequest.newBuilder()
     .uri(URI.create("https://totoz.eu/search.xml?terms=" + query))
     .build();
     client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
     .thenApply(HttpResponse::body)
     .thenApply(SmokeTestsApplication::extract)
     .thenAccept(System.out::println)
     .join();
     }
     public static Collection<String> extractNames(String body) {
     try {
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
     Document document = builder.parse(new ByteArrayInputStream(body.getBytes()));
     XPath xpath = XPathFactory.newInstance().newXPath();
     String expression = "/totozes/totoz/name/text()";
     XPathNodes nodes = xpath.evaluateExpression(expression, document, XPathNodes.class);
     return StreamSupport.stream(nodes.spliterator(), false).map(Node::getNodeValue).collect(Collectors.toList());
     } catch (Exception e) {
     throw new RuntimeException(e);
     }
     }

    Complètement à l'arrache, mais c'était l'occasion d'utiliser le nouveau client http de Java 11. C'est assez verbeux (oui c'est java) et la gestion d'erreur est pourrie.