blob: 464c1afc6d626868f0f8c4742e2d56a1b5b98fad (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package com.mavlushechka.a1qa.utils;
import com.mavlushechka.a1qa.constants.Endpoint;
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 {
private final static String postsSpec = Endpoint.POSTS.spec;
private final static String usersSpec = Endpoint.USERS.spec;
public static Post getPost(int id) throws IOException {
return JSONParser.convertToObject(URLConnectionManager.get(postsSpec + "/" + id), Post.class);
}
public static int getPostResponseCode(int id, String requestMethod) throws IOException {
return URLConnectionManager.getResponseCode(postsSpec + "/" + id, requestMethod);
}
public static List<Post> getPosts() throws IOException {
return getObjects(postsSpec, Post.class);
}
public static int getPostsResponseCode(String requestMethod) throws IOException {
return URLConnectionManager.getResponseCode(postsSpec, requestMethod);
}
public static Post postPost(Post post) throws IOException {
return JSONParser.convertToObject(URLConnectionManager.post(postsSpec, JSONParser.convertToJSON(post)), Post.class);
}
public static User getUser(int id) throws IOException {
return JSONParser.convertToObject(URLConnectionManager.get(usersSpec + "/" + id), User.class);
}
public static List<User> getUsers() throws IOException {
return getObjects(usersSpec, User.class);
}
public static int getUsersResponseCode(String requestMethod) throws IOException {
return URLConnectionManager.getResponseCode(usersSpec, requestMethod);
}
private static <T> List<T> getObjects(String spec, Class<T> classOfObject) throws IOException {
Object[] objects = JSONParser.convertArray(URLConnectionManager.get(spec), classOfObject);
ArrayList<T> objectsArrayList = new ArrayList<>();
for (Object object : objects) {
objectsArrayList.add((T) object);
}
return objectsArrayList.stream().toList();
}
}
|