question 303 HTTP & API Clients · easy
Read a Link header
GitHub's API puts its pagination in a Link response header instead of the body: <https://api.github.com/…&page=2>; rel="next", <https://api.github.com/…&page=5>; rel="last". Write parse_link_header(value): return a dictionary from each rel name to its URL.
Function
parse_link_header(value)valuethe header text: entries separated by commas, each
<URL> followed by ; key="value" parameters, one of them relReturns
{rel: url}; {} for an empty header; entries without a rel are ignoredExamples
callparse_link_header('<https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=2>; rel="next", <https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=5>; rel="last"')returns{'next': 'https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=2', 'last': 'https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=5'}
callparse_link_header('<https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=4>; rel="prev", <https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=1>; rel="first"')returns{'prev': 'https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=4', 'first': 'https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=1'}
callparse_link_header('<https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=3>; rel="next"')returns{'next': 'https://api.github.com/repos/tidewater/smartqueue/issues?state=open&per_page=2&page=3'}