"""Util for owsrequests.""" from http import HTTPStatus from flask import request as flask_request import requests from collaborator.constants import header from collaborator.utils.error import OwsError def unwrap_data_or_error(res: requests.Response, error_code: str) -> dict: """Raise OwsError on a non-2xx response, else return the response data.""" if res.status_code not in [HTTPStatus.OK, HTTPStatus.CREATED]: try: errors_data = res.json() except Exception: errors_data = { "code": error_code, "message": res.content.decode(), } raise OwsError( code=errors_data.get("code", error_code), message=errors_data.get("message", ""), status=res.status_code, ) return res.json() if res.text else {} def get_default_ows_headers(): """Get default headers from the current Flask request for OWS service calls. Returns: dict: Dictionary containing the standard Orchard headers from the current request. """ return { header.ORCHARD_IDENTITY_ID: flask_request.headers[header.ORCHARD_IDENTITY_ID], header.ORCHARD_PROFILE_ID: flask_request.headers[header.ORCHARD_PROFILE_ID], header.ORCHARD_PROFILE_TYPE: flask_request.headers[header.ORCHARD_PROFILE_TYPE], }