-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerClientCode.java
145 lines (119 loc) · 5.78 KB
/
ServerClientCode.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class ServerClientCode {
private static final HttpClient CLIENT = HttpClient.newHttpClient();
private static final Gson GSON = new Gson();
static List<Integer> howmanyidies = new ArrayList<>();
public static User postRequest(URI uri, User user) throws Exception {
final String jsonRequest = GSON.toJson(user);
System.out.println(jsonRequest);
HttpRequest postRequest = HttpRequest.newBuilder()
.uri(uri)
.POST(HttpRequest.BodyPublishers.ofString(jsonRequest))
.build();
HttpResponse<String> postResponse = CLIENT.send(postRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(postResponse.body());
return GSON.fromJson(postResponse.body(), User.class);
}
public static User getRequest(URI uri) throws Exception {
HttpRequest getRequest = HttpRequest.newBuilder()
.uri(uri)
.build();
HttpResponse<String> getResponse = CLIENT.send(getRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(getResponse.body());
return GSON.fromJson(getResponse.body(), User.class);
}
public static User deleteRequest(URI uri) throws Exception {
HttpRequest deleteRequest = HttpRequest.newBuilder()
.uri(uri)
.DELETE()
.build();
HttpResponse<String> deleteResponse = CLIENT.send(deleteRequest, HttpResponse.BodyHandlers.ofString());
return GSON.fromJson(deleteResponse.body(), User.class);
}
public static List<User> showAllUsers(URI uri) throws Exception {
HttpRequest getAllRequest = HttpRequest.newBuilder()
.uri(uri)
.build();
HttpResponse<String> getAllResponse = CLIENT.send(getAllRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(getAllResponse.body());
return GSON.fromJson(getAllResponse.body(), new TypeToken<List<User>>() {
}.getType());
}
public static User getUserById(String url, int id) throws Exception {
HttpRequest getAllRequest = HttpRequest.newBuilder()
.uri(URI.create(url + id))
.build();
HttpResponse<String> getAllResponse = CLIENT.send(getAllRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(getAllResponse.body());
return GSON.fromJson(getAllResponse.body(), User.class);
}
// to get list of usernames
// public static void getUserNames() throws Exception {
// System.out.println("Usernames:");
// for (int i = 1; i <= 10; i++) {
// HttpRequest getAllRequest = HttpRequest.newBuilder()
// .uri(new URI("https://jsonplaceholder.typicode.com/users/" + i))
// .build();
//
// HttpResponse<String> getAllResponse = CLIENT.send(getAllRequest, HttpResponse.BodyHandlers.ofString());
// final User user = GSON.fromJson(getAllResponse.body(), User.class);
// System.out.println(user.getUsername());
// }
// }
public static List<User> getUserByUserName(String url, String username) throws Exception {
HttpRequest getAllRequest = HttpRequest.newBuilder()
.uri(URI.create(url + username))
.build();
HttpResponse<String> getAllResponse = CLIENT.send(getAllRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(getAllResponse.body());
return GSON.fromJson(getAllResponse.body(), new TypeToken<List<User>>() {
}.getType());
}
public static List<Task> openUserTasks(String url, int userId) throws Exception {
HttpRequest getAllRequest = HttpRequest.newBuilder()
.uri(URI.create(url + userId + "/todos"))
.build();
HttpResponse<String> getAllResponse = CLIENT.send(getAllRequest, HttpResponse.BodyHandlers.ofString());
List<Task> tasks = GSON.fromJson(getAllResponse.body(), new TypeToken<List<Task>>() {
}.getType());
return tasks.stream()
.filter(task -> !task.isCompleted())
.collect(Collectors.toList());
}
//method getting last post ID for next method (userComments)
public static void lastPostId(URI uri) throws Exception {
HttpRequest getAllRequest = HttpRequest.newBuilder()
.uri(uri)
.build();
HttpResponse<String> getAllResponse = CLIENT.send(getAllRequest, HttpResponse.BodyHandlers.ofString());
List<Posts> posts = GSON.fromJson(getAllResponse.body(), new TypeToken<List<Posts>>() {
}.getType());
howmanyidies = posts.stream()
.sorted(Comparator.comparing(Posts::getId).reversed())
.map(Posts::getId)
.limit(1)
.collect(Collectors.toList());
}
public static void userComments(String url) throws Exception {
ServerClientCode.lastPostId(URI.create("https://jsonplaceholder.typicode.com/users/1/posts"));
int id = howmanyidies.get(0);
int userId = 1;
HttpRequest getAllRequest = HttpRequest.newBuilder()
.uri(URI.create(url + id + "/comments"))
.build();
String filename = "user-"+userId+"-post-"+id+"-comments.json";
HttpResponse<Path> getAllResponse = CLIENT.send(getAllRequest, HttpResponse.BodyHandlers.ofFile(Paths.get(filename)));
System.out.println(getAllResponse.body());
}
}