I have an object called Datastore, for example, that requires a few variables to initialize: value1, value2, and a key. The two values are arbitrary, but the key must be validated by means of a call to an API over the network, how should initialization of this object be set up? Is it okay to have network calls inside of the constructor? Should setters be used with a no args constructor? Builder pattern?
Example key validation
private int verifyKey(String key) {
try {
URL url = new URL("https://api.com/verifykey/" + key);
HttpUrlConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connection();
//other implementation
} catch(Exception e) {
e.printStackTrace();
} finally {
//close connection
}
//return result
}
Key
separately and then feed it to theDatastore
constructor.