Trending

World technology news

→
Following the next page of a filtered stock catalog with HTTP Link
Software Engineering · 2017
Software Engineering2017

Following the next page of a filtered stock catalog with HTTP Link

A two-page local HTTP experiment preserves the filter and resource context with an RFC 8288 link.

Archive retrospective · Topic year 2017

Developer's JournalPublished: Dec 4, 20266 min read
Reading progress0%
In this article
  1. A catalog client chooses the wrong second page
  2. Resolve a target with the response context
  3. Preserve the family across a moving inventory
  4. What the loopback run observed
  5. Make the stopping condition observable
  6. Commit the export when its traversal finishes

A catalog client chooses the wrong second page

An overnight stock export requests the ink family and receives two items in its first response. The old client increments the page number while rebuilding the query and loses the family filter. A paper item can now reach the ink report. RFC 8288, published in October 2017, provides a model for links between resources and a way to express them in the HTTP Link field. The catalog can publish a target associated with the next relation. The client can follow that target without learning the server's query or cursor encoding. The fixture keeps a paper product in a separate family so the test can check the actual identities returned. Getting three rows is a weak assertion if the third belongs to the wrong catalog.

Illustrative diagram of the link from page one to page two of an ink catalog.

Resolve a target with the response context

An HTTP response can advertise links to another page, a document or other resources. Their relation types give clients the information they need to select a link by purpose. The demonstration emits one next relation with a query-only target. Resolving that target against the response URL keeps the path /export/items, while resolving it against a bare host would take the request to another path. RFC 8288 also defines an anchor parameter that can override the normal context. A general client that cannot honor such a change must handle the affected link according to the documented rules. The example intentionally leaves anchor out so the observed behavior can be attributed to normal relative reference resolution. It also keeps the catalog's path more than one component deep to expose an incorrect base.

Application flowConceptual sketch · summary of the article's technical flow

Preserve the family across a moving inventory

The first fixture response returns K17 and K25. Its next target contains both the ink family and the number of the next page. The second response returns K31. This catalog is static, so the same ordering holds between calls. A live inventory system can gain new items between requests. Offset pagination might then duplicate or omit an entry. A service could define a snapshot, an ordered key, or an opaque cursor with a documented scope. RFC 8288 describes how to attach and read the relation. The catalog still owns the continuity guarantee of the referenced pages. Tests for a moving inventory should verify the link target and the set and order of products returned under the service's own cursor policy.

What the loopback run observed

The Python 3.12 fixture starts an HTTP server on a loopback interface and lets the operating system select the port. The test makes a real request, verifies the first two items and asserts the complete Link field. It resolves the query-only target and makes the second request. The run printed the expected link followed by the sequence K17 K25 K31. It also verified that the terminal page had no Link field. The tiny extraction in this program is specific to the exact header emitted by its fixture. In general, commas can occur in targets and quoted attributes, and there can be several link values. A general parser needs to handle the grammar. This is a present-day run against the 2017 document, with no claim about a period browser implementation.

from urllib.parse import parse_qs, urlencode, urljoin, urlsplit
from urllib.request import urlopen
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
import json

CATALOG = {"ink": [["K17", "K25"], ["K31"]], "paper": [["P01"]]}

class Catalog(BaseHTTPRequestHandler):
    def do_GET(self):
        params = parse_qs(urlsplit(self.path).query)
        family = params["family"][0]
        page = int(params["page"][0])
        pages = CATALOG[family]
        body = json.dumps(pages[page - 1]).encode()
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        if page < len(pages):
            query = urlencode({"family": family, "page": page + 1})
            self.send_header("Link", f'<?{query}>; rel="next"')
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def log_message(self, *_):
        pass

with HTTPServer(("127.0.0.1", 0), Catalog) as server:
    worker = Thread(target=server.serve_forever)
    worker.start()
    try:
        first = f"http://127.0.0.1:{server.server_port}/export/items?family=ink&page=1"
        with urlopen(first, timeout=3) as response:
            assert json.load(response) == ["K17", "K25"]
            header = response.headers["Link"]
            assert header == '<?family=ink&page=2>; rel="next"'
            # Strict fixture contract, never a generic Link parser.
            target = urljoin(response.geturl(), header.split(">", 1)[0][1:])
        with urlopen(target, timeout=3) as response:
            assert json.load(response) == ["K31"]
            assert response.headers.get("Link") is None
        print(header)
        print("K17 K25 K31 | no next link on final page")
    finally:
        server.shutdown()
        worker.join()

Make the stopping condition observable

An absent next relation is the agreed terminal state for this catalog. Another service might return an empty page together with a useful next link after applying a visibility filter. A client that stops when the body is empty would miss later items in that protocol. A cycle is a separate failure mode. An operational client can record visited targets and impose a bounded request budget. Both conditions should produce a report saying that the export is incomplete. If the overnight job stores the family, run identifier and final page state, the morning team can distinguish a complete small catalog from a partial run. The fixture's final assertion exercises the stopping condition in addition to the order and identity of the products.

Commit the export when its traversal finishes

A consuming application can accumulate the pages under a single export identifier and check the collected product set before publishing it to other systems. A dropped connection after the first page should leave an unfinished run. A retried request must retain the original scope or use a resumable cursor documented by the service. To test future changes to routing, add a fixture with another nested path and assert the exact URL requested by the client. To test future data changes, add a wrong-family item and ensure the validator rejects it even when the row count is plausible. These checks connect an otherwise small HTTP field to a concrete business result, a complete and correctly scoped inventory report.

385 reads ✎ Write a comment

Reader discussion

Discussion prompt

What is the most important unanswered question about this topic?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.