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

com.stytch.java.b2b.api.magiclinksemail.MagicLinksEmail.kt Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package com.stytch.java.b2b.api.magiclinksemail

// !!!
// 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.api.magiclinksemaildiscovery.Discovery
import com.stytch.java.b2b.api.magiclinksemaildiscovery.DiscoveryImpl
import com.stytch.java.b2b.models.magiclinksemail.InviteRequest
import com.stytch.java.b2b.models.magiclinksemail.InviteRequestOptions
import com.stytch.java.b2b.models.magiclinksemail.InviteResponse
import com.stytch.java.b2b.models.magiclinksemail.LoginOrSignupRequest
import com.stytch.java.b2b.models.magiclinksemail.LoginOrSignupResponse
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 Email {
    public val discovery: Discovery

    /**
     * Send either a login or signup magic link to a Member. A new, pending, or invited Member will receive a signup Email
     * Magic Link. Members will have a `pending` status until they successfully authenticate. An active Member will receive a
     * login Email Magic Link.
     *
     * The magic link is valid for 60 minutes.
     */
    public suspend fun loginOrSignup(data: LoginOrSignupRequest): StytchResult

    /**
     * Send either a login or signup magic link to a Member. A new, pending, or invited Member will receive a signup Email
     * Magic Link. Members will have a `pending` status until they successfully authenticate. An active Member will receive a
     * login Email Magic Link.
     *
     * The magic link is valid for 60 minutes.
     */
    public fun loginOrSignup(
        data: LoginOrSignupRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Send either a login or signup magic link to a Member. A new, pending, or invited Member will receive a signup Email
     * Magic Link. Members will have a `pending` status until they successfully authenticate. An active Member will receive a
     * login Email Magic Link.
     *
     * The magic link is valid for 60 minutes.
     */
    public fun loginOrSignupCompletable(data: LoginOrSignupRequest): CompletableFuture>

    /**
     * Send an invite email to a new to join an. The Member will be created with an `invited` status until they successfully
     * authenticate. Sending invites to `pending` Members will update their status to `invited`. Sending invites to already
     * `active` Members will return an error.
     *
     * The magic link invite will be valid for 1 week.
     */
    public suspend fun invite(
        data: InviteRequest,
        methodOptions: InviteRequestOptions? = null,
    ): StytchResult

    /**
     * Send an invite email to a new to join an. The Member will be created with an `invited` status until they successfully
     * authenticate. Sending invites to `pending` Members will update their status to `invited`. Sending invites to already
     * `active` Members will return an error.
     *
     * The magic link invite will be valid for 1 week.
     */
    public fun invite(
        data: InviteRequest,
        methodOptions: InviteRequestOptions? = null,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Send an invite email to a new to join an. The Member will be created with an `invited` status until they successfully
     * authenticate. Sending invites to `pending` Members will update their status to `invited`. Sending invites to already
     * `active` Members will return an error.
     *
     * The magic link invite will be valid for 1 week.
     */
    public fun inviteCompletable(
        data: InviteRequest,
        methodOptions: InviteRequestOptions? = null,
    ): CompletableFuture>
}

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

    override val discovery: Discovery = DiscoveryImpl(httpClient, coroutineScope)

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

            val asJson = moshi.adapter(LoginOrSignupRequest::class.java).toJson(data)
            httpClient.post("/v1/b2b/magic_links/email/login_or_signup", asJson, headers)
        }

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

    override fun loginOrSignupCompletable(data: LoginOrSignupRequest): CompletableFuture> =
        coroutineScope.async {
            loginOrSignup(data)
        }.asCompletableFuture()

    override suspend fun invite(
        data: InviteRequest,
        methodOptions: InviteRequestOptions?,
    ): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()
            methodOptions?.let {
                headers = methodOptions.addHeaders(headers)
            }

            val asJson = moshi.adapter(InviteRequest::class.java).toJson(data)
            httpClient.post("/v1/b2b/magic_links/email/invite", asJson, headers)
        }

    override fun invite(
        data: InviteRequest,
        methodOptions: InviteRequestOptions?,
        callback: (StytchResult) -> Unit,
    ) {
        coroutineScope.launch {
            callback(invite(data, methodOptions))
        }
    }

    override fun inviteCompletable(
        data: InviteRequest,
        methodOptions: InviteRequestOptions?,
    ): CompletableFuture> =
        coroutineScope.async {
            invite(data, methodOptions)
        }.asCompletableFuture()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy