package com.mavlushechka.a1qa.utils; import com.mavlushechka.a1qa.constants.Method; import com.mavlushechka.a1qa.models.Liker; import com.mavlushechka.a1qa.models.Post; import com.mavlushechka.a1qa.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(Method.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(Method.WALL_POST.name, "message=" + message))) .getJSONObject("response").getInt("post_id"); } public static void editPost(Post post, Post editedPost) throws IOException { UrlConnectionManager.get( url.formatted( Method.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(Method.WALL_DELETE.name, "post_id=" + post.id())); } public static int createComment(int postId, String message) throws IOException { return new JSONObject(UrlConnectionManager.get(url.formatted(Method.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(Method.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; } }