Skip to content

Commit

Permalink
Accounts work now, spotless, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-richardson authored and AlexandrouR committed Mar 2, 2020
1 parent c6f13ba commit a841eb7
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 78 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ apply {

repositories {
maven { url "https://dl.bintray.com/ethereum/maven/" }
mavenLocal()
}

test {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=4.6.0-SNAPSHOT
version=4.6.1-SNAPSHOT
2 changes: 1 addition & 1 deletion src/main/java/org/web3j/console/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class Runner {

private static final String USAGE =
"Usage: web3j version|wallet|solidity|new|import|generate-tests|audit ...";
"Usage: web3j version|wallet|solidity|new|import|generate-tests|audit|account ...";

private static final String LOGO =
"\n" // generated at http://patorjk.com/software/taag
Expand Down
179 changes: 103 additions & 76 deletions src/main/java/org/web3j/console/account/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,99 +13,126 @@
package org.web3j.console.account;

import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.*;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

import org.web3j.console.config.CliConfig;

import static org.web3j.codegen.Console.exitError;

public class AccountManager {
private static final String USAGE = "account login|logout|create";
private static final String CLOUD_URL = "http://localhost:8000";
private static final String CLOUD_URL = "http://localhost:80";

public static void main(final CliConfig config, final String[] args) {
OkHttpClient client = new OkHttpClient();

Scanner console = new Scanner(System.in);
if (args[0].equals("create")) {
if (config.getLoginToken() != null) {
exitError("You are already logged in. To create a new account, please log out first.");
}

System.out.println("Please enter your email address: ");
String email = console.nextLine().trim();

RequestBody accountBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("email", email)
.build();

Request newAccountRequest =
new okhttp3.Request.Builder()
.url(String.format("%s/api/users/create/", CLOUD_URL))
.post(accountBody)
.build();

try {
Response sendRawResponse = client.newCall(newAccountRequest).execute();
ResponseBody body;
if (sendRawResponse.code() == 200 && (body = sendRawResponse.body()) != null) {
String rawResponse = body.string();
JsonObject responseJsonObj = JsonParser.parseString(rawResponse).getAsJsonObject();
String token = responseJsonObj.get("token").getAsString();
config.setLoginToken(token);
System.out.println("Account created successfully. You can now use Web3j Cloud. Please confirm your e-mail within 24 hours to continue using all features without interruption.");
switch (args[0]) {
case "create":
{
if (config.getLoginToken() != null) {
exitError(
"You are already logged in. To create a new account, please log out first.");
}

System.out.println("Please enter your email address: ");
String email = console.nextLine().trim();

RequestBody accountBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("email", email)
.build();

Request newAccountRequest =
new Request.Builder()
.url(String.format("%s/api/users/create/", CLOUD_URL))
.post(accountBody)
.build();

try {
Response sendRawResponse = client.newCall(newAccountRequest).execute();
ResponseBody body;
if (sendRawResponse.code() == 200
&& (body = sendRawResponse.body()) != null) {
String rawResponse = body.string();
JsonObject responseJsonObj =
JsonParser.parseString(rawResponse).getAsJsonObject();
String token = responseJsonObj.get("token").getAsString();
config.setLoginToken(token);
System.out.println(
"Account created successfully. You can now use Web3j Cloud. Please confirm your e-mail within 24 hours to continue using all features without interruption.");
} else {
System.out.println("Account creation failed. Please try again later.");
}
} catch (IOException e) {
e.printStackTrace();
}

break;
}
} catch (IOException e) {
e.printStackTrace();
}


} else if (args[0].equals("login")) {
System.out.println("Please enter your email address: ");
String email = console.nextLine().trim();
System.out.println("Please enter your password: ");
String password = console.nextLine().trim();


RequestBody loginBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("username", email)
.addFormDataPart("password", password)
.build();

Request loginRequest =
new okhttp3.Request.Builder()
.url(String.format("%s/api/api-token-auth/", CLOUD_URL))
.post(loginBody)
.build();

try {
Response sendRawResponse = client.newCall(loginRequest).execute();
ResponseBody body;
if (sendRawResponse.code() == 200 && (body = sendRawResponse.body()) != null) {
String rawResponse = body.string();
JsonObject responseJsonObj = JsonParser.parseString(rawResponse).getAsJsonObject();
String token = responseJsonObj.get("token").getAsString();
config.setLoginToken(token);
System.out.println("You have been successfully logged in to Web3j Cloud.");
case "login":
{
if (config.getLoginToken() != null) {
exitError(
"You are already logged in. To log in again, please log out first.");
}

System.out.println("Please enter your email address: ");
String email = console.nextLine().trim();
System.out.println("Please enter your password: ");
String password = console.nextLine().trim();

RequestBody loginBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("username", email)
.addFormDataPart("password", password)
.build();

Request loginRequest =
new Request.Builder()
.url(String.format("%s/api/api-token-auth/", CLOUD_URL))
.post(loginBody)
.build();

try {
Response sendRawResponse = client.newCall(loginRequest).execute();
ResponseBody body;
if (sendRawResponse.code() == 200
&& (body = sendRawResponse.body()) != null) {
String rawResponse = body.string();
JsonObject responseJsonObj =
JsonParser.parseString(rawResponse).getAsJsonObject();
String token = responseJsonObj.get("token").getAsString();
config.setLoginToken(token);
System.out.println(
"You have been successfully logged in to Web3j Cloud.");
} else {
System.out.println(
"Error while attempting to log you in. Please check your username and password, and if the problem persists, try again later.");
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
} catch (IOException e) {
e.printStackTrace();
}


} else if (args[0].equals("logout")) {
config.setLoginToken(null);
} else {
exitError(USAGE);
case "logout":
config.setLoginToken(null);
System.out.println("Logged out successfully of Web3j Cloud.");
break;
default:
exitError(USAGE);
break;
}
}
}
1 change: 1 addition & 0 deletions src/main/java/org/web3j/console/update/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public Updater(CliConfig config) {
}

public void promptIfUpdateAvailable() {

if (config.isUpdateAvailable()) {
System.out.println(
String.format(
Expand Down

0 comments on commit a841eb7

Please sign in to comment.