diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/mavlushechka/a1qa/constants/VkApiMethod.java (renamed from src/main/java/com/mavlushechka/a1qa/constants/Method.java) | 4 | ||||
-rw-r--r-- | src/main/java/com/mavlushechka/a1qa/utils/VkApiUtils.java | 20 |
2 files changed, 14 insertions, 10 deletions
diff --git a/src/main/java/com/mavlushechka/a1qa/constants/Method.java b/src/main/java/com/mavlushechka/a1qa/constants/VkApiMethod.java index 221f98b..7126abb 100644 --- a/src/main/java/com/mavlushechka/a1qa/constants/Method.java +++ b/src/main/java/com/mavlushechka/a1qa/constants/VkApiMethod.java @@ -1,13 +1,13 @@ package com.mavlushechka.a1qa.constants; -public enum Method { +public enum VkApiMethod { WALL_GET_LIKES("wall.getLikes"), WALL_POST("wall.post"), WALL_EDIT("wall.edit"), WALL_DELETE("wall.delete"), WALL_CREATE_COMMENT("wall.createComment"), USERS_GET("users.get"); public final String name; - Method(String name) { + VkApiMethod(String name) { this.name = name; } diff --git a/src/main/java/com/mavlushechka/a1qa/utils/VkApiUtils.java b/src/main/java/com/mavlushechka/a1qa/utils/VkApiUtils.java index 845ecc7..8d90898 100644 --- a/src/main/java/com/mavlushechka/a1qa/utils/VkApiUtils.java +++ b/src/main/java/com/mavlushechka/a1qa/utils/VkApiUtils.java @@ -1,6 +1,6 @@ package com.mavlushechka.a1qa.utils; -import com.mavlushechka.a1qa.constants.Method; +import com.mavlushechka.a1qa.constants.VkApiMethod; import com.mavlushechka.a1qa.models.Liker; import com.mavlushechka.a1qa.models.Post; import com.mavlushechka.a1qa.models.User; @@ -10,43 +10,47 @@ 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, ""))) + 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(Method.WALL_POST.name, "message=" + message))) + 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( - Method.WALL_EDIT.name, + 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(Method.WALL_DELETE.name, "post_id=" + post.id())); + 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(Method.WALL_CREATE_COMMENT.name, "post_id=" + postId + "&message=" + message))) - .getJSONObject("response").getInt("comment_id"); + 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(Method.WALL_GET_LIKES.name, "post_id=" + post.id()) + url.formatted(VkApiMethod.WALL_GET_LIKES.name, "post_id=" + post.id()) )).getJSONObject("response").getJSONArray("users").toString(), Liker.class ); |