From a32de03308b2246f1e7cd4c970e6f96116b41ea1 Mon Sep 17 00:00:00 2001 From: Mavlushechka Date: Mon, 17 Oct 2022 22:18:25 +0500 Subject: Divide classes into 2 packages: framework and project --- .../a1qa/project/utils/VkApiUtils.java | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/java/com/mavlushechka/a1qa/project/utils/VkApiUtils.java (limited to 'src/main/java/com/mavlushechka/a1qa/project/utils') diff --git a/src/main/java/com/mavlushechka/a1qa/project/utils/VkApiUtils.java b/src/main/java/com/mavlushechka/a1qa/project/utils/VkApiUtils.java new file mode 100644 index 0000000..a5cd439 --- /dev/null +++ b/src/main/java/com/mavlushechka/a1qa/project/utils/VkApiUtils.java @@ -0,0 +1,68 @@ +package com.mavlushechka.a1qa.project.utils; + +import com.mavlushechka.a1qa.project.constants.VkApiMethod; +import com.mavlushechka.a1qa.framework.utils.JsonParser; +import com.mavlushechka.a1qa.framework.utils.UrlConnectionManager; +import com.mavlushechka.a1qa.project.models.Liker; +import com.mavlushechka.a1qa.project.models.Post; +import com.mavlushechka.a1qa.project.models.User; +import org.json.JSONObject; + +import java.io.IOException; + +public class VkApiUtils { + + + private final static double apiVersion = 5.131; + private final static String url = "https://api.vk.com/method/%s?v=" + apiVersion + "&%s&access_token=" + JsonParser.parseData("testData", "vkontakte.account.token"); + + + public static User getCurrentUser() throws IOException { + return JsonParser.convertToObject( + new JSONObject(UrlConnectionManager.get(url.formatted(VkApiMethod.USERS_GET.name, ""))) + .getJSONArray("response").get(0).toString(), User.class + ); + } + + public static int createPost(String message) throws IOException { + return new JSONObject(UrlConnectionManager.get(url.formatted(VkApiMethod.WALL_POST.name, "message=" + message))) + .getJSONObject("response").getInt("post_id"); + } + + public static void editPost(Post post, Post editedPost) throws IOException { + UrlConnectionManager.get( + url.formatted( + VkApiMethod.WALL_EDIT.name, + "post_id=" + post.id() + "&message=" + editedPost.message() + "&attachments=" + editedPost.attachment() + ) + ); + } + + public static void deletePost(Post post) throws IOException { + UrlConnectionManager.get(url.formatted(VkApiMethod.WALL_DELETE.name, "post_id=" + post.id())); + } + + public static int createComment(int postId, String message) throws IOException { + return new JSONObject(UrlConnectionManager.get( + url.formatted( + VkApiMethod.WALL_CREATE_COMMENT.name, "post_id=" + postId + "&message=" + message + ) + )).getJSONObject("response").getInt("comment_id"); + } + + public static boolean containsLike(Post post, User user) throws IOException { + Object[] users = JsonParser.convertArray(new JSONObject(UrlConnectionManager.get( + url.formatted(VkApiMethod.WALL_GET_LIKES.name, "post_id=" + post.id()) + )).getJSONObject("response").getJSONArray("users").toString(), + Liker.class + ); + + for (Object userObject : users) { + if (((Liker) userObject).getUid() == user.getId()) { + return true; + } + } + return false; + } + +} -- cgit v1.2.3