Skip to content

Commit

Permalink
feat(api-v3): return error when return_format is not one of our known…
Browse files Browse the repository at this point in the history
… formats.
  • Loading branch information
YishaiGlasner committed Nov 20, 2023
1 parent 1596f18 commit 8115d1f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ def test_wrap_all_entities(self):
self.assertTrue('<a class ="refLink"' in data['versions'][0]['text'][3])
self.assertTrue('<a href="/topics' in data['versions'][0]['text'][8])

def text_text_only(self):
def test_text_only(self):
response = c.get(f"/api/v3/texts/Shulchan_Arukh%2C_Orach_Chayim.1:1?return_format=text_only")
self.assertEqual(200, response.status_code)
data = json.loads(response.content)
self.assertFalse('<' in data['versions'][0]['text'])

def test_error_return_format(self):
response = c.get(f"/api/v3/texts/Shulchan_Arukh%2C_Orach_Chayim.1:1?return_format=not_valid")
self.assertEqual(400, response.status_code)
data = json.loads(response.content)
self.assertEqual(data['error'], "return_format should be one of those formats: ['default', 'wrap_all_entities', 'text_only'].")
4 changes: 4 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Text(View):

RETURN_FORMATS = ['default', 'wrap_all_entities', 'text_only']

def dispatch(self, request, *args, **kwargs):
try:
self.oref = Ref.instantiate_ref_with_legacy_parse_fallback(kwargs['tref'])
Expand Down Expand Up @@ -48,6 +50,8 @@ def get(self, request, *args, **kwargs):
versions_params = [self.split_piped_params(param_str) for param_str in versions_params]
fill_in_missing_segments = request.GET.get('fill_in_missing_segments', False)
return_format = request.GET.get('return_format', 'default')
if return_format not in self.RETURN_FORMATS:
return jsonResponse({'error': f'return_format should be one of those formats: {self.RETURN_FORMATS}.'}, status=400)
text_manager = TextManager(self.oref, versions_params, fill_in_missing_segments, return_format)
data = text_manager.get_versions_for_query()
data = self._handle_warnings(data)
Expand Down

0 comments on commit 8115d1f

Please sign in to comment.