rsocket-kotlin icon indicating copy to clipboard operation
rsocket-kotlin copied to clipboard

rsocket-kotlin-scripting target

Open yschimke opened this issue 4 years ago • 10 comments

What could we do to optimise the kotlin scripting experience?

https://github.com/yschimke/okurl-scripts/blob/master/commands/rsocketTcpProxy.main.kts

  1. single maven dependency to import to bring in JVM scripting dependencies

Collapse the DependsOn to a single line

  1. Utility functions for use in ascripting environments based around common operations, with lower barrier than normal public API. Make this a one liner.
val httpClient = HttpClient(ClientCIO) {
        install(ClientWebSockets)
        install(ClientRSocketSupport) {
          connector = RSocketConnector {
            loggerFactory = PrintLogger.withLevel(LoggingLevel.DEBUG)
            connectionConfig {
              payloadMimeType = config.payloadMimeType
            }
          }
        }
      }

yschimke avatar Dec 17 '20 05:12 yschimke

  1. should work with just:
@file:Repository("https://jcenter.bintray.com")
@file:DependsOn("io.rsocket.kotlin:rsocket-transport-ktor-client-jvm:0.12.0")
@file:DependsOn("io.ktor:ktor-client-cio-jvm:1.4.3")

All other are just transitive dependencies

  1. Not sure that we need to include something smaller than that. It's common pattern for ktor to install features like this. Simplest variant is:
HttpClient {
  install(WebSockets)
  install(RSocketSupport)
}

Engine will be auto-defined on JVM, and Client* aliases not needed (I was copied them from one of tests, where we have both server and client WS setup in one file).

  • with possibility to define extensions in kotlin, anyone can create smth like this:
fun RSocketHttpClient(block: RSocketConnectorBuilder.() -> Unit): HttpClient = HttpClient {
  install(WebSockets)
  install(RSocketSupport) {
    connector = RSocketConnector(block)
  }
}

val client = RSocketHttpClient {
  connectionConfig {
    payloadMimeType = config.payloadMimeType
  }
}

whyoleg avatar Dec 17 '20 07:12 whyoleg

For 2 the goal here isn't to knowingly configure Ktor. It's to achieve some scripting like task against rsocket APIs as a client or server.

yschimke avatar Dec 17 '20 07:12 yschimke

So the suggestion is a scripting library that combines some minimal set of common dependencies from 1. with the utility you defined in 2

fun RSocketHttpClient(block: RSocketConnectorBuilder.() -> Unit): HttpClient = HttpClient {
  install(WebSockets)
  install(RSocketSupport) {
    connector = RSocketConnector(block)
  }
}

Get the overall script down to

@file:Repository("https://jcenter.bintray.com")
@file:DependsOn("io.rsocket.kotlin:rsocket-scripting-jvm:0.12.0")

val client = RSocketHttpClient {
  connectionConfig {
    payloadMimeType = config.payloadMimeType
  }
}

val response = client.request(Payload(...json...))
.. do something with response

yschimke avatar Dec 17 '20 07:12 yschimke

Any objection if I put up a sample PR for debate?

yschimke avatar Dec 17 '20 07:12 yschimke

As I'm not as familiar with the code structure. Dependencies and imports and configuration is making even quite simple examples quite involved to quickly create and iterate on.

yschimke avatar Dec 17 '20 07:12 yschimke

Any objection if I put up a sample PR for debate?

yes! That's what I was thinking to ask you

whyoleg avatar Dec 17 '20 07:12 whyoleg

With future serialisation support (#112) such scripting functionality will be better than testing APIs through some CLI. Really look forward on it!

whyoleg avatar Dec 17 '20 07:12 whyoleg

I'm unlikely to take the on any time soon, so closing off

https://gist.github.com/yschimke/9b5d36790478a37776c97b7a73331578

yschimke avatar Dec 28 '20 18:12 yschimke

Let's leave it open for now, I can take it on my own some time in future, no hurry. I think, that scripting examples, experience - it's a great opportunity to share even simple rsocket clients / servers in one script, so anyone could try it out

whyoleg avatar Dec 28 '20 18:12 whyoleg

The rocket-cli command for the server above is

$ rsocket-cli --setup 'wss://rsocket-demo.herokuapp.com/rsocket'  tcp://localhost:9000

yschimke avatar Dec 28 '20 18:12 yschimke