rsocket / rsocket-kotlin

RSocket Kotlin multi-platform implementation
http://rsocket.io
Apache License 2.0
552 stars 37 forks source link

rsocket-kotlin-scripting target #128

Open yschimke opened 3 years ago

yschimke commented 3 years ago

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
            }
          }
        }
      }
whyoleg commented 3 years ago
  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

  2. 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 } }

yschimke commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

yschimke commented 3 years ago

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.

whyoleg commented 3 years ago

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

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

whyoleg commented 3 years ago

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

yschimke commented 3 years ago

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

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

whyoleg commented 3 years ago

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

yschimke commented 3 years ago

The rocket-cli command for the server above is

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