summaryrefslogtreecommitdiff
path: root/src/main/java/com/mavlushechka
diff options
context:
space:
mode:
authorMavlushechka <mavlushechka@gmail.com>2022-10-04 00:17:11 +0500
committerMavlushechka <mavlushechka@gmail.com>2022-10-04 00:17:11 +0500
commita3ea0b7e758f4a4b8d28d71bc6f7c356591f142a (patch)
treec83c091e75050242832ddcc18dd39767539aadcc /src/main/java/com/mavlushechka
parent9f192298db05757f0c8f0377c8467d0adc820bb1 (diff)
Create JSONPlaceholderAPI utility class
Diffstat (limited to 'src/main/java/com/mavlushechka')
-rw-r--r--src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java
new file mode 100644
index 0000000..17e2a87
--- /dev/null
+++ b/src/main/java/com/mavlushechka/a1qa/utils/JSONPlaceholderAPI.java
@@ -0,0 +1,54 @@
+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 JSONPlaceholderAPI {
+
+ public static Post getPost(String spec) throws IOException {
+ return getObject(spec, Post.class);
+ }
+
+ public static List<Post> 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<User> 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> T getObject(String spec, Class<T> classOfObject) throws IOException {
+ return JSONParser.convertToObject(URLConnectionManager.get(spec), classOfObject);
+ }
+
+ 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();
+ }
+
+ private static <T> T postObject(String spec, T object, Class<T> classOfObject) throws IOException {
+ return JSONParser.convertToObject(URLConnectionManager.post(spec, JSONParser.convertToJSON(object)), classOfObject);
+ }
+
+}