The cookbook example "Accessing HTTP Response Metadata While Streaming" doesn't stream

未关闭
#2,232 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
58/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
活跃
技术栈
shell
领域
documentation

调研方向

从标题为 "Accessing HTTP Response Metadata While Streaming" 的 cookbook 示例开始,并针对 https://example.com 重现该示例。确定一种比所示的使用 $in 或条件表达式的变通方案更简洁的流式处理形式;完成的标准是:在 HTTP body 仍可供后续 lines 命令使用的同时,打印出 status 和 content type。

由索引模型根据 Issue 内容生成。

描述

The example in question is:

http get --allow-errors https://api.example.com/events.jsonl
| metadata access {|meta|
    print $"Status: ($meta.http_response.status)"
    print $"Content-Type: ($meta.http_response.headers | where name == content-type | get value.0)"

    if $meta.http_response.status != 200 {
        error make {msg: $"Failed with status ($meta.http_response.status)"}
    } else { }
  }
| lines
| each { from json }
| where event_type == "error"

This won't work because the input to the closure is fed to the first statement, which is print $"Status: ...", while the closure output is taken from else {}. If the http response status is 200, the closure will always output nothing because the input for the closure was consumed by print. We can't test this because api.example.com is a fake subdomain, so let's modify the example to use example.com and not assume the output is json:

http get --allow-errors https://example.com
| metadata access {|meta|
    print $"Status: ($meta.http_response.status)"
    print $"Content-Type: ($meta.http_response.headers | where name == content-type | get value.0)"

    if $meta.http_response.status != 200 {
        error make {msg: $"Failed with status ($meta.http_response.status)"}
    } else { }
  }
| lines

If you run this, you will see that the status and content type are printed, but the http body is not returned from the closure.

One solution is to use the $in variable to collect the input and return it from the else statement:

http get --allow-errors https://example.com
| metadata access {|meta|
    let input = $in
    print $"Status: ($meta.http_response.status)"
    print $"Content-Type: ($meta.http_response.headers | where name == content-type | get value.0)"

    if $meta.http_response.status != 200 {
        error make {msg: $"Failed with status ($meta.http_response.status)"}
    } else { $input }
  }
| lines

This does return the http body from the closure, but the example is called "Accessing HTTP Response Metadata While Streaming", and $in collects the input, so it's no longer a stream.

If we want to make this work while streaming, we can make the if statement into the only top level statement of the closure by moving the print statements into its conditional expression:

http get --allow-errors https://example.com
| metadata access {|meta|
    if (
        print $"Status: ($meta.http_response.status)";
        print $"Content-Type: ($meta.http_response.headers | where name == content-type | get value.0)";

        $meta.http_response.status != 200
    ) {
        error make {msg: $"Failed with status ($meta.http_response.status)"}
    } else {  }
  }
| lines

When run, this streams the http body after printing the status and content-type. However, it is pretty hacky. If we were to put this in the cookbook, it would need an accompanying explanation as to how sticking statements in the conditional expression prevents them from eating the closure input. Perhaps someone with more nushell experience than me can come up with a cleaner way to write this?

主要语言
TypeScript
星标
258
派生
561
平均合并
3 小时 20 分钟
30 天内合并 PR
15

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

nushell/nushell.github.io 的其他 Issue

查看 nushell/nushell.github.io 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。