opatry / todoist-kt

Todoist REST API bindings for Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

Todoist REST API Kotlin bindings

Kotlin binding for Todoist REST API using Ktor Http Client and Gson as Json marshaller.

Example

val httpClient = HttpClient(CIO) {
    install(ContentNegotiation) {
        gson()
    }
    install(Auth) {
        bearer {
            sendWithoutRequest { true }
            loadTokens {
                // see https://developer.todoist.com/rest/v2/#authorization
                // see https://app.todoist.com/app/settings/integrations/developer
                val apiToken = System.getenv("TODOIST_API_TOKEN")
                BearerTokens(apiToken, "")
            }
        }
    }
    defaultRequest {
        if (url.host.isEmpty()) {
            val defaultUrl = URLBuilder().takeFrom("https://api.todoist.com/rest")
            url.host = defaultUrl.host
            url.protocol = defaultUrl.protocol
            if (!url.encodedPath.startsWith('/')) {
                val basePath = defaultUrl.encodedPath
                url.encodedPath = "$basePath/${url.encodedPath}"
            }
        }
    }
}

val todoService = HttpTodoistService(httpClient)
runBlocking {
    withContext(Dispatchers.IO) {
        todoService.getTasks().forEach(::println)
    }
}

About

Todoist REST API bindings for Kotlin

License:MIT License


Languages

Language:Kotlin 100.0%