Advertisement
Suzana_Marek

userCreation

Dec 8th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import com.google.gson.Gson;
  2. import com.google.gson.JsonObject;
  3. import java.io.IOException;
  4. import java.net.URI;
  5. import java.net.http.HttpClient;
  6. import java.net.http.HttpRequest;
  7. import java.net.http.HttpResponse;
  8. import java.util.regex.Pattern;
  9.  
  10.     public class createNewUser {
  11.         private static final String CHECK_EMAIL_URL = "https://dev.dripit.io/api/is-email-used";
  12.         private static final String CREATE_USER_URL = "https://dev.dripit.io/api/users/create-new-user";
  13.         private static final Gson gson = new Gson();
  14.         private static final HttpClient httpClient = HttpClient.newHttpClient();
  15.         public static class UserCreationResult {
  16.             private final boolean success;
  17.             private final String message;
  18.             public UserCreationResult(boolean success, String message) {
  19.                 this.success = success;
  20.                 this.message = message;
  21.             }
  22.             public boolean isSuccess() {
  23.                 return success;
  24.             }
  25.             public String getMessage() {
  26.                 return message;
  27.             }
  28.         }
  29.         public static UserCreationResult createUser(
  30.                 String email,
  31.                 String username,
  32.                 String wallet,
  33.                 String authIdentifier
  34.         ) {
  35.             if (!isValidEmail(email)) {
  36.                 return new UserCreationResult(false, "Invalid email format");
  37.             }
  38.  
  39.             try {
  40.                 boolean emailUsed = checkEmailUsed(email);
  41.                 if (emailUsed) {
  42.                     return new UserCreationResult(false, "Email is already in use");
  43.                 }
  44.                 JsonObject payload = new JsonObject();
  45.                 payload.addProperty("email", email);
  46.                 payload.addProperty("login_type", 2);
  47.                 payload.addProperty("username", username);
  48.                 payload.addProperty("wallet", wallet);
  49.                 payload.addProperty("auth_identifier", authIdentifier);
  50.  
  51.                 HttpResponse<String> response = sendCreateUserRequest(payload);
  52.  
  53.                 if (response.statusCode() == 201) {
  54.                     return new UserCreationResult(true, "User created successfully");
  55.                 } else {
  56.                     return new UserCreationResult(false,
  57.                             "User creation failed. Status: " + response.statusCode() +
  58.                                     ", Response: " + response.body());
  59.                 }
  60.  
  61.             } catch (Exception e) {
  62.                 return new UserCreationResult(false, "Error: " + e.getMessage());
  63.             }
  64.         }
  65.         private static boolean checkEmailUsed(String email) throws IOException, InterruptedException {
  66.             HttpRequest request = HttpRequest.newBuilder()
  67.                     .uri(URI.create(CHECK_EMAIL_URL + "?email=" + email))
  68.                     .GET()
  69.                     .build();
  70.  
  71.             HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
  72.             return Boolean.parseBoolean(response.body());
  73.         }
  74.         private static HttpResponse<String> sendCreateUserRequest(JsonObject payload) throws IOException, InterruptedException {
  75.             HttpRequest request = HttpRequest.newBuilder()
  76.                     .uri(URI.create(CREATE_USER_URL))
  77.                     .header("Content-Type", "application/json")
  78.                     .POST(HttpRequest.BodyPublishers.ofString(gson.toJson(payload)))
  79.                     .build();
  80.  
  81.             return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
  82.         }
  83.         private static boolean isValidEmail(String email) {
  84.             if (email == null) return false;
  85.             String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
  86.             return Pattern.compile(emailRegex).matcher(email).matches();
  87.         }
  88.         public static void main(String[] args) {
  89.             UserCreationResult result = createUser(
  90.                     "[email protected]",
  91.                     "test.xy",
  92.                     "0x0c18Dc2e0b9420b5CBd9DE661c67219B08E8b1c1",
  93.                     "123!"
  94.             );
  95.             System.out.println("Success: " + result.isSuccess());
  96.             System.out.println("Message: " + result.getMessage());
  97.         }
  98.     }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement