Nicer text/* response values

Đang mở
#906 2 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu với ResponseValue được tạo và đường dẫn into_inner_stream của nó được hiển thị trong issue, sau đó so sánh các API text() và json() của reqwest::Response mà nó tham chiếu. Làm rõ liệu kết quả mong muốn là một API đơn giản hơn cho body dạng văn bản hay một ví dụ về việc thu thập stream. Công việc được xem là hoàn tất khi cách tiếp cận được chọn giúp các response văn bản cơ bản trở nên dễ xử lý và được minh họa rõ ràng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Rust
Star
1k
Fork
142
Merge trung bình
19 giờ 13 phút
Pull request đã merge (30 ngày)
16

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của oxidecomputer/progenitor

Tất cả issue của oxidecomputer/progenitor

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.