|
| 1 | +import java.util.Map; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.*; |
| 4 | +import io.openruntimes.java.*; |
| 5 | +import com.google.gson.Gson; |
| 6 | +import io.appwrite.Client; |
| 7 | +import io.appwrite.exceptions.AppwriteException; |
| 8 | +import io.appwrite.services.Databases; |
| 9 | +import kotlin.Result; |
| 10 | +import kotlin.coroutines.Continuation; |
| 11 | +import kotlin.coroutines.CoroutineContext; |
| 12 | +import kotlin.coroutines.EmptyCoroutineContext; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import okhttp3.Response; |
| 15 | +import com.google.gson.JsonParser; |
| 16 | +import com.google.gson.JsonObject; |
| 17 | + |
| 18 | +final Gson gson = new Gson(); |
| 19 | + |
| 20 | +public RuntimeResponse main(RuntimeRequest req, RuntimeResponse res) throws Exception { |
| 21 | + // Initialize the Appwrite client and databases service |
| 22 | + Client client = new Client(); |
| 23 | + var variables = req.getVariables(); |
| 24 | + String payloadString = req.getPayload().toString(); |
| 25 | + Map<String, Object> responseData = new HashMap<>(); |
| 26 | + |
| 27 | + // Check if the required environment variables are set |
| 28 | + if (variables == null |
| 29 | + || !variables.containsKey("APPWRITE_FUNCTION_ENDPOINT") |
| 30 | + || !variables.containsKey("APPWRITE_FUNCTION_API_KEY") |
| 31 | + || !variables.containsKey("APPWRITE_FUNCTION_PROJECT_ID") |
| 32 | + || variables.get("APPWRITE_FUNCTION_ENDPOINT") == null |
| 33 | + || variables.get("APPWRITE_FUNCTION_API_KEY") == null |
| 34 | + || variables.get("APPWRITE_FUNCTION_PROJECT_ID") == null) { |
| 35 | + return res.json(Map.of("Environment variables are not set. Function cannot use Appwrite SDK.", false)); |
| 36 | + } else { |
| 37 | + // Set the Appwrite client properties |
| 38 | + client |
| 39 | + .setEndpoint(variables.get("APPWRITE_FUNCTION_ENDPOINT")) |
| 40 | + .setProject(variables.get("APPWRITE_FUNCTION_PROJECT_ID")) |
| 41 | + .setKey(variables.get("APPWRITE_FUNCTION_API_KEY")); |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + // Parse the payload data into a Map |
| 46 | + Databases databases = new Databases(client); |
| 47 | + Map<String, String> payload = gson.fromJson(payloadString, Map.class); |
| 48 | + String databaseId = (String) payload.get("databaseId"); |
| 49 | + String collectionId = (String) payload.get("collectionId"); |
| 50 | + |
| 51 | + if (databaseId == null || collectionId == null) { |
| 52 | + return res.json(Map.of("Invalid payload.", false)); |
| 53 | + } |
| 54 | + |
| 55 | + databases.deleteCollection( |
| 56 | + databaseId, |
| 57 | + collectionId, |
| 58 | + new Continuation<Object>() { |
| 59 | + @NotNull |
| 60 | + @Override |
| 61 | + public CoroutineContext getContext() { |
| 62 | + return EmptyCoroutineContext.INSTANCE; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void resumeWith(@NotNull Object o) { |
| 67 | + String json = ""; |
| 68 | + try { |
| 69 | + if (o instanceof Result.Failure) { |
| 70 | + Result.Failure failure = (Result.Failure) o; |
| 71 | + throw failure.exception; |
| 72 | + } else { |
| 73 | + Response response = (Response) o; |
| 74 | + } |
| 75 | + } catch (Throwable th) { |
| 76 | + System.out.print("ERROR: " + th.toString()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + return res.json(Map.of("success", true)); |
| 83 | + } catch (AppwriteException e) { |
| 84 | + return res.json(Map.of("Collection not found.", false)); |
| 85 | + } |
| 86 | +} |
0 commit comments