From d25d0cfa65d7e334ad7bdec2ef419a946aad3e62 Mon Sep 17 00:00:00 2001 From: Bernhard Mallinger Date: Sat, 16 Dec 2023 13:20:09 +0100 Subject: [PATCH] Use same format handling in collection coverage as elsewhere (#1412) This now fixes the issue that the `Accept` header wasn't considered for format selection. This adds netcdf as a known mime type in order to be able to test this, but also for actual clients to use. Note that if we want to allow other mime types such as `application/x-grib2`, we also have to add them to `FORMAT_TYPES`. Fixes #1390 --- pygeoapi/api.py | 12 +++++------- tests/test_api.py | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pygeoapi/api.py b/pygeoapi/api.py index fa7cf90..3d34ba5 100644 --- a/pygeoapi/api.py +++ b/pygeoapi/api.py @@ -107,6 +107,7 @@ F_JSONLD = 'jsonld' F_GZIP = 'gzip' F_PNG = 'png' F_MVT = 'mvt' +F_NETCDF = 'NetCDF' #: Formats allowed for ?f= requests (order matters for complex MIME types) FORMAT_TYPES = OrderedDict(( @@ -114,7 +115,8 @@ FORMAT_TYPES = OrderedDict(( (F_JSONLD, 'application/ld+json'), (F_JSON, 'application/json'), (F_PNG, 'image/png'), - (F_MVT, 'application/vnd.mapbox-vector-tile') + (F_MVT, 'application/vnd.mapbox-vector-tile'), + (F_NETCDF, 'application/x-netcdf'), )) #: Locale used for system responses (e.g. exceptions) @@ -2433,11 +2435,10 @@ class API: """ query_args = {} - format_ = F_JSON + format_ = request.format or F_JSON # Force response content type and language (en-US only) headers headers = request.get_response_headers(SYSTEM_LOCALE, - FORMAT_TYPES[F_JSON], **self.api_headers) LOGGER.debug('Loading provider') @@ -2499,10 +2500,7 @@ class API: 'InvalidParameterValue', msg) query_args['datetime_'] = datetime_ - - if 'f' in request.params: - # Format explicitly set using a query parameter - query_args['format_'] = format_ = request.format + query_args['format_'] = format_ properties = request.params.get('properties') if properties: diff --git a/tests/test_api.py b/tests/test_api.py index 1b2dfa2..4e522c3 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1449,8 +1449,11 @@ def test_get_collection_coverage(config, api_): rsp_headers, code, response = api_.get_collection_coverage( req, 'gdps-temperature') - assert code == HTTPStatus.OK - assert rsp_headers['Content-Type'] == 'application/prs.coverage+json' + # NOTE: This test used to assert the code to be 200 OK, + # but it requested HTML, which is not available, + # so it should be 400 Bad Request + assert code == HTTPStatus.BAD_REQUEST + assert rsp_headers['Content-Type'] == 'text/html' req = mock_request({'subset': 'Lat(5:10),Long(5:10)'}) rsp_headers, code, response = api_.get_collection_coverage( @@ -1487,6 +1490,13 @@ def test_get_collection_coverage(config, api_): assert code == HTTPStatus.OK assert isinstance(response, bytes) + req = mock_request(HTTP_ACCEPT='application/x-netcdf') + rsp_headers, code, response = api_.get_collection_coverage( + req, 'cmip5') + + assert code == HTTPStatus.OK + assert rsp_headers['Content-Type'] == 'application/x-netcdf' + # req = mock_request({ # 'subset': 'time("2006-07-01T06:00:00":"2007-07-01T06:00:00")' # })