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

enable stream when content-type is text/event-stream #360

Merged
merged 2 commits into from
Apr 11, 2024

Conversation

zuisong
Copy link
Contributor

@zuisong zuisong commented Apr 7, 2024

closes #204

@zuisong
Copy link
Contributor Author

zuisong commented Apr 7, 2024

I have no idea how to write a test case for this feature.

@zuisong zuisong force-pushed the auto-enable-stream branch 3 times, most recently from 680e8af to a107333 Compare April 7, 2024 09:50
src/main.rs Outdated Show resolved Hide resolved
src/printer.rs Outdated Show resolved Hide resolved
@ducaale
Copy link
Owner

ducaale commented Apr 10, 2024

Not something to address in this PR but I noticed that HTTPie can format payloads contained in individual events

$ http https://sse.dev/test --body
data: {
    "msg": "It works!",
    "now": 1712764259321,
    "sse_dev": "is great",
    "testing": true
}

data: {
    "msg": "It works!",
    "now": 1712764261322,
    "sse_dev": "is great",
    "testing": true
}

self,
ContentType::Unknown | ContentType::UrlencodedForm | ContentType::Multipart
)
match self {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensure the author updates this method when adding a content-type in the future.

Copy link
Owner

@ducaale ducaale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! just one final comment:

@@ -168,8 +168,8 @@ Example: --print=Hb"
pub quiet: bool,

/// Always stream the response body.
#[clap(short = 'S', long)]
pub stream: bool,
#[clap(short = 'S', long, default_missing_value = "true", num_args= 0..=1, require_equals = true)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to prefer Option<bool> over bool? It affects the generated help output, but in terms of functionality, the result is the same: false/true versus None/Some(true)

Before

-S, --stream      Always stream the response body

After

-S, --stream[=<STREAM>]   Always stream the response body [possible values: true, false]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to provide users with the ability to choose to turn off automatic print body as stream.

cli args stream value description
xh -S Some(true) alway enable stream
xh -S=true Some(true) alway enable stream
xh -S=false Some(false) alway disable stream
xh None auto enable stream when meet stream body

@@ -447,6 +447,9 @@ impl Printer {
let compression_type = get_compression_type(response.headers());
let mut body = decompress(response, compression_type);

// Automatically activate stream mode when it hasn't been set by the user and the content type is stream
let stream = self.stream.unwrap_or(content_type.is_stream());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if user set -S=false, it will't print body as stream

@ducaale ducaale merged commit 269941e into ducaale:master Apr 11, 2024
9 checks passed
@zuisong zuisong deleted the auto-enable-stream branch April 11, 2024 13:16
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

Successfully merging this pull request may close these issues.

Support auto-enable --stream when meet server sent events
2 participants