diff --git a/ckanext/drupal_api/config.py b/ckanext/drupal_api/config.py index 5fd2857..77d4fcd 100644 --- a/ckanext/drupal_api/config.py +++ b/ckanext/drupal_api/config.py @@ -17,6 +17,10 @@ CONFIG_REQUEST_HTTP_USER = "ckanext.drupal_api.request.user" CONFIG_REQUEST_HTTP_PASS = "ckanext.drupal_api.request.pass" +CONFIG_REQUEST_HTTP_HEADER_KEY = "ckanext.drupal_api.request.header.key" +CONFIG_REQUEST_HTTP_HEADER_VALUE = "ckanext.drupal_api.request.header.value" +DEFAULT_REQUEST_HEADER_KEY = "X-CKAN-API-Key" + CONFIG_DRUPAL_API_VERSION = "ckanext.drupal_api.api_version" JSON_API = "json" CORE_API = "core" @@ -57,6 +61,11 @@ def get_http_user() -> str | None: def get_http_pass() -> str | None: return tk.config[CONFIG_REQUEST_HTTP_PASS] +def get_http_header_key() -> str: + return tk.config[CONFIG_REQUEST_HTTP_HEADER_KEY] or DEFAULT_REQUEST_HEADER_KEY + +def get_http_header_value() -> str | None: + return tk.config[CONFIG_REQUEST_HTTP_HEADER_VALUE] def get_config_options() -> dict[str, dict[str, Any]]: """Defines how we are going to render the global configuration @@ -127,4 +136,18 @@ def get_config_options() -> dict[str, dict[str, Any]]: "validators": [unicode_safe], "type": "password", }, + "http_header_key": { + "key": CONFIG_REQUEST_HTTP_HEADER_KEY, + "label": tk._("HTTP header auth key"), + "value": get_http_header_key(), + "validators": [default(DEFAULT_REQUEST_HEADER_KEY), unicode_safe], + "type": "text", + }, + "http_header_value": { + "key": CONFIG_REQUEST_HTTP_HEADER_VALUE, + "label": tk._("HTTP header auth key value"), + "value": get_http_header_value(), + "validators": [unicode_safe], + "type": "password", + }, } diff --git a/ckanext/drupal_api/config_declaration.yaml b/ckanext/drupal_api/config_declaration.yaml index 99d4e1e..398dd10 100644 --- a/ckanext/drupal_api/config_declaration.yaml +++ b/ckanext/drupal_api/config_declaration.yaml @@ -21,6 +21,12 @@ groups: - key: ckanext.drupal_api.request.pass editable: true + + - key: ckanext.drupal_api.request.header.key + editable: true + + - key: ckanext.drupal_api.request.header.value + editable: true - key: ckanext.drupal_api.api_version default: core diff --git a/ckanext/drupal_api/config_schema.yaml b/ckanext/drupal_api/config_schema.yaml index 1bf4ea8..402c2aa 100644 --- a/ckanext/drupal_api/config_schema.yaml +++ b/ckanext/drupal_api/config_schema.yaml @@ -47,3 +47,12 @@ fields: label: HTTP auth password validators: unicode_safe input_type: password + + - field_name: ckanext.drupal_api.request.header.key + label: HTTP header auth key + validators: unicode_safe + + - field_name: ckanext.drupal_api.request.header.value + label: HTTP header auth key value + validators: unicode_safe + input_type: password diff --git a/ckanext/drupal_api/logic/api.py b/ckanext/drupal_api/logic/api.py index 7965223..41b97ab 100644 --- a/ckanext/drupal_api/logic/api.py +++ b/ckanext/drupal_api/logic/api.py @@ -17,12 +17,16 @@ def make_request(url: str) -> dict: http_user = da_conf.get_http_user() http_pass = da_conf.get_http_pass() + http_header_key = da_conf.get_http_header_key() + http_header_value = da_conf.get_http_header_value() timeout = da_conf.get_request_timeout() session = requests.Session() if http_user and http_pass: session.auth = (http_user, http_pass) + if http_header_key and http_header_value: + session.headers[http_header_key] = http_header_value _add_drupal_session(session) req = session.get(url, timeout=timeout)