-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LinkData model for parsing HTTP Link headers
Add `safir.models.LinkData`, which parses HTTP `Link` headers for pagination information. This is useful for clients of services that use the database pagination support, including their test suites.
- Loading branch information
Showing
5 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### New features | ||
|
||
- Add new `safir.models.LinkData` model that parses the contents of an HTTP `Link` header and extracts pagination information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Representation for a ``Link`` HTTP header.""" | ||
|
||
from __future__ import annotations | ||
|
||
import re | ||
from dataclasses import dataclass | ||
from typing import Self | ||
|
||
__all__ = ["LinkData"] | ||
|
||
_LINK_REGEX = re.compile(r'\s*<(?P<target>[^>]+)>;\s*rel="(?P<type>[^"]+)"') | ||
"""Matches a component of a valid ``Link`` header.""" | ||
|
||
|
||
@dataclass | ||
class LinkData: | ||
"""Holds the data returned in an :rfc:`8288` ``Link`` header.""" | ||
|
||
prev_url: str | None | ||
"""URL of the previous page, or `None` for the first page.""" | ||
|
||
next_url: str | None | ||
"""URL of the next page, or `None` for the last page.""" | ||
|
||
first_url: str | None | ||
"""URL of the first page.""" | ||
|
||
@classmethod | ||
def from_header(cls, header: str | None) -> Self: | ||
"""Parse an :rfc:`8288` ``Link`` with pagination URLs. | ||
Parameters | ||
---------- | ||
header | ||
Contents of an RFC 8288 ``Link`` header. | ||
Returns | ||
------- | ||
LinkData | ||
Parsed form of that header. | ||
""" | ||
links = {} | ||
if header: | ||
for element in header.split(","): | ||
if m := re.match(_LINK_REGEX, element): | ||
if m.group("type") in ("prev", "next", "first"): | ||
links[m.group("type")] = m.group("target") | ||
elif m.group("type") == "previous": | ||
links["prev"] = m.group("target") | ||
|
||
return cls( | ||
prev_url=links.get("prev"), | ||
next_url=links.get("next"), | ||
first_url=links.get("first"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters