Skip to content

pedro-cze/kscript-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kscript-utils

About

This lib provides basic functions for building kotlin scripts using kscript tool. These are:

  • http.kt - functions for handling http requests (request, header, get, post, put, patch...)
  • db.kt - helper functions for db connection establishment and generic select query
  • auth.kt - core authentication request and api key parsing
  • log.kt - basic log functions (info, error, success)

Usage

Gradle

This library is not released yet.
Use current SNAPSHOT version.

Maven

This library is not released yet.
Use current SNAPSHOT version.

Example: authentication + get request

#!/usr/bin/env kscript
@file:CompilerOpts("-jvm-target 1.8")
@file:DependsOn("cz.my2n.libs:kscript-lib:$libVersion")
try {
    val credentials = Credentials("[email protected]", "secretPassword")
    val baseUrl = "https://my2n-testprod.dev.2n.cz/middleware/api/partner/v1"
    val apiKey: ApiKey  = authenticate(baseUrl, credentials)
    
    val accessToken = header("x-api-key")(apiKey.accessToken)
    val devices = parseResponse(call(get(baseUrl, "/devices", accessToken)))
} catch(e: Exception) {
    error(e.message)
}
info("Done.")

Example: db connection + select

#!/usr/bin/env kscript
@file:CompilerOpts("-jvm-target 1.8")
@file:DependsOn("cz.my2n.libs:kscript-lib:$libVersion")
try {
    val connection: Connection? = createConnection(mvsConnString)
    connection?.let {
        val queryParams: Map<String, String> = mapOf("sipNumber", "123456")
        val rowToDevice: (Row) -> Device = { Device(it.id, it.sipNumber) }
        val devices: Set<Device> = select("select * from device where sip_number = :sipNumber", queryParams, connection, rowToDevice)
        // ...
    }
} catch(e: Exception) {
    error(e.message)
}
info("Done.")

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published