javascript 如何解析来自 github API 的链接头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8735792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse link header from github API
提问by toxinlabs
the github API sends the pagination data for the json results in the http link header:
github API 在 http 链接标头中发送 json 结果的分页数据:
Link: <https://api.github.com/repos?page=3&per_page=100>; rel="next",
<https://api.github.com/repos?page=50&per_page=100>; rel="last"
since the github API is not the only API using this method (i think) i wanted to ask if someone has a useful little snippet to parse the link header (and convert it to an array for example) so that i can use it for my js app.
由于 github API 不是唯一使用此方法的 API(我认为)我想问一下是否有人有一个有用的小片段来解析链接标头(例如将其转换为数组),以便我可以将它用于我的js 应用程序。
i googled around but found nothing useful regarding how to parse pagination from json APIs
我用谷歌搜索,但没有发现关于如何从 json API 解析分页的有用信息
采纳答案by Kevin Sawicki
There is a PageLinksclass in the GitHub Java APIthat shows how to parse the Link
header.
GitHub Java API中有一个PageLinks类,用于显示如何解析标头。Link
回答by Cosmin
The parse-link-header NPM moduleexists for this purpose; its source can be found on githubunder a MIT license (free for commercial use).
parse-link-header NPM 模块就是为此而存在的;它的源代码可以在 GitHub 上找到,获得 MIT 许可(可免费用于商业用途)。
Installation is as simple as:
安装非常简单:
npm install parse-link-header
Usage looks like the following:
用法如下所示:
var parse = require('parse-link-header');
var parsed = parse('<https://api.github.com/repos?page=3&per_page=100>; rel="next", <https://api.github.com/repos?page=50&per_page=100>; rel="last"')
...after which one has parsed.next
, parsed.last
, etc:
...之后有parsed.next
, parsed.last
, 等:
{ next:
{ page: '3',
per_page: '100',
rel: 'next',
url: 'https://api.github.com/repos?page=3&per_page=100' },
last:
{ page: '50',
per_page: '100',
rel: 'last',
url: ' https://api.github.com/repos?page=50&per_page=100' } }
回答by Atul Varma
I found wombleton/link-headerson github. It appears to be made for the browser, as opposed to being an npm module, but it seems like it wouldn't be hard to modify it to work in a server-side environment. It uses pegjs to generate a real RFC 5988 parser rather than string splits, so it should work well for any link header, rather than just Github's.
我在 github 上找到了wombleton/link-headers。它似乎是为浏览器设计的,而不是一个 npm 模块,但似乎不难修改它以在服务器端环境中工作。它使用 pegjs 生成真正的 RFC 5988 解析器而不是字符串拆分,因此它应该适用于任何链接标头,而不仅仅是 Github 的。
回答by danriti
I found this Gistthat:
我发现这个要点:
Parse Github
Links
header in JavaScript
Links
在 JavaScript 中解析 Github标头
Tested it out on the Github API and it returns an object like:
在 Github API 上对其进行了测试,它返回一个对象,如:
var results = {
last: "https://api.github.com/repositories/123456/issues?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&state=open&since=2013-07-24T02%3A12%3A30.309Z&direction=asc&page=4"
next: "https://api.github.com/repositories/123456/issues?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&state=open&since=2013-07-24T02%3A12%3A30.309Z&direction=asc&page=2"
};
回答by pimbrouwers
I completely understand this is "technically" a JavaScript
thread. But, if you're like me and arrived here by Google'ing "how to parse Link header"I thought I'd share my solution for my envinronment (C#).
我完全理解这是“技术上”一个JavaScript
线程。但是,如果您像我一样通过 Google 的“如何解析链接标头”到达这里,我想我会分享我的环境 (C#) 解决方案。
public class LinkHeader
{
public string FirstLink { get; set; }
public string PrevLink { get; set; }
public string NextLink { get; set; }
public string LastLink { get; set;}
public static LinkHeader FromHeader(string linkHeader)
{
LinkHeader linkHeader = null;
if (!string.IsNullOrWhiteSpace(linkHeader))
{
string[] linkStrings = linkHeader.Split("\",");
if (linkStrings != null && linkStrings.Any())
{
linkHeader = new LinkHeader();
foreach (string linkString in linkStrings)
{
var relMatch = Regex.Match(linkString, "(?<=rel=\").+?(?=\")", RegexOptions.IgnoreCase);
var linkMatch = Regex.Match(linkString, "(?<=<).+?(?=>)", RegexOptions.IgnoreCase);
if (relMatch.Success && linkMatch.Success)
{
string rel = relMatch.Value.ToUpper();
string link = linkMatch.Value;
switch (rel)
{
case "FIRST":
linkHeader.FirstLink = link;
break;
case "PREV":
linkHeader.PrevLink = link;
break;
case "NEXT":
linkHeader.NextLink = link;
break;
case "LAST":
linkHeader.LastLink = link;
break;
}
}
}
}
}
return linkHeader;
}
}
Testing in a console app, using GitHub's example Link header:
在控制台应用程序中进行测试,使用 GitHub 的示例链接标头:
void Main()
{
string link = "<https://api.github.com/user/repos?page=3&per_page=100>; rel=\"next\",< https://api.github.com/user/repos?page=50&per_page=100>; rel=\"last\"";
LinkHeader linkHeader = LinkHeader.FromHeader(link);
}
回答by Anton Babenko
If you can use Python and don't want to implement full specification, but need to have something what work for Github API, then here we go:
如果您可以使用 Python 并且不想实现完整规范,但需要有一些适用于 Github API 的东西,那么我们开始:
import re
header_link = '<https://api.github.com/repos?page=3&per_page=100>; rel="next", <https://api.github.com/repos?page=50&per_page=100>; rel="last"'
if re.search(r'; rel="next"', header_link):
print re.sub(r'.*<(.*)>; rel="next".*', r'', header_link)
回答by Harman
Here is a simple javascript function that extracts the useful info from the link in a nice object notation.
这是一个简单的 javascript 函数,它从一个很好的对象表示法中的链接中提取有用的信息。
var linkParser = (linkHeader) => {
let re = /<([^\?]+\?[a-z]+=([\d]+))>;[\s]*rel="([a-z]+)"/g;
let arrRes = [];
let obj = {};
while ((arrRes = re.exec(linkHeader)) !== null) {
obj[arrRes[3]] = {
url: arrRes[1],
page: arrRes[2]
};
}
return obj;
}
It will output the result like this ==>
它会输出这样的结果 ==>
{
"next": {
"url": "https://api.github.com/user/9919/repos?page=2",
"page": "2"
},
"last": {
"url": "https://api.github.com/user/9919/repos?page=10",
"page": "10"
}
}
回答by noelbk
Here's a simple bash script with curl and sed to get all pages from a long query
这是一个带有 curl 和 sed 的简单 bash 脚本,用于从长查询中获取所有页面
url="https://api.github.com/repos/$GIT_USER/$GIT_REPO/issues"
while [ "$url" ]; do
echo "$url" >&2
curl -Ss -n "$url"
url="$(curl -Ss -I -n "$url" | sed -n -E 's/Link:.*<(.*?)>; rel="next".*//p')"
done > issues.json