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

main.com.stytch.java.b2b.api.otpemaildiscovery.OTPEmailDiscovery.kt Maven / Gradle / Ivy

package com.stytch.java.b2b.api.otpemaildiscovery

// !!!
// 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.b2b.models.otpemaildiscovery.AuthenticateRequest
import com.stytch.java.b2b.models.otpemaildiscovery.AuthenticateResponse
import com.stytch.java.b2b.models.otpemaildiscovery.SendRequest
import com.stytch.java.b2b.models.otpemaildiscovery.SendResponse
import com.stytch.java.common.InstantAdapter
import com.stytch.java.common.StytchResult
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 Discovery {
    /**
     * Send a discovery OTP to an email address. The OTP is valid for 10 minutes. Only the most recently sent OTP is valid:
     * when an OTP is sent, all OTPs previously sent to the same email address are invalidated, even if unused or unexpired.
     */
    public suspend fun send(data: SendRequest): StytchResult

    /**
     * Send a discovery OTP to an email address. The OTP is valid for 10 minutes. Only the most recently sent OTP is valid:
     * when an OTP is sent, all OTPs previously sent to the same email address are invalidated, even if unused or unexpired.
     */
    public fun send(
        data: SendRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Send a discovery OTP to an email address. The OTP is valid for 10 minutes. Only the most recently sent OTP is valid:
     * when an OTP is sent, all OTPs previously sent to the same email address are invalidated, even if unused or unexpired.
     */
    public fun sendCompletable(data: SendRequest): CompletableFuture>

    /**
     * Authenticates the OTP and returns an intermediate session token. Intermediate session tokens can be used for various
     * Discovery login flows and are valid for 10 minutes.
     */
    public suspend fun authenticate(data: AuthenticateRequest): StytchResult

    /**
     * Authenticates the OTP and returns an intermediate session token. Intermediate session tokens can be used for various
     * Discovery login flows and are valid for 10 minutes.
     */
    public fun authenticate(
        data: AuthenticateRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Authenticates the OTP and returns an intermediate session token. Intermediate session tokens can be used for various
     * Discovery login flows and are valid for 10 minutes.
     */
    public fun authenticateCompletable(data: AuthenticateRequest): CompletableFuture>
}

internal class DiscoveryImpl(
    private val httpClient: HttpClient,
    private val coroutineScope: CoroutineScope,
) : Discovery {
    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/b2b/otps/email/discovery/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 authenticate(data: AuthenticateRequest): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()

            val asJson = moshi.adapter(AuthenticateRequest::class.java).toJson(data)
            httpClient.post("/v1/b2b/otps/email/discovery/authenticate", asJson, headers)
        }

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

    override fun authenticateCompletable(data: AuthenticateRequest): CompletableFuture> =
        coroutineScope.async {
            authenticate(data)
        }.asCompletableFuture()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy