Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Add endpoints for single record details
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Jan 12, 2024
1 parent 3a445d5 commit f845057
Show file tree
Hide file tree
Showing 20 changed files with 875 additions and 98 deletions.
4 changes: 2 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def healthcheck():
from .records import routes as record_routes

app.include_router(
record_routes.router, prefix=f"{base_uri}/records", tags=["records"]
record_routes.router, prefix=f"{base_uri}/records", tags=["Records"]
)
app.include_router(
article_routes.router, prefix=f"{base_uri}/articles", tags=["articles"]
article_routes.router, prefix=f"{base_uri}/articles", tags=["Articles"]
)

return app
2 changes: 1 addition & 1 deletion app/articles/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def index(
website_api.add_parameter("order", "-first_published_at")
elif order == "date:asc":
website_api.add_parameter("order", "first_published_at")
results = website_api.get_results(page)
results = website_api.get_result(page)
return results


Expand Down
6 changes: 3 additions & 3 deletions app/articles/schemas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from app.schemas import APIResponse, APIResult
from app.schemas import APISearchResponse, APISearchResult
from pydantic import ConfigDict


class Article(APIResult):
class Article(APISearchResult):
url: str = ""
type: str = ""
first_published: str = ""
Expand All @@ -15,7 +15,7 @@ def toJSON(self):
return self.__dict__


class ArticleSearchResults(APIResponse):
class ArticleSearchResults(APISearchResponse):
model_config = ConfigDict(arbitrary_types_allowed=True)

results: list[Article] = []
7 changes: 6 additions & 1 deletion app/sources/api.py → app/lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

class BaseAPI(ABC):
@abstractmethod
def get_results(self):
def get_result(self):
pass


class GetAPI(BaseAPI):
api_base_url: str
api_path: str = "/"
results_per_page: int = 20
params: dict = {}

Expand All @@ -24,6 +26,9 @@ def build_query_string(self) -> str:
else ""
)

def build_url(self) -> str:
return f"{self.api_base_url}{self.api_path}{self.build_query_string()}"

def execute(self, url: str) -> dict:
r = requests.get(url)
if r.status_code == 404:
Expand Down
68 changes: 64 additions & 4 deletions app/records/routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from app.records import router
from app.schemas import Filter
from app.sources.rosetta import RosettaRecords
from app.sources.rosetta import RosettaRecordDetails, RosettaRecordsSearch

from .schemas import RecordSearchResults
from .schemas import (
ExternalRecord,
Record,
RecordArchive,
RecordCreator,
RecordSearchResults,
)


@router.get("/")
Expand All @@ -12,12 +18,12 @@ async def index(
groups: str | None = None,
highlight: bool | None = False,
) -> RecordSearchResults:
rosetta_api = RosettaRecords()
rosetta_api = RosettaRecordsSearch()
rosetta_api.add_query(q)
if groups:
# group:(tna,digitised,nonTna,creator,archive)
rosetta_api.add_parameter("filter", f"group:({groups})")
results = rosetta_api.get_results(page, highlight)
results = rosetta_api.get_result(page, highlight)
return results


Expand Down Expand Up @@ -46,3 +52,57 @@ async def filters() -> list[Filter]:
filters.append(level_filter)

return filters


@router.get("/{item_id}/")
async def item(
item_id: str,
): # ) -> Record | ExternalRecord | RecordCreator | RecordArchive:
rosetta_api = RosettaRecordDetails()
result = rosetta_api.get_result(item_id)
return result


@router.get("/external/")
async def external(
q: str = "",
page: int | None = 1,
highlight: bool | None = False,
) -> RecordSearchResults:
return index(q, page, "nonTna", highlight)


@router.get("/external/filters/")
async def external_filters() -> list[Filter]:
filters = []
return filters


@router.get("/creators/")
async def creators(
q: str = "",
page: int | None = 1,
highlight: bool | None = False,
) -> RecordSearchResults:
return index(q, page, "creator", highlight)


@router.get("/creators/filters/")
async def creators_filters() -> list[Filter]:
filters = []
return filters


@router.get("/archives/")
async def archives(
q: str = "",
page: int | None = 1,
highlight: bool | None = False,
) -> RecordSearchResults:
return index(q, page, "archive", highlight)


@router.get("/archives/filters/")
async def archives_filters() -> list[Filter]:
filters = []
return filters
9 changes: 9 additions & 0 deletions app/records/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .details import (
Details,
ExternalRecord,
Record,
RecordArchive,
RecordCreator,
RecordCreatorPerson,
)
from .search import RecordSearchResult, RecordSearchResults
72 changes: 72 additions & 0 deletions app/records/schemas/details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from pydantic import BaseModel


class Details(BaseModel):
type: str
id: str = ""
# dump: dict = {} # TEMP

def __init__(self, id: str):
super().__init__()
self.id = id

def toJSON(self):
return self.__dict__


class Record(Details):
type: str = "record"
ref: str | None = None
title: str = ""
date: str = ""
is_digitised: bool | None = None

def __init__(self, id: str):
super().__init__(id)


class ExternalRecord(Details):
type: str = "external_record"
ref: str | None = None
title: str = ""
covering_date: str | None = None
held_by: str | None = None

def __init__(self, id: str):
super().__init__(id)


class RecordCreator(Details):
type: str = "creator"
name: str = ""
date: str = ""
places: list[str] = []
identifier: str = ""
history: str = ""

def __init__(self, id: str):
super().__init__(id)


class RecordCreatorPerson(RecordCreator):
type: str = "person"
name_parts: dict = {}
date: str = ""
gender: str = ""
functions: str = ""
biography: str = ""

def __init__(self, id: str):
super().__init__(id)


class RecordArchive(Details):
type: str = "archive"
name: str = ""
archon: str = ""
places: list[str] = []
agents: dict = {}
contact_info: dict = {}

def __init__(self, id: str):
super().__init__(id)
10 changes: 4 additions & 6 deletions app/records/schemas.py → app/records/schemas/search.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import math

from app.schemas import APIResponse, APIResult
from app.schemas import APISearchResponse, APISearchResult
from pydantic import ConfigDict


class Record(APIResult):
class RecordSearchResult(APISearchResult):
ref: str | None = None
covering_date: str | None = None
held_by: str | None = None
Expand All @@ -16,7 +14,7 @@ def toJSON(self):
return self.__dict__


class RecordSearchResults(APIResponse):
class RecordSearchResults(APISearchResponse):
model_config = ConfigDict(arbitrary_types_allowed=True)

results: list[Record] = []
results: list[RecordSearchResult] = []
5 changes: 2 additions & 3 deletions app/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .api_response import APIResponse
from .api_result import APIResult
from .filter import Filter
from .common_filters_api import Filter
from .common_search_api import APISearchResponse, APISearchResult
10 changes: 0 additions & 10 deletions app/schemas/api_result.py

This file was deleted.

File renamed without changes.
13 changes: 10 additions & 3 deletions app/schemas/api_response.py → app/schemas/common_search_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
from fastapi import HTTPException
from pydantic import BaseModel, ConfigDict

from .api_result import APIResult

class APISearchResult(BaseModel):
title: str | None = None
id: int | str = ""
description: str | None = None

class APIResponse(BaseModel):
def toJSON(self):
return self.__dict__


class APISearchResponse(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

count: int = 0
Expand All @@ -15,7 +22,7 @@ class APIResponse(BaseModel):
result_range_min: int = 0
result_range_max: int = 0
results_per_page: int = 0
results: list[APIResult] = []
results: list[APISearchResult] = []

def get_pages(self):
return (
Expand Down
58 changes: 0 additions & 58 deletions app/sources/rosetta.py

This file was deleted.

1 change: 1 addition & 0 deletions app/sources/rosetta/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .api import RosettaRecordDetails, RosettaRecordsSearch
Loading

0 comments on commit f845057

Please sign in to comment.