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

Commit

Permalink
Add source URL to responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Jan 15, 2024
1 parent f12a319 commit 7704484
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
5 changes: 3 additions & 2 deletions app/lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions app/records/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/records/schemas/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Details(BaseModel):
type: str
id: str = ""
# dump: dict = {} # TEMP
source_url: str = ""

def __init__(self, id: str):
super().__init__()
Expand Down
1 change: 1 addition & 0 deletions app/schemas/common_search_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
16 changes: 10 additions & 6 deletions app/sources/rosetta/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -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())
Expand All @@ -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"
Expand All @@ -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":
Expand All @@ -122,12 +124,14 @@ 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()
record.date = parsed_data.date()
record.places = parsed_data.places()
record.identifier = parsed_data.identifier()
record.history = parsed_data.functions()
record.source_url = source_url
return record.toJSON()
return {}
13 changes: 9 additions & 4 deletions app/sources/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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"]
Expand All @@ -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"]
Expand Down

0 comments on commit 7704484

Please sign in to comment.