Added validation checks before requests are sent

This commit is contained in:
Rohit Awate 2018-01-28 19:32:03 +05:30
parent 8189de624d
commit d884ec4269

View file

@ -62,6 +62,7 @@ public class DashboardController implements Initializable {
@Override @Override
public void initialize(URL url, ResourceBundle rb) { public void initialize(URL url, ResourceBundle rb) {
applySettings(); applySettings();
addressField.setText("https://api.chucknorris.io/jokes/random");
responseBox.getChildren().remove(0); responseBox.getChildren().remove(0);
httpMethodBox.getItems().addAll(httpMethods); httpMethodBox.getItems().addAll(httpMethods);
httpMethodBox.setValue("GET"); httpMethodBox.setValue("GET");
@ -74,13 +75,22 @@ public class DashboardController implements Initializable {
try { try {
String address = addressField.getText(); String address = addressField.getText();
if (address.equals("")) { if (address.equals("")) {
snackBar.show("Please enter a valid address.", 7000); snackBar.show("Please enter an address.", 3000);
return; return;
} }
switch (httpMethodBox.getValue()) { switch (httpMethodBox.getValue()) {
case "GET": case "GET":
if (requestManager == null) /*
Creates a new instance if its the first request of that session or
the HTTP method type was changed. Also checks if a request is already being processed.
*/
if (requestManager == null || requestManager.getClass() != GETRequestManager.class)
requestManager = new GETRequestManager(); requestManager = new GETRequestManager();
else if (requestManager.isRunning()) {
snackBar.show("Please wait while the current request is processed.", 3000);
return;
}
GETRequest getRequest = new GETRequest(addressField.getText()); GETRequest getRequest = new GETRequest(addressField.getText());
requestManager.setRequest(getRequest); requestManager.setRequest(getRequest);
cancelButton.setOnAction(e -> requestManager.cancel()); cancelButton.setOnAction(e -> requestManager.cancel());
@ -95,7 +105,7 @@ public class DashboardController implements Initializable {
}); });
requestManager.setOnCancelled(e -> { requestManager.setOnCancelled(e -> {
loadingLayer.setVisible(false); loadingLayer.setVisible(false);
snackBar.show("Request canceled.", 5000); snackBar.show("Request canceled.", 2000);
requestManager.reset(); requestManager.reset();
}); });
requestManager.start(); requestManager.start();
@ -104,7 +114,7 @@ public class DashboardController implements Initializable {
loadingLayer.setVisible(false); loadingLayer.setVisible(false);
} }
} catch (MalformedURLException ex) { } catch (MalformedURLException ex) {
snackBar.show("Invalid URL. Please verify and try again.", 7000); snackBar.show("Invalid address. Please verify and try again.", 3000);
} }
} }