Use Tuple as response

This commit is contained in:
Marc Beyer 2022-01-13 23:14:36 +01:00
parent 097cce14e8
commit b12de1cd2e
3 changed files with 64 additions and 22 deletions

View File

@ -0,0 +1,27 @@
package helper;
public class Tuple<X, Y> {
public final X key;
public final Y value;
public Tuple(X key, Y value) {
this.key = key;
this.value = value;
}
public X getKey() {
return key;
}
public Y getValue() {
return value;
}
@Override
public String toString() {
return "Tuple{" +
"key=" + key +
", value=" + value +
'}';
}
}

View File

@ -2,6 +2,7 @@ package res;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import helper.Tuple;
import java.io.BufferedReader;
import java.io.DataOutputStream;
@ -15,7 +16,6 @@ import java.util.*;
public class DataController {
public static long USER_ID = -1;
public static String TOKEN = "";
private static final String ALL_EVENTS_ENDPOINT = "http://localhost:8080/event/all";
private static final String ADD_EVENT_ENDPOINT = "http://localhost:8080/event/add";
@ -23,6 +23,7 @@ public class DataController {
private static final String LOGIN_ENDPOINT = "http://localhost:8080/user/login";
private static final String ALL_USERS_ENDPOINT = "http://localhost:8080/user/all";
private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test";
private final HttpRequest httpRequest;
@ -32,19 +33,28 @@ public class DataController {
public boolean login(String username, String password) {
try {
String response = httpRequest.sendPostRequest(
Tuple<Integer, String> response = httpRequest.sendPostRequest(
LOGIN_ENDPOINT,
"login=" + username
+ "&password=" + password,
false
);
String[] data = response.getValue().split("\\s+");
USER_ID = Long.parseLong(response.split("\\s+")[1]);
TOKEN = response.split("\\s+")[0];
USER_ID = Long.parseLong(data[1]);
HttpRequest.TOKEN = data[1];
Tuple<Integer, String> auth = httpRequest.sendPostRequest(
HEADER_TEST_ENDPOINT,
"",
true
);
System.out.println("auth " + auth);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return USER_ID >= 0;
}
@ -68,11 +78,11 @@ public class DataController {
ArrayList<Event> eventList = new ArrayList<>();
try {
String jsonResponse = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true);
Tuple<Integer, String> response = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true);
String jsonResponse = response.getValue();
System.out.println(jsonResponse);
ObjectMapper objectMapper = new ObjectMapper();
//String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
for (Object obj : objectMapper.readValue(jsonResponse, Object[].class)) {
ArrayList<Object> list = new ArrayList<>();
@ -112,7 +122,6 @@ public class DataController {
// Parse JSON
ObjectMapper objectMapper = new ObjectMapper();
//String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
return objectMapper.readValue(jsonString, Event[].class);
}

View File

@ -1,5 +1,7 @@
package res;
import helper.Tuple;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
@ -9,7 +11,9 @@ import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class HttpRequest {
public String sendPostRequest(String urlString, String urlParameters, boolean sendAuth) throws Exception {
public static String TOKEN = "";
public Tuple<Integer, String> sendPostRequest(String urlString, String urlParameters, boolean sendAuth) throws Exception {
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
@ -39,7 +43,7 @@ public class HttpRequest {
if(sendAuth){
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Authorization", "Bearer {token}");
con.setRequestProperty("Authorization", "Bearer " + TOKEN);
}
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
@ -47,22 +51,24 @@ public class HttpRequest {
}
int status = con.getResponseCode();
if (status == 200) {
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
String inputLine;
StringBuilder content = new StringBuilder();
BufferedReader in;
con.disconnect();
return content.toString();
if (status == 200) {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else {
con.disconnect();
throw new Exception("Status: " + status);
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
return new Tuple<>(status, content.toString());
}
public String sendGetRequest(String urlString) throws Exception {