oxidecomputer / oxidecomputer/progenitor

Nicer text/* response values

Open
#906 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1k
Forks
136
Avg merge
8h 36m
Merged PRs (30d)
14

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated ResponseValue and its into_inner_stream path shown in the issue, then compare the reqwest::Response text() and json() APIs it references. Clarify whether the desired outcome is a simpler text-body API or an example for collecting the stream. Done means the chosen approach makes basic text responses straightforward and is demonstrated clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.