Is it possible to generate functions with Kotlin? ...
# getting-started
q
Is it possible to generate functions with Kotlin? I would like to take a list of endpoints from an API and their arguments and then build those into methods during runtime. So for example:
Copy code
{
  "api.test": [],
  "<http://apps.permissions.info|apps.permissions.info>": [
    "token"
  ],
  "apps.permissions.request": [
    "token",
    "scopes",
    "trigger_id"
  ],
So in the end I could call my method with myApp.api.test('Bearer s0met0ken') and myApp.apps.permissions.request('Bearer s0met0ken', 'user.write', 'aTrigger') Is this possible with some functional magic?
c
There’s nothing built-in to the language for this. Your best bet is probably creating custom Gradle task and using KotlinPoet (https://github.com/square/kotlinpoet) to generate source code, which gets compiled with the rest of your app
Alternatively, if you’re exclusively targeting the JVM, Retrofit is a really great option for easily building REST clients. And a recent release now supports coroutines with the
suspend
modifier! https://square.github.io/retrofit/
q
Thanks @Casey Brooks I will take a look!