#!/usr/bin/python3
# pylint: disable=missing-module-docstring,invalid-name

import os
import json

try:
    from urllib import unquote
# pylint: disable-next=bare-except
except:
    from urllib.parse import unquote

from zypp_plugin import Plugin


class SpacewalkExtraHTTPHeaders(Plugin):
    """
    Plugin to add extra HTTP Headers to Zypper requests
    """

    # pylint: disable-next=unused-argument
    def RESOLVEURL(self, headers, body):
        """
        Resolve URL.

        :returns: None
        """
        try:
            self.http_headers = {}
            if "headers_file" in headers:
                if os.path.isfile(headers["headers_file"]):
                    # pylint: disable-next=unspecified-encoding
                    with open(headers["headers_file"]) as headers_file:
                        self.http_headers = json.load(headers_file)

        # pylint: disable-next=broad-exception-caught
        except Exception as exc:
            self.answer("ERROR", {}, str(exc))
        self.answer("RESOLVEDURL", self.http_headers, unquote(headers["url"]))


plugin = SpacewalkExtraHTTPHeaders()
plugin.main()
