summaryrefslogtreecommitdiff
path: root/src/main/java/com/mavlushechka/a1qa/project/utils
diff options
context:
space:
mode:
authorMavlushechka <mavlushechka@gmail.com>2022-10-17 22:18:25 +0500
committerMavlushechka <mavlushechka@gmail.com>2022-10-17 22:18:25 +0500
commita32de03308b2246f1e7cd4c970e6f96116b41ea1 (patch)
treed20f5c41028732cb72b46dda38ec584221f542e3 /src/main/java/com/mavlushechka/a1qa/project/utils
parentb41d2c22bf3e55123a09fae8b1d548dd9548299d (diff)
Divide classes into 2 packages: framework and project
Diffstat (limited to 'src/main/java/com/mavlushechka/a1qa/project/utils')
-rw-r--r--src/main/java/com/mavlushechka/a1qa/project/utils/VkApiUtils.java68
1 files changed, 68 insertions, 0 deletions
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;
+ }
+
+}