diff --git a/app/lib/api.py b/app/lib/api.py index fac1c14..69bfff9 100644 --- a/app/lib/api.py +++ b/app/lib/api.py @@ -23,9 +23,10 @@ def add_parameter(self, key: str, value: str | int) -> None: def invalidator(self) -> str: return uuid.uuid4() - def build_query_string(self) -> str: + def build_query_string(self, add_invalidator=False) -> str: params = self.params - params["invalidator"] = self.invalidator() + if add_invalidator: + params["invalidator"] = self.invalidator() return "?" + urllib.parse.urlencode(params) if len(params) else "" def build_url(self) -> str: diff --git a/app/records/routes.py b/app/records/routes.py index 05d05a7..d3b81ed 100644 --- a/app/records/routes.py +++ b/app/records/routes.py @@ -118,10 +118,10 @@ async def archives_filters() -> list[Filter]: return filters -@router.get("/{item_id}/") +@router.get("/{id}/") async def item( - item_id: str, + id: str, ): # ) -> Record | RecordCreator | RecordArchive: rosetta_api = RosettaRecordDetails() - result = rosetta_api.get_result(item_id) + result = rosetta_api.get_result(id) return result diff --git a/app/records/schemas/details.py b/app/records/schemas/details.py index 2c42c05..5a781ab 100644 --- a/app/records/schemas/details.py +++ b/app/records/schemas/details.py @@ -5,6 +5,7 @@ class Details(BaseModel): type: str id: str = "" # dump: dict = {} # TEMP + source_url: str = "" def __init__(self, id: str): super().__init__() diff --git a/app/schemas/common_search_api.py b/app/schemas/common_search_api.py index d0b2414..d179919 100644 --- a/app/schemas/common_search_api.py +++ b/app/schemas/common_search_api.py @@ -23,6 +23,7 @@ class APISearchResponse(BaseModel): result_range_max: int = 0 results_per_page: int = 0 results: list[APISearchResult] = [] + source_url: str = "" def get_pages(self): return ( diff --git a/app/sources/rosetta/api.py b/app/sources/rosetta/api.py index 269c82e..02d1367 100644 --- a/app/sources/rosetta/api.py +++ b/app/sources/rosetta/api.py @@ -13,8 +13,7 @@ class RosettaRecords(GetAPI): - def __init__(self): - self.api_base_url = Config().ROSETTA_API_URL + api_base_url = Config().ROSETTA_API_URL class RosettaRecordsSearch(RosettaRecords): @@ -35,10 +34,11 @@ def get_result( url = self.build_url() print(url) raw_results = self.execute(url) - return self.parse_results(raw_results, page) + return self.parse_results(raw_results, page, url) - def parse_results(self, raw_results, page): + def parse_results(self, raw_results, page, source_url): response = RecordSearchResults() + response.source_url = source_url for r in raw_results["metadata"]: parsed_data = RosettaSourceParser(r["_source"]) record = RecordSearchResult() @@ -83,9 +83,9 @@ def get_result(self, id: str) -> dict: url = self.build_url() print(url) raw_results = self.execute(url) - return self.parse_results(raw_results) + return self.parse_results(raw_results, url) - def parse_results(self, raw_results): + def parse_results(self, raw_results, source_url): parsed_data = RosettaResponseParser(raw_results) if parsed_data.type() == "record": record = Record(parsed_data.id()) @@ -99,6 +99,7 @@ def parse_results(self, raw_results): record.closure_status = parsed_data.closure_status() record.languages = parsed_data.languages() record.access_condition = parsed_data.access_condition() + record.source_url = source_url return record.toJSON() if ( parsed_data.type() == "archive" @@ -110,6 +111,7 @@ def parse_results(self, raw_results): record.places = parsed_data.places() record.contact_info = parsed_data.contact_info() record.agents = parsed_data.agents() + record.source_url = source_url return record.toJSON() if parsed_data.type() == "agent": if parsed_data.actual_type() == "person": @@ -122,6 +124,7 @@ def parse_results(self, raw_results): record.functions = parsed_data.functions() record.history = parsed_data.functions() record.biography = parsed_data.biography() + record.source_url = source_url return record.toJSON() record = RecordCreator(parsed_data.id()) record.name = parsed_data.title() @@ -129,5 +132,6 @@ def parse_results(self, raw_results): record.places = parsed_data.places() record.identifier = parsed_data.identifier() record.history = parsed_data.functions() + record.source_url = source_url return record.toJSON() return {} diff --git a/app/sources/website.py b/app/sources/website.py index f53c377..3fd712c 100644 --- a/app/sources/website.py +++ b/app/sources/website.py @@ -6,15 +6,19 @@ class WagtailAPI(GetAPI): + api_base_url = Config().WAGTAIL_API_URL + def __init__(self): - self.api_url = Config().WAGTAIL_API_URL + self.api_base_url def get_result(self) -> dict: - url = f"{self.api_url}/pages/{self.build_query_string()}" + url = f"{self.api_base_url}/pages/{self.build_query_string()}" return self.execute(url) class WebsiteArticles(WagtailAPI): + api_path = "/pages/" + def add_query(self, query_string: str) -> None: self.add_parameter("search", query_string) @@ -24,10 +28,11 @@ def get_result(self, page: int | None = 1) -> dict: self.add_parameter("limit", self.results_per_page) # self.add_parameter("fields", "search_description,teaser_image_jpg") # TODO: Not all pages have a teaser image self.add_parameter("fields", "search_description") - url = f"{self.api_url}/pages/{self.build_query_string()}" + url = self.build_url() print(f"API request URL: {url}") raw_results = self.execute(url) response = ArticleSearchResults() + response.source_url = url for a in raw_results["items"]: article = Article() article.title = a["title"] @@ -39,7 +44,7 @@ def get_result(self, page: int | None = 1) -> dict: # article.image = a["teaser_image_jpg"] # TODO: Temp until all pages have a teaser_image page_details_api = WebsiteArticles() - page_details_url = f"{self.api_url}/pages/{article.id}" + page_details_url = f"{self.api_base_url}{self.api_path}{article.id}" page_details = page_details_api.execute(page_details_url) article.description = page_details["meta"]["search_description"] article.image = page_details["teaser_image_jpg"]