Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nicer text/* response values #906

Open
thgoebel opened this issue Sep 6, 2024 · 0 comments
Open

Nicer text/* response values #906

thgoebel opened this issue Sep 6, 2024 · 0 comments

Comments

@thgoebel
Copy link

thgoebel commented Sep 6, 2024

For text response values, it is not very ergonomic to get the text (the response body).

Example

Consider the following API:

openapi: "3.0.0"

info:
  title: Hello World
  version: "1.0"

paths:
  /hello:
    get:
      operationId: getHello
      responses:
        "200":
          description: Get hello
          content:
            text/plain:
              schema:
                type: string
                example: Hello World

Progenitor generates the following code:

#[allow(clippy::all)]
impl Client {
    ///Sends a `GET` request to `/hello`
    pub async fn get_hello<'a>(&'a self) -> Result<ResponseValue<ByteStream>, Error<()>> {
        let url = format!("{}/hello", self.baseurl,);
        #[allow(unused_mut)]
        let mut request = self.client.get(url).build()?;
        let result = self.client.execute(request).await;
        let response = result?;
        match response.status().as_u16() {
            200u16 => Ok(ResponseValue::stream(response)),
            _ => Err(Error::UnexpectedResponse(response)),
        }
    }
}

To integrate this in my code and read the response, I need something like this:

use futures::StreamExt;

async fn test_hello() {
    let client = Client::new("baseurl");
    let resp_val = client.get_hello().await.unwrap();
    let mut stream = resp_val.into_inner_stream();

    let mut bytes: Vec<u8> = Vec::new();
    while let Some(n) = stream.next().await {
        let b = n.unwrap();
        bytes.extend_from_slice(&b);
    }
    let hello = String::from_utf8(bytes).unwrap();
    dbg!("{}", hello);
}

Expected behaviour

That's not very ergonomic. Sure, I know at a high-level that I need to collect the byte stream into a vector. But it still took me a while to figure out the exact types to make it compile...

It would be nice if there were a more obvious API that makes it easier to "just get the body string".

reqwest::Response already has useful methods like text() or json(). But since progenitor's client hides the reqwest::Response from me, I cannot use those.

Alternatively, it would be nice to have an example somewhere that shows how to read such basic text responses.

Version

(cargo-)progenitor 0.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant