gcap.py 861 Bytes
import requests
from requests.auth import HTTPBasicAuth
from settings import conf
from common.tools.dict_to_xml import dicttoxml, escape_xml
from apps.doc import consts


class GCAP:

    def __init__(self):
        self.url = conf.GCAP_URL
        self.headers = {
            'Content-Type': 'text/plain',
        }
        self.auth = HTTPBasicAuth(conf.GCAP_AUTH_USER, conf.GCAP_AUTH_PWD)

    @staticmethod
    def dict_to_xml(comparison_res):
        comparison_xml = dicttoxml(comparison_res, root=False, attr_type=False)
        return consts.BASE_XML_TEXT.format(consts.CDATA_TEXT.format(escape_xml(comparison_xml))).encode('utf-8')

    def send(self, comparison_res):
        data = self.dict_to_xml(comparison_res)
        response = requests.post(self.url, headers=self.headers, data=data, verify=False, auth=self.auth)
        return response