Skip to content

Commit

Permalink
refactor using custom response object
Browse files Browse the repository at this point in the history
  • Loading branch information
kyokukou committed Nov 6, 2024
1 parent abd284a commit 7261955
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 40 deletions.
14 changes: 7 additions & 7 deletions oaipmh/requests/data_queries.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Dict
from oaipmh.serializers.output_formats import Response

from oaipmh.serializers.output_formats import InteriorData

def get_record(params: Dict[str, str]) -> InteriorData:
return
def get_record(params: Dict[str, str]) -> Response:
return "<a>b</a>", 200, {}

def list_records(params: Dict[str, str]) -> InteriorData:
return
def list_records(params: Dict[str, str]) -> Response:
return "<a>b</a>", 200, {}

def list_identifiers(params: Dict[str, str]) -> InteriorData:
return
def list_identifiers(params: Dict[str, str]) -> Response:
return "<a>b</a>", 200, {}
14 changes: 7 additions & 7 deletions oaipmh/requests/info_queries.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Dict
from oaipmh.serializers.output_formats import InteriorData
from oaipmh.serializers.output_formats import Response

def identify(params: Dict[str, str]) -> InteriorData:
return
def identify(params: Dict[str, str]) -> Response:
return "<a>b</a>", 200, {}

def list_metadata_formats(params: Dict[str, str]) -> InteriorData:
return
def list_metadata_formats(params: Dict[str, str]) -> Response:
return "<a>b</a>", 200, {}

def list_sets(params: Dict[str, str]) -> InteriorData:
return
def list_sets(params: Dict[str, str]) -> Response:
return "<a>b</a>", 200, {}
16 changes: 6 additions & 10 deletions oaipmh/requests/routes.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
from typing import Dict
from datetime import datetime, timezone
from flask import Blueprint, request, Response, render_template
from flask import Blueprint, request, render_template

from oaipmh.requests.verb_sorter import verb_sorter
from oaipmh.serializers.output_formats import Response

blueprint = Blueprint('general', __name__)


@blueprint.route("/oai", methods=['GET', 'POST'])
def oai() -> Response:

#TODO what happens if duplicate params
#TODO duplicate params dont create errors, technically not to spec
params: Dict[str, str] = request.args.to_dict() if request.method == 'GET' else request.form.to_dict()
result=verb_sorter(params)

response_xml=render_template("base.xml",
response_date=datetime.now(timezone.utc),
request_info="request info", #TODO
interior_xml="interior data" #TODO
)
headers={"Content-Type":"application/xml"}
response, code, headers=verb_sorter(params)
headers["Content-Type"]="application/xml"

return response_xml, 200, headers
return response, code, headers

@blueprint.route('/favicon.ico')
def favicon():
Expand Down
5 changes: 3 additions & 2 deletions oaipmh/requests/verb_sorter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Dict


from oaipmh.requests.info_queries import identify, list_metadata_formats, list_sets
from oaipmh.requests.data_queries import get_record, list_identifiers, list_records
from oaipmh.serializers.output_formats import InteriorData
from oaipmh.serializers.output_formats import Response
from oaipmh.data.oai_errors import OAIBadVerb

def verb_sorter(params: Dict[str, str]) -> InteriorData:
def verb_sorter(params: Dict[str, str]) -> Response:
"""
sorts OAI queries to the appropriate handler based on their verb statement
this defines what the client is asking for as per the OAI standard
Expand Down
4 changes: 2 additions & 2 deletions oaipmh/serializers/output_formats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from typing import Tuple, Dict, Any

class InteriorData: #TODO placeholder return type
value=""
Response = Tuple[Dict[str, Any], int, Dict[str, Any]]
24 changes: 12 additions & 12 deletions tests/test_verb_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,77 @@
def test_get_record(test_client):
params = {"verb": "GetRecord", "identifier": "oai:example.org:record123"}

with patch('oaipmh.requests.verb_sorter.get_record') as mock_get_record:
with patch('oaipmh.requests.verb_sorter.get_record', return_value=("working", 200, {})) as mock_get_record:
response = test_client.get("/oai", query_string=params)
assert response.status_code == 200
mock_get_record.assert_called_once_with(params)

with patch('oaipmh.requests.verb_sorter.get_record') as mock_get_record:
with patch('oaipmh.requests.verb_sorter.get_record', return_value=("working", 200, {})) as mock_get_record:
response = test_client.post("/oai", data=params)
assert response.status_code == 200
mock_get_record.assert_called_once_with(params)

def test_list_records(test_client):
params = {"verb": "ListRecords", "metadataPrefix": "oai_dc"}

with patch('oaipmh.requests.verb_sorter.list_records') as mock_list_records:
with patch('oaipmh.requests.verb_sorter.list_records', return_value=("working", 200, {})) as mock_list_records:
response = test_client.get("/oai", query_string=params)
assert response.status_code == 200
mock_list_records.assert_called_once_with(params)

with patch('oaipmh.requests.verb_sorter.list_records') as mock_list_records:
with patch('oaipmh.requests.verb_sorter.list_records', return_value=("working", 200, {})) as mock_list_records:
response = test_client.post("/oai", data=params)
assert response.status_code == 200
mock_list_records.assert_called_once_with(params)

def test_list_identifiers(test_client):
params = {"verb": "ListIdentifiers", "metadataPrefix": "oai_dc"}

with patch('oaipmh.requests.verb_sorter.list_identifiers') as mock_list_identifiers:
with patch('oaipmh.requests.verb_sorter.list_identifiers', return_value=("working", 200, {})) as mock_list_identifiers:
response = test_client.get("/oai", query_string=params)
assert response.status_code == 200
mock_list_identifiers.assert_called_once_with(params)

with patch('oaipmh.requests.verb_sorter.list_identifiers') as mock_list_identifiers:
with patch('oaipmh.requests.verb_sorter.list_identifiers', return_value=("working", 200, {})) as mock_list_identifiers:
response = test_client.post("/oai", data=params)
assert response.status_code == 200
mock_list_identifiers.assert_called_once_with(params)

def test_identify(test_client):
params = {"verb": "Identify"}

with patch('oaipmh.requests.verb_sorter.identify') as mock_identify:
with patch('oaipmh.requests.verb_sorter.identify', return_value=("working", 200, {})) as mock_identify:
response = test_client.get("/oai", query_string=params)
assert response.status_code == 200
mock_identify.assert_called_once_with(params)

with patch('oaipmh.requests.verb_sorter.identify') as mock_identify:
with patch('oaipmh.requests.verb_sorter.identify', return_value=("working", 200, {})) as mock_identify:
response = test_client.post("/oai", data=params)
assert response.status_code == 200
mock_identify.assert_called_once_with(params)

def test_list_metadata_formats(test_client):
params = {"verb": "ListMetadataFormats"}

with patch('oaipmh.requests.verb_sorter.list_metadata_formats') as mock_list_metadata_formats:
with patch('oaipmh.requests.verb_sorter.list_metadata_formats', return_value=("working", 200, {})) as mock_list_metadata_formats:
response = test_client.get("/oai", query_string=params)
assert response.status_code == 200
mock_list_metadata_formats.assert_called_once_with(params)

with patch('oaipmh.requests.verb_sorter.list_metadata_formats') as mock_list_metadata_formats:
with patch('oaipmh.requests.verb_sorter.list_metadata_formats', return_value=("working", 200, {})) as mock_list_metadata_formats:
response = test_client.post("/oai", data=params)
assert response.status_code == 200
mock_list_metadata_formats.assert_called_once_with(params)

def test_list_sets(test_client):
params = {"verb": "ListSets"}

with patch('oaipmh.requests.verb_sorter.list_sets') as mock_list_sets:
with patch('oaipmh.requests.verb_sorter.list_sets', return_value=("working", 200, {})) as mock_list_sets:
response = test_client.get("/oai", query_string=params)
assert response.status_code == 200
mock_list_sets.assert_called_once_with(params)

with patch('oaipmh.requests.verb_sorter.list_sets') as mock_list_sets:
with patch('oaipmh.requests.verb_sorter.list_sets', return_value=("working", 200, {})) as mock_list_sets:
response = test_client.post("/oai", data=params)
assert response.status_code == 200
mock_list_sets.assert_called_once_with(params)
Expand Down

0 comments on commit 7261955

Please sign in to comment.