blob: 2d02c7a272d3054346c945b36063d912e9f2f2d2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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=" + System.getProperty("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;
}
}
|