Skip to content

Commit e24e26d

Browse files
committed
OAProc: fix non JSON outputs (#2311)
1 parent 1f3a29e commit e24e26d

3 files changed

Lines changed: 32 additions & 20 deletions

File tree

pygeoapi/api/processes.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,27 +327,12 @@ def get_jobs(api: API, request: APIRequest,
327327
job_result_url = f"{api.base_url}/jobs/{job_['identifier']}/results" # noqa
328328

329329
job2['links'] = [{
330-
'href': f'{job_result_url}?f={F_HTML}',
330+
'href': job_result_url,
331331
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/results',
332-
'type': FORMAT_TYPES[F_HTML],
333-
'title': l10n.translate(f'Results of job as HTML', request.locale), # noqa
334-
}, {
335-
'href': f'{job_result_url}?f={F_JSON}',
336-
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/results',
337-
'type': FORMAT_TYPES[F_JSON],
338-
'title': l10n.translate(f'Results of job as JSON', request.locale), # noqa
332+
'type': job_['mimetype'],
333+
'title': f"Results of job {job_id} as {job_['mimetype']}"
339334
}]
340335

341-
if job_['mimetype'] not in (FORMAT_TYPES[F_JSON],
342-
FORMAT_TYPES[F_HTML]):
343-
344-
job2['links'].append({
345-
'href': job_result_url,
346-
'rel': 'http://www.opengis.net/def/rel/ogc/1.0/results', # noqa
347-
'type': job_['mimetype'],
348-
'title': f"Results of job {job_id} as {job_['mimetype']}" # noqa
349-
})
350-
351336
serialized_jobs['jobs'].append(job2)
352337

353338
serialized_query_params = ''
@@ -528,7 +513,10 @@ def execute_process(api: API, request: APIRequest,
528513
pretty_print_ = False
529514
response2 = to_json(response, pretty_print_)
530515
else:
516+
pretty_print_ = False
531517
response2 = response
518+
if isinstance(response, (list, dict)):
519+
response2 = to_json(response, pretty_print_)
532520

533521
if (headers.get('Preference-Applied', '') == RequestedProcessExecutionMode.respond_async.value): # noqa
534522
LOGGER.debug('Asynchronous mode detected, returning statusInfo')

pygeoapi/process/hello_world.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@
9494
'minOccurs': 0,
9595
'maxOccurs': 1,
9696
'keywords': ['as_bytes']
97+
},
98+
'media_type': {
99+
'title': 'Media type',
100+
'description': 'Force a specific media type',
101+
'schema': {
102+
'type': 'string',
103+
'default': 'application/json'
104+
},
105+
'minOccurs': 0,
106+
'maxOccurs': 1,
107+
'keywords': ['media_type']
97108
}
98109
},
99110
'outputs': {
@@ -132,7 +143,7 @@ def __init__(self, processor_def):
132143
self.supports_outputs = True
133144

134145
def execute(self, data, outputs=None):
135-
mimetype = 'application/json'
146+
mimetype = data.get('media_type', 'application/json')
136147
name = data.get('name')
137148

138149
if name is None:

tests/api/test_processes.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_describe_processes(config, api_):
9595
assert process['title'] == 'Hello World'
9696
assert len(process['keywords']) == 3
9797
assert len(process['links']) == 6
98-
assert len(process['inputs']) == 3
98+
assert len(process['inputs']) == 4
9999
assert len(process['outputs']) == 1
100100
assert len(process['outputTransmission']) == 1
101101
assert len(process['jobControlOptions']) == 2
@@ -248,6 +248,12 @@ def test_execute_process(config, api_):
248248
'as_bytes': True
249249
}
250250
}
251+
req_body_11 = {
252+
'inputs': {
253+
'name': 'Test document as text/plain media type',
254+
'media_type': 'text/plain'
255+
}
256+
}
251257

252258
cleanup_jobs = set()
253259

@@ -422,6 +428,13 @@ def test_execute_process(config, api_):
422428
response2 = '{"id":"echo","value":"Hello Test document as bytes response!"}' # noqa
423429
assert response == response2
424430

431+
req = mock_api_request(data=req_body_11)
432+
rsp_headers, code, response = execute_process(api_, req, 'hello-world')
433+
434+
assert rsp_headers['Content-Type'] == 'text/plain'
435+
response2 = '{"id":"echo","value":"Hello Test document as text/plain media type!"}' # noqa
436+
assert response == response2
437+
425438
# Cleanup
426439
time.sleep(2) # Allow time for any outstanding async jobs
427440
for _, job_id in cleanup_jobs:

0 commit comments

Comments
 (0)