Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start trial option if not started #2288

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions maestro-cli/src/main/java/maestro/cli/api/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import okio.IOException
import okio.buffer
import java.io.File
import java.nio.file.Path
import java.util.Scanner
import kotlin.io.path.absolutePathString
import kotlin.io.path.exists
import kotlin.time.Duration.Companion.minutes
Expand Down Expand Up @@ -351,6 +352,23 @@ class ApiClient(
if (!response.isSuccessful) {
val errorMessage = response.body?.string().takeIf { it?.isNotEmpty() == true } ?: "Unknown"

if (response.code == 403 && errorMessage.contains("Your trial has not started yet", ignoreCase = true)) {
println("\u001B[31mYour trial has not started yet.\u001B[0m")
print("Would you like to start your trial now? (Y/N): ")
val scanner = Scanner(System.`in`)
val userInput = scanner.nextLine().trim().lowercase()
if (userInput == "y") {
val isTrialStarted = startTrial(authToken, baseUrl)
if (isTrialStarted) {
println("\u001B[32mTrial successfully started. Enjoy your 7-day free trial!\u001B[0m")
return retry("Trial has started, retrying the upload")
proksh marked this conversation as resolved.
Show resolved Hide resolved
} else {
println(response.toString())
println("\u001B[31mFailed to start trial. Please contact support.\u001B[0m")
}
}
}

if (response.code >= 500) {
return retry("Upload failed with status code ${response.code}: $errorMessage")
} else {
Expand All @@ -368,6 +386,25 @@ class ApiClient(
}
}

private fun startTrial(authToken: String, baseUrl: String): Boolean {
println("Starting your trial...")
val body = "".toRequestBody(null)
proksh marked this conversation as resolved.
Show resolved Hide resolved
val trialActivationUrl = "$baseUrl/start-trial"
val trialRequest = Request.Builder()
.header("Authorization", "Bearer $authToken")
.url(trialActivationUrl)
.post(body)
.build()

try {
val response = client.newCall(trialRequest).execute()
return response.isSuccessful;
} catch (e: IOException) {
println("Network error while starting trial: ${e.message}")
proksh marked this conversation as resolved.
Show resolved Hide resolved
return false
}
}

private fun parseRobinUploadResponse(responseBody: Map<*, *>): UploadResponse {
@Suppress("UNCHECKED_CAST")
val orgId = responseBody["orgId"] as String
Expand Down
Loading