Skip to content

Commit

Permalink
Added writing API response to BQ in a Thread (#588)
Browse files Browse the repository at this point in the history
* added writing api response to BQ in a thread

* linting
  • Loading branch information
HazalCiplak authored Jul 15, 2022
1 parent 323410d commit 8ba408a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion peerscout_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import html
from typing import NamedTuple, Optional
from threading import Thread
import jsonschema

from google.cloud.bigquery import Client
Expand Down Expand Up @@ -406,6 +407,17 @@ def write_peerscout_api_response_to_bq(
)


def write_peerscout_api_response_to_bq_in_a_thread(
recommendation_request: dict,
recommendation_response: dict
):
thread = Thread(
target=write_peerscout_api_response_to_bq,
args=(recommendation_request, recommendation_response)
)
thread.start()


def create_app():
app = Flask(__name__)
keyword_extractor = get_keyword_extractor(get_spacy_language_model_env())
Expand Down Expand Up @@ -493,7 +505,7 @@ def _peerscout_api():
reviewing_editor_recommendation_json=json_response_for_reviewing_editors
)

write_peerscout_api_response_to_bq(
write_peerscout_api_response_to_bq_in_a_thread(
recommendation_request=data,
recommendation_response=api_json_response
)
Expand Down
17 changes: 17 additions & 0 deletions tests_peerscout_api/unit_tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ def _get_editor_recommendations_for_api_mock() -> MagicMock:
yield mock


@pytest.fixture(name='write_peerscout_api_response_to_bq_in_a_thread_mock')
def _write_peerscout_api_response_to_bq_in_a_thread_mock() -> MagicMock:
with patch.object(target_module, 'write_peerscout_api_response_to_bq_in_a_thread') as mock:
yield mock


@pytest.fixture(name='get_keyword_extractor_mock', autouse=True)
def _get_keyword_extractor_mock() -> MagicMock:
with patch.object(target_module, 'get_keyword_extractor') as mock:
Expand Down Expand Up @@ -220,6 +226,17 @@ def test_should_respond_with_recomendation(
response = test_client.post('/api/peerscout', json=INPUT_DATA_VALID)
assert _get_ok_json(response) == get_valid_recommendation_response()

def test_should_call_write_peerscout_api_response_to_bq_in_a_thread_with_correct_params(
self,
test_client: FlaskClient,
write_peerscout_api_response_to_bq_in_a_thread_mock: MagicMock
):
response = test_client.post('/api/peerscout', json=INPUT_DATA_VALID)
write_peerscout_api_response_to_bq_in_a_thread_mock.assert_called_with(
recommendation_request=INPUT_DATA_VALID,
recommendation_response=_get_ok_json(response)
)


class TestGetRecommendationHtml:
def test_should_have_recomendation_heading_when_the_recomendation_not_avaliable(
Expand Down

0 comments on commit 8ba408a

Please sign in to comment.