Advertisement
Suzana_Marek

LastHomeworkChanges

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