package com.mavlushechka.a1qa.utils; import com.mavlushechka.a1qa.models.Post; import com.mavlushechka.a1qa.models.User; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class JSONPlaceholderAPIManager { public static Post getPost(String spec) throws IOException { return getObject(spec, Post.class); } public static List getPosts(String spec) throws IOException { return getObjects(spec, Post.class); } public static Post postPost(String spec, Post post) throws IOException { return postObject(spec, post, Post.class); } public static User getUser(String spec) throws IOException { return getObject(spec, User.class); } public static List getUsers(String spec) throws IOException { return getObjects(spec, User.class); } public static User postUser(String spec, User user) throws IOException { return postObject(spec, user, User.class); } private static T getObject(String spec, Class classOfObject) throws IOException { return JSONParser.convertToObject(URLConnectionManager.get(spec), classOfObject); } private static List getObjects(String spec, Class classOfObject) throws IOException { Object[] objects = JSONParser.convertArray(URLConnectionManager.get(spec), classOfObject); ArrayList objectsArrayList = new ArrayList<>(); for (Object object : objects) { objectsArrayList.add((T) object); } return objectsArrayList.stream().toList(); } private static T postObject(String spec, T object, Class classOfObject) throws IOException { return JSONParser.convertToObject(URLConnectionManager.post(spec, JSONParser.convertToJSON(object)), classOfObject); } }