recon: add /api/wiki-enrich endpoint (extraction #5 prep, additive) (#8)

HTTP wrapper over the wiki_index lookup so the (future) navi-places service can
fetch wiki enrichment over HTTP instead of reading recon's 2.1 GB
data/wiki_index.db directly (Phase A option B — HTTP coupling).

  GET /api/wiki-enrich?wikidata=<Qid>           (primary key)
  GET /api/wiki-enrich?name=<name>&country=<cc>  (fallback key)
  -> 200 {wiki_summary?, wiki_population?, wiki_url?, wikivoyage_url?}
  -> 400 if no usable key; 404 on no match. Public (no auth, like /api/place/*).

Route keys are wikidata_id / name+country — NOT osm_type/osm_id — because that
is how wiki_index is actually queried (the in-process _enrich_with_wiki_index
looks up by result['wikidata_id'] then name+country_code, never by OSM id; see
extraction-5-wiki-enrich-investigation.md). An osm-keyed route would have forced
a redundant in-recon place lookup.

Changes (additive only):
  - lib/place_detail.py: new standalone lookup_wiki_index(wikidata_id, name,
    country_code) doing the same two SELECTs + field/URL mapping as the
    in-process path, returning a dict or None. Pure DB read, never raises.
    `_enrich_with_wiki_index` is LEFT UNTOUCHED — it can be DRY-refactored to
    delegate to this in a later PR; the in-process enrichment path is unchanged.
  - lib/wiki_enrich_api.py: new wiki_enrich_bp blueprint with the route.
  - lib/api.py: register the blueprint (one block).
  - lib/wiki_enrich_api_test.py: 4 tests (hit-by-wikidata + decoded fields,
    no-match -> 404, name+country fallback, no-key -> 400) over an in-memory
    fixture DB; plain-assert style + __main__ runner (recon venv has no pytest).
    Verified green against recon's venv (flask 3.1.2).

Does NOT remove the in-process _enrich_with_wiki_index call from place_detail —
that happens in a later PR once navi-places is live and serving.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
malice 2026-05-22 13:23:08 -06:00 committed by GitHub
commit f42b1fef3b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 176 additions and 0 deletions

31
lib/wiki_enrich_api.py Normal file
View file

@ -0,0 +1,31 @@
"""Wiki-enrich API — read-only HTTP wrapper over the wiki_index lookup.
Extraction #5 prep: lets the (future) navi-places service fetch wiki enrichment
over HTTP instead of reading recon's 2.1 GB data/wiki_index.db directly. Additive
only does not change place_detail's in-process `_enrich_with_wiki_index` path.
GET /api/wiki-enrich?wikidata=<Qid> (primary key)
GET /api/wiki-enrich?name=<name>&country=<cc> (fallback key)
Public (no auth), matching /api/place/*. 400 if no usable key; 404 on no match.
"""
from flask import Blueprint, request, jsonify
from .place_detail import lookup_wiki_index
wiki_enrich_bp = Blueprint('wiki_enrich', __name__)
@wiki_enrich_bp.route('/api/wiki-enrich')
def api_wiki_enrich():
wikidata = (request.args.get('wikidata') or '').strip() or None
name = (request.args.get('name') or '').strip() or None
country = (request.args.get('country') or '').strip() or None
if not wikidata and not (name and country):
return jsonify({'error': 'provide ?wikidata=<id> or ?name=<name>&country=<cc>'}), 400
result = lookup_wiki_index(wikidata_id=wikidata, name=name, country_code=country)
if result is None:
return jsonify({'error': 'no wiki match'}), 404
return jsonify(result)