Untitled

 avatar
supelpawel
java
2 years ago
871 B
2
Indexable
    private static final String POSTS_API_URL = "https://jsonplaceholder.typicode.com/posts";

    public static List<PostDto> getPosts() throws IOException, InterruptedException {

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .GET()
                .header("accept", "application/json")
                .uri(URI.create(POSTS_API_URL))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        int expected = 200;
        int actualStatusCode = response.statusCode();

        AssertJUnit.assertEquals(expected, actualStatusCode);

        ObjectMapper mapper = new ObjectMapper();

        List<PostDto> posts = mapper.readValue(response.body(), new TypeReference<>() {
        });

        return posts;
    }