Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pygeoapi/process/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Authors: Tom Kralidis <tomkralidis@gmail.com>
# Francesco Martinelli <francesco.martinelli@ingv.it>
#
# Copyright (c) 2022 Tom Kralidis
# Copyright (c) 2026 Tom Kralidis
# Copyright (c) 2024 Francesco Martinelli
#
# Permission is hereby granted, free of charge, to any person
Expand All @@ -29,6 +29,7 @@
#
# =================================================================

import json
import logging

from pygeoapi.process.base import BaseProcessor, ProcessorExecuteError
Expand Down Expand Up @@ -82,6 +83,17 @@
'minOccurs': 0,
'maxOccurs': 1,
'keywords': ['message']
},
'as_bytes': {
'title': 'As bytes',
'description': 'Whether to force return as bytes',
'schema': {
'type': 'bool',
'default': False
},
'minOccurs': 0,
'maxOccurs': 1,
'keywords': ['as_bytes']
}
},
'outputs': {
Expand Down Expand Up @@ -136,6 +148,9 @@ def execute(self, data, outputs=None):
'value': value
}

if data.get('as_bytes', False):
json.dumps(produced_outputs).encode('utf-8')

return mimetype, produced_outputs

def __repr__(self):
Expand Down
5 changes: 3 additions & 2 deletions pygeoapi/process/manager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Ricardo Garcia Silva <ricardo.garcia.silva@geobeyond.it>
# Francesco Martinelli <francesco.martinelli@ingv.it>
#
# Copyright (c) 2024 Tom Kralidis
# Copyright (c) 2026 Tom Kralidis
# (c) 2023 Ricardo Garcia Silva
# (c) 2026 Francesco Martinelli
#
Expand Down Expand Up @@ -277,7 +277,8 @@ def _execute_handler_sync(self, p: BaseProcessor, job_id: str,
current_status = JobStatus.running
jfmt, outputs = p.execute(data_dict, **extra_execute_parameters)

if isinstance(outputs, bytes):
if isinstance(outputs, bytes) and outputs.isascii():
LOGGER.debug('output is ASCII; decoding utf-8')
outputs = outputs.decode('utf-8')

if requested_response == RequestedResponse.document.value:
Expand Down
14 changes: 13 additions & 1 deletion tests/api/test_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_describe_processes(config, api_):
assert process['title'] == 'Hello World'
assert len(process['keywords']) == 3
assert len(process['links']) == 6
assert len(process['inputs']) == 2
assert len(process['inputs']) == 3
assert len(process['outputs']) == 1
assert len(process['outputTransmission']) == 1
assert len(process['jobControlOptions']) == 2
Expand Down Expand Up @@ -242,6 +242,12 @@ def test_execute_process(config, api_):
'name': 'Test document'
}
}
req_body_10 = {
'inputs': {
'name': 'Test document as bytes response',
'as_bytes': True
}
}

cleanup_jobs = set()

Expand Down Expand Up @@ -410,6 +416,12 @@ def test_execute_process(config, api_):
response2 = '{"id":"echo","value":"Hello Test document!"}'
assert response == response2

req = mock_api_request(data=req_body_10)
rsp_headers, code, response = execute_process(api_, req, 'hello-world')

response2 = '{"id":"echo","value":"Hello Test document as bytes response!"}' # noqa
assert response == response2

# Cleanup
time.sleep(2) # Allow time for any outstanding async jobs
for _, job_id in cleanup_jobs:
Expand Down
Loading