rsocket / rsocket-rust

RSocket Rust Implementation using Tokio
Apache License 2.0
199 stars 20 forks source link

Support headers on WebsocketClientTransport #51

Closed hplewis closed 2 years ago

hplewis commented 2 years ago

It would be good to support setting headers when using the WebsocketClientTransport. I imagine this would be done kinda like how the rsocket-java project does it, in that you can add any number of customer headers when setting up the connection, to be used when the connection is established.

Motivation

It would be useful to use this to create an rsocket client in rust which connects to an existing rsocket server that requires Authorization headers.

Desired solution

Either like:

    let client = RSocketFactory::connect()
        .transport(WebsocketClientTransport::from("127.0.0.1:8080"))
        .header("Authorization", "abc123")
        .setup(Payload::from("READY!"))
        .mime_type("text/plain", "text/plain")
        .start()
        .await?;

or

    let client = RSocketFactory::connect()
        .transport(WebsocketClientTransport::from("127.0.0.1:8080").header("Authorization", "abc123"))
        .setup(Payload::from("READY!"))
        .mime_type("text/plain", "text/plain")
        .start()
        .await?;

I'd need to look a little closer to see which would make more sense.

Considered alternatives

None really that would make much sense.

Additional context

N/A

jjeffcaii commented 2 years ago

It makes sense. TBD. Thanks!

jjeffcaii commented 2 years ago

It has been implemented in #52. Please use rsocket_rust_transport_websocket="0.7.3", here is a sample:

    async fn test_client() {
        let req: WebsocketRequest = WebsocketRequest::builder()
            .uri("ws://127.0.0.1:8080/hello")
            .header("x-foo-bar", "42")
            .method("GET")
            .body(())
            .unwrap();
        let tp = WebsocketClientTransport::from(req);
        let c = RSocketFactory::connect()
            .transport(tp)
            .start()
            .await
            .expect("connect failed");
        let res = c
            .request_response(Payload::builder().set_data_utf8("foo").build())
            .await
            .expect("request failed")
            .unwrap();

        println!("response: {:?}", res);
    }
jjeffcaii commented 2 years ago

@hplewis Please try it and feel free if you have any other questions. 😃

hplewis commented 2 years ago

Thanks! I'll try it out tomorrow. :)

hplewis commented 1 year ago

A bit awkward to respond a year and a half later, sorry. Got pulled away but back on this and can confirm it works! Thank you!