Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.gson.Gson;
- import com.google.gson.JsonObject;
- import java.io.IOException;
- import java.net.URI;
- import java.net.http.HttpClient;
- import java.net.http.HttpRequest;
- import java.net.http.HttpResponse;
- import java.util.regex.Pattern;
- public class createNewUser {
- private static final String CHECK_EMAIL_URL = "https://dev.dripit.io/api/is-email-used";
- private static final String CREATE_USER_URL = "https://dev.dripit.io/api/users/create-new-user";
- private static final Gson gson = new Gson();
- private static final HttpClient httpClient = HttpClient.newHttpClient();
- public static class UserCreationResult {
- private final boolean success;
- private final String message;
- public UserCreationResult(boolean success, String message) {
- this.success = success;
- this.message = message;
- }
- public boolean isSuccess() {
- return success;
- }
- public String getMessage() {
- return message;
- }
- }
- public static UserCreationResult createUser(
- String email,
- String username,
- String wallet,
- String authIdentifier
- ) {
- if (!isValidEmail(email)) {
- return new UserCreationResult(false, "Invalid email format");
- }
- try {
- boolean emailUsed = checkEmailUsed(email);
- if (emailUsed) {
- return new UserCreationResult(false, "Email is already in use");
- }
- JsonObject payload = new JsonObject();
- payload.addProperty("email", email);
- payload.addProperty("login_type", 2);
- payload.addProperty("username", username);
- payload.addProperty("wallet", wallet);
- payload.addProperty("auth_identifier", authIdentifier);
- HttpResponse<String> response = sendCreateUserRequest(payload);
- if (response.statusCode() == 201) {
- return new UserCreationResult(true, "User created successfully");
- } else {
- return new UserCreationResult(false,
- "User creation failed. Status: " + response.statusCode() +
- ", Response: " + response.body());
- }
- } catch (Exception e) {
- return new UserCreationResult(false, "Error: " + e.getMessage());
- }
- }
- private static boolean checkEmailUsed(String email) throws IOException, InterruptedException {
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create(CHECK_EMAIL_URL + "?email=" + email))
- .GET()
- .build();
- HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
- return Boolean.parseBoolean(response.body());
- }
- private static HttpResponse<String> sendCreateUserRequest(JsonObject payload) throws IOException, InterruptedException {
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create(CREATE_USER_URL))
- .header("Content-Type", "application/json")
- .POST(HttpRequest.BodyPublishers.ofString(gson.toJson(payload)))
- .build();
- return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
- }
- private static boolean isValidEmail(String email) {
- if (email == null) return false;
- String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
- return Pattern.compile(emailRegex).matcher(email).matches();
- }
- public static void main(String[] args) {
- UserCreationResult result = createUser(
- "test.xy",
- "0x0c18Dc2e0b9420b5CBd9DE661c67219B08E8b1c1",
- "123!"
- );
- System.out.println("Success: " + result.isSuccess());
- System.out.println("Message: " + result.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement