#!/usr/bin/python3

import os
import json

try:
    from urllib import unquote
except:
    from urllib.parse import unquote

from zypp_plugin import Plugin

class SpacewalkExtraHTTPHeaders(Plugin):
    """
    Plugin to add extra HTTP Headers to Zypper requests
    """
    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']):
                    with open(headers['headers_file']) as headers_file:
                        self.http_headers = json.load(headers_file)

        except Exception as exc:
            self.answer("ERROR", {}, str(exc))
        self.answer("RESOLVEDURL", self.http_headers, unquote(headers['url']))

plugin = SpacewalkExtraHTTPHeaders()
plugin.main()
