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

com.stytch.java.consumer.api.project.Project.kt Maven / Gradle / Ivy

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

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

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.stytch.java.common.InstantAdapter
import com.stytch.java.common.StytchResult
import com.stytch.java.consumer.models.project.MetricsRequest
import com.stytch.java.consumer.models.project.MetricsResponse
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 Project {
    public suspend fun metrics(data: MetricsRequest): StytchResult

    public fun metrics(
        data: MetricsRequest,
        callback: (StytchResult) -> Unit,
    )

    public fun metricsCompletable(data: MetricsRequest): CompletableFuture>
}

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

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

            val asJson = moshi.adapter(MetricsRequest::class.java).toJson(data)
            val type = Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java)
            val adapter: JsonAdapter> = moshi.adapter(type)
            val asMap = adapter.fromJson(asJson) ?: emptyMap()
            httpClient.get("/v1/projects/metrics", asMap, headers)
        }

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

    override fun metricsCompletable(data: MetricsRequest): CompletableFuture> =
        coroutineScope.async {
            metrics(data)
        }.asCompletableFuture()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy