All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.stytch.java.consumer.api.otpemail.OTPEmail.kt Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
package com.stytch.java.consumer.api.otpemail

// !!!
// WARNING: This file is autogenerated
// Only modify code within MANUAL() sections
// or your changes may be overwritten later!
// !!!

import com.squareup.moshi.Moshi
import com.stytch.java.common.InstantAdapter
import com.stytch.java.common.StytchResult
import com.stytch.java.consumer.models.otpemail.LoginOrCreateRequest
import com.stytch.java.consumer.models.otpemail.LoginOrCreateResponse
import com.stytch.java.consumer.models.otpemail.SendRequest
import com.stytch.java.consumer.models.otpemail.SendResponse
import com.stytch.java.http.HttpClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.CompletableFuture

public interface Email {
    /**
     * Send a One-Time Passcode (OTP) to a User using their email. If you'd like to create a user and send them a passcode
     * with one request, use our [log in or create endpoint](https://stytch.com/docs/api/log-in-or-create-user-by-email-otp).
     *
     * ### Add an email to an existing user
     * This endpoint also allows you to add a new email address to an existing Stytch User. Including a `user_id`,
     * `session_token`, or `session_jwt` in your Send one-time passcode by email request will add the new, unverified email
     * address to the existing Stytch User. If the user successfully authenticates within 5 minutes, the new email address
     * will be marked as verified and remain permanently on the existing Stytch User. Otherwise, it will be removed from the
     * User object, and any subsequent login requests using that email address will create a new User.
     *
     * ### Next steps
     * Collect the OTP which was delivered to the user. Call [Authenticate OTP](https://stytch.com/docs/api/authenticate-otp)
     * using the OTP `code` along with the `email_id` found in the response as the `method_id`.
     */
    public suspend fun send(data: SendRequest): StytchResult

    /**
     * Send a One-Time Passcode (OTP) to a User using their email. If you'd like to create a user and send them a passcode
     * with one request, use our [log in or create endpoint](https://stytch.com/docs/api/log-in-or-create-user-by-email-otp).
     *
     * ### Add an email to an existing user
     * This endpoint also allows you to add a new email address to an existing Stytch User. Including a `user_id`,
     * `session_token`, or `session_jwt` in your Send one-time passcode by email request will add the new, unverified email
     * address to the existing Stytch User. If the user successfully authenticates within 5 minutes, the new email address
     * will be marked as verified and remain permanently on the existing Stytch User. Otherwise, it will be removed from the
     * User object, and any subsequent login requests using that email address will create a new User.
     *
     * ### Next steps
     * Collect the OTP which was delivered to the user. Call [Authenticate OTP](https://stytch.com/docs/api/authenticate-otp)
     * using the OTP `code` along with the `email_id` found in the response as the `method_id`.
     */
    public fun send(
        data: SendRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Send a One-Time Passcode (OTP) to a User using their email. If you'd like to create a user and send them a passcode
     * with one request, use our [log in or create endpoint](https://stytch.com/docs/api/log-in-or-create-user-by-email-otp).
     *
     * ### Add an email to an existing user
     * This endpoint also allows you to add a new email address to an existing Stytch User. Including a `user_id`,
     * `session_token`, or `session_jwt` in your Send one-time passcode by email request will add the new, unverified email
     * address to the existing Stytch User. If the user successfully authenticates within 5 minutes, the new email address
     * will be marked as verified and remain permanently on the existing Stytch User. Otherwise, it will be removed from the
     * User object, and any subsequent login requests using that email address will create a new User.
     *
     * ### Next steps
     * Collect the OTP which was delivered to the user. Call [Authenticate OTP](https://stytch.com/docs/api/authenticate-otp)
     * using the OTP `code` along with the `email_id` found in the response as the `method_id`.
     */
    public fun sendCompletable(data: SendRequest): CompletableFuture>

    /**
     * Send a one-time passcode (OTP) to a User using their email. If the email is not associated with a User already, a User
     * will be created.
     *
     * ### Next steps
     *
     * Collect the OTP which was delivered to the User. Call [Authenticate OTP](https://stytch.com/docs/api/authenticate-otp)
     * using the OTP `code` along with the `phone_id` found in the response as the `method_id`.
     */
    public suspend fun loginOrCreate(data: LoginOrCreateRequest): StytchResult

    /**
     * Send a one-time passcode (OTP) to a User using their email. If the email is not associated with a User already, a User
     * will be created.
     *
     * ### Next steps
     *
     * Collect the OTP which was delivered to the User. Call [Authenticate OTP](https://stytch.com/docs/api/authenticate-otp)
     * using the OTP `code` along with the `phone_id` found in the response as the `method_id`.
     */
    public fun loginOrCreate(
        data: LoginOrCreateRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Send a one-time passcode (OTP) to a User using their email. If the email is not associated with a User already, a User
     * will be created.
     *
     * ### Next steps
     *
     * Collect the OTP which was delivered to the User. Call [Authenticate OTP](https://stytch.com/docs/api/authenticate-otp)
     * using the OTP `code` along with the `phone_id` found in the response as the `method_id`.
     */
    public fun loginOrCreateCompletable(data: LoginOrCreateRequest): CompletableFuture>
}

internal class EmailImpl(
    private val httpClient: HttpClient,
    private val coroutineScope: CoroutineScope,
) : Email {
    private val moshi = Moshi.Builder().add(InstantAdapter()).build()

    override suspend fun send(data: SendRequest): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()

            val asJson = moshi.adapter(SendRequest::class.java).toJson(data)
            httpClient.post("/v1/otps/email/send", asJson, headers)
        }

    override fun send(
        data: SendRequest,
        callback: (StytchResult) -> Unit,
    ) {
        coroutineScope.launch {
            callback(send(data))
        }
    }

    override fun sendCompletable(data: SendRequest): CompletableFuture> =
        coroutineScope.async {
            send(data)
        }.asCompletableFuture()

    override suspend fun loginOrCreate(data: LoginOrCreateRequest): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()

            val asJson = moshi.adapter(LoginOrCreateRequest::class.java).toJson(data)
            httpClient.post("/v1/otps/email/login_or_create", asJson, headers)
        }

    override fun loginOrCreate(
        data: LoginOrCreateRequest,
        callback: (StytchResult) -> Unit,
    ) {
        coroutineScope.launch {
            callback(loginOrCreate(data))
        }
    }

    override fun loginOrCreateCompletable(data: LoginOrCreateRequest): CompletableFuture> =
        coroutineScope.async {
            loginOrCreate(data)
        }.asCompletableFuture()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy