Working initial version
This commit is contained in:
82
src/formex_viewer/server.py
Normal file
82
src/formex_viewer/server.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import lxml.etree as ET
|
||||
from fastapi import FastAPI, Response
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from formex_viewer.formex4 import FormexArticleConverter
|
||||
from formex_viewer.main import (
|
||||
CellarClient,
|
||||
CellarIdentifier,
|
||||
ContentType,
|
||||
Language,
|
||||
SystemName,
|
||||
)
|
||||
|
||||
origins = [
|
||||
"http://localhost:5173",
|
||||
]
|
||||
app = FastAPI()
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
type CacheKey = tuple[str, Language]
|
||||
|
||||
CACHE: dict[CacheKey, str] = {}
|
||||
|
||||
|
||||
def _get_fmx4_data(celex_id: str, language: Language) -> str:
|
||||
"""
|
||||
Fetch the FMX4 data from the server.
|
||||
"""
|
||||
|
||||
if (celex_id, language) in CACHE:
|
||||
return CACHE[(celex_id, language)]
|
||||
|
||||
client = CellarClient(language)
|
||||
cellar_id = CellarIdentifier(
|
||||
system_name=SystemName.CELEX,
|
||||
system_id=celex_id,
|
||||
)
|
||||
fmx4_data = client.publication_text(cellar_id, ContentType.ZIP_FMX4)
|
||||
|
||||
CACHE[(celex_id, language)] = fmx4_data
|
||||
|
||||
return fmx4_data
|
||||
|
||||
|
||||
@app.get("/{celex_id}/articles")
|
||||
def article_ids(celex_id: str, language: Language = Language.ENG):
|
||||
"""
|
||||
Fetch the article IDs from the server.
|
||||
"""
|
||||
fmx4_data = _get_fmx4_data(celex_id, language)
|
||||
xml = ET.fromstring(fmx4_data.encode("utf-8"))
|
||||
|
||||
article_xpath = "//ARTICLE/@IDENTIFIER"
|
||||
article_ids = xml.xpath(article_xpath)
|
||||
article_ids = [int(article_id.lstrip("0")) for article_id in article_ids]
|
||||
article_ids.sort()
|
||||
return article_ids
|
||||
|
||||
|
||||
@app.get("/{celex_id}/articles/{article_id}/{language}")
|
||||
def article(celex_id: str, article_id: int, language: Language = Language.ENG):
|
||||
"""
|
||||
Fetch an article from the server.
|
||||
"""
|
||||
fmx4_data = _get_fmx4_data(celex_id, language)
|
||||
xml = ET.fromstring(fmx4_data.encode("utf-8"))
|
||||
|
||||
article_xpath = "//ARTICLE"
|
||||
articles = xml.xpath(article_xpath)
|
||||
for article in articles:
|
||||
num = article.get("IDENTIFIER").lstrip("0")
|
||||
if num == str(article_id):
|
||||
return Response(
|
||||
FormexArticleConverter().convert_article(article),
|
||||
media_type="text/html",
|
||||
)
|
||||
Reference in New Issue
Block a user