我正在尝试使用应用程序的API文档。
为了调用它,我使用的是代码:
curl -X GET \
'https://api.program.com/v1/notes?page=1&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer xxx123xxx456xxx789xxx0001' \
-H 'cache-control: no-cache'
由于明显的原因,此处的令牌已更改。但是,我真正尝试设置的内容涉及page=1
参数。
我的问题有两个:
page=1
之后
然后将运行page=2
,page=3
,... 我当前的设置看起来像这样,但是它满足了我的需求(我不太了解),而且我也不怎么打破循环:
for ((i=1;i<=5;i++)); do
curl -X GET \
'https://api.program.com/v1/notes?page=1&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer xxx123xxx456xxx789xxx0001' \
-H 'cache-control: no-cache'
done
没有更多记录时的结果如下:
{
"meta": {
"pageSize": 1000,
"page": 65
},
"links": {
"self": "/v1/conversations?page=65&pageSize=1000&sort=desc",
"first": "/v1/conversations?page=1&pageSize=1000&sort=desc",
"prev": "/v1/conversations?page=64&pageSize=1000&sort=desc",
"next": null
},
"data": []
}
根据查尔斯·达菲(Charles Duffy)的回应,我正在运行的内容-被保存为test_run.sh
:
getPage () {
curl -X GET 'https://api.test.com/v1/test?page="$1"&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9Cim97g123NXpMkS_Jc9xggCYlMQVvKsAeBCw' \
-H 'cache-control: no-cache'
}
但是返回错误:
MacBook-Pro-8:~ admin$ ./test_run.sh
-bash: ./json_blob.sh: Permission denied
但是,当我在命令行中直接运行以下命令时,它会很好地返回结果:
curl -X GET 'https://api.test.com/v1/test?page="$1"&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9Cim97g123NXpMkS_Jc9xggCYlMQVvKsAeBCw' \
-H 'cache-control: no-cache' > test_run.json
答案 0 :(得分:0)
对于每次迭代,您都需要检查是否存在"next" : null
,如果找到,则中断循环。
while : ; do
if [[ $(curl -X GET 'https://api.program.com/v1/notes?page=1&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer xxx123xxx456xxx789xxx0001' \
-H 'cache-control: no-cache' | jq '.link.next') == 'null' ]] ; then
break #Exit the loop
fi
done
答案 1 :(得分:0)
在实践中,我可能会写得更类似于以下内容:
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform float u_time;
uniform sampler2D u_buffer_0;
#define uv gl_FragCoord.xy / u_resolution.xy
#define ts abs(sin(u_time))
#ifdef BUFFER_0
void main() {
float v = 1.0;
gl_FragColor = vec4(v, 1.0, 0.0, 1.0);
}
#else
void main() {
vec3 color = texture2D(u_buffer_0, uv).rgb;
gl_FragColor = vec4(color, 1.0);
}
#endif
此处的输出文件名为#!/usr/bin/env bash
case $BASH_VERSION in ''|[12].*) echo "Bash 3.0+ required" >&2; exit 1;; esac
# Given a page number, return text of that page.
# Note that we switch from single to double quotes before expanding $1
getPage() {
curl --fail -X GET 'https://api.program.com/v1/notes?page='"$1"'&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer xxx123xxx456xxx789xxx0001' \
-H 'cache-control: no-cache'
}
# return a stream with *all* page text, from page 1 until
# the first one with no .link.next.
getAllPages() {
local page i=1
while page=$(getPage "$i"); do
printf '%s\n' "$page"
if [[ $(jq '.links.next' <<<"$page") = null ]]; then
break
fi
(( ++i ))
done
}
getAllPages \
| jq -c '.data[] | .attributes | {text: .preview, from: .meta.from}' \
> data_json_blob.jsonl
,因为它采用JSONLines格式,而不是标准JSON。 (您不能将多个对象放在一个JSON文件中,而不必将它们封装在列表或其他容器中并使其有效;因此,当您在输入中提供1:1 .jsonl
转换时,多个对象不只一个,通常,您会得到JSONL,而不是JSON。