Skip to content

Commit

Permalink
Update search cache retrieval and update methods to use search cache …
Browse files Browse the repository at this point in the history
…endpoint rather than direct calls to database.
  • Loading branch information
maxachis committed Jun 15, 2024
1 parent e846650 commit 5213701
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions agency_homepage_searcher/homepage_searcher.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import csv

Check warning on line 1 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L1 <100>

Missing docstring in public module
Raw output
./agency_homepage_searcher/homepage_searcher.py:1:1: D100 Missing docstring in public module
import json
import os
import tempfile
from http import HTTPStatus

import requests
from dataclasses import dataclass
from pathlib import Path
from typing import List, Union
from enum import Enum


from agency_homepage_searcher.agency_info import AgencyInfo
from agency_homepage_searcher.google_searcher import GoogleSearcher, QuotaExceededError
from util.huggingface_api_manager import HuggingFaceAPIManager
from util.db_manager import DBManager
from util.miscellaneous_functions import get_filename_friendly_timestamp

MAX_SEARCHES = 100 # Maximum searches to perform at a time when searching for results

BASE_URL = "https://data-sources.pdap.io/api/"
SEARCH_CACHE_ENDPOINT = "homepage-search-cache"
FULL_CACHE_ENDPOINT = f"{BASE_URL}{SEARCH_CACHE_ENDPOINT}"

class SearchResultEnum(Enum):

Check failure on line 25 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L25 <302>

expected 2 blank lines, found 1
Raw output
./agency_homepage_searcher/homepage_searcher.py:25:1: E302 expected 2 blank lines, found 1
"""

Check warning on line 26 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L26 <200>

One-line docstring should fit on one line with quotes
Raw output
./agency_homepage_searcher/homepage_searcher.py:26:1: D200 One-line docstring should fit on one line with quotes

Check warning on line 26 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L26 <204>

1 blank line required after class docstring
Raw output
./agency_homepage_searcher/homepage_searcher.py:26:1: D204 1 blank line required after class docstring
Expand Down Expand Up @@ -94,6 +102,17 @@ def __init__(
self.database_manager = database_manager
self.huggingface_api_manager = huggingface_api_manager
self.us_state_reference = USStateReference(database_manager)
self.pdap_api_key = os.getenv("PDAP_API_KEY")

def get_search_cache_header(self) -> dict:
"""

Check warning on line 108 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L108 <205>

1 blank line required between summary line and description
Raw output
./agency_homepage_searcher/homepage_searcher.py:108:1: D205 1 blank line required between summary line and description
Returns a header for the search cache table.
Returns: dict
"""
return {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.pdap_api_key}"
}

def create_agency_info(self, agency_row: list) -> AgencyInfo:
"""

Check warning on line 118 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L118 <205>

1 blank line required between summary line and description
Raw output
./agency_homepage_searcher/homepage_searcher.py:118:1: D205 1 blank line required between summary line and description
Expand Down Expand Up @@ -123,8 +142,16 @@ def get_agencies_without_homepage_urls(self) -> list[AgencyInfo]:
Retrieves a list of agencies without homepage URLs.
Returns: list[AgencyInfo]
"""
agency_rows = self.database_manager.execute(SQL_GET_AGENCIES_WITHOUT_HOMEPAGE_URLS)
return [self.create_agency_info(agency_row) for agency_row in agency_rows]
# TODO: Implement endpoint
response = requests.get(
url=FULL_CACHE_ENDPOINT,
headers=self.get_search_cache_header()
)
if response.status_code != HTTPStatus.OK:
raise Exception(f"Failed to get search cache. Status code: {response.status_code}")

return [self.create_agency_info(row) for row in response.json()]


def search(self, agency_info: AgencyInfo) -> Union[SearchResults, None]:

Check failure on line 156 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L156 <303>

too many blank lines (2)
Raw output
./agency_homepage_searcher/homepage_searcher.py:156:5: E303 too many blank lines (2)
"""

Check warning on line 157 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L157 <205>

1 blank line required between summary line and description
Raw output
./agency_homepage_searcher/homepage_searcher.py:157:1: D205 1 blank line required between summary line and description
Expand Down Expand Up @@ -260,11 +287,15 @@ def update_search_cache(self, search_results: list[SearchResults]) -> None:
Args:
search_results:
"""
parameters = []
for search_result in search_results:
parameter = (search_result.agency_id, search_result.search_result_status.value)
parameters.append(parameter)
self.database_manager.executemany(SQL_UPDATE_CACHE, parameters)

response = requests.post(
url=FULL_CACHE_ENDPOINT,
data=json.dumps((search_result.search_result_status.value for search_result in search_results)),
headers=self.get_search_cache_header()
)

if response.status_code != HTTPStatus.OK:
raise Exception(f"Failed to update search cache. Status code: {response.status_code}")

def _try_search_agency_info(self, agency_info: AgencyInfo) -> Union[SearchResults, List]:
"""

Check warning on line 301 in agency_homepage_searcher/homepage_searcher.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] agency_homepage_searcher/homepage_searcher.py#L301 <205>

1 blank line required between summary line and description
Raw output
./agency_homepage_searcher/homepage_searcher.py:301:1: D205 1 blank line required between summary line and description
Expand Down

0 comments on commit 5213701

Please sign in to comment.