# Path: src/sii/lib/ptcl/helpers.py
# def with_retry(func, count=RETRIES_MAX, ival=RETRIES_SLEEP):
#     retries = 0
#     while retries < count:
#         try:
#             return func()
#         except Exception as exc:
#             code, msg = exc.args[0]
# 
#             if code == 503:
#                 retries += 1
#                 time.sleep(ival)
#                 continue
#             else:
#                 raise


import re
import xmlsec
from suds.client import Client
from lxml        import etree
from .helpers import with_retry

        # Add the <ds:Reference/> node to the signature template.
        ref = xmlsec.template.add_reference(signode, digest_method=xmlsec.Transform.SHA1)

        # Add the enveloped transform descriptor.
        xmlsec.template.add_transform(ref, transform=xmlsec.Transform.ENVELOPED)

        # Add Key Value Info and x509 Data
        key_info = xmlsec.template.ensure_key_info(signode)
        xmlsec.template.add_key_value(key_info)
        xmlsec.template.add_x509_data(key_info)

        # Load Key and Certificate
        key = xmlsec.Key.from_file(self.key_path, xmlsec.KeyFormat.PEM)
        key.load_cert_from_file(self.cert_path,   xmlsec.KeyFormat.PEM)

        # Create Crypto Context and sign Signature Node
        ctx = xmlsec.SignatureContext()
        ctx.key = key
        ctx.sign(signode)

        return root

    def _fetch_token(self, request_etree):
        request_xml = etree.tostring(
            request_etree,
            method='xml',
            encoding='unicode'
        )


# Path: src/sii/lib/ptcl/helpers.py
# def with_retry(func, count=RETRIES_MAX, ival=RETRIES_SLEEP):
#     retries = 0
#     while retries < count:
#         try:
#             return func()
#         except Exception as exc:
#             code, msg = exc.args[0]
# 
#             if code == 503:
#                 retries += 1
#                 time.sleep(ival)
#                 continue
#             else:
#                 raise


# Path: src/sii/lib/ptcl/Token.py
import re
import xmlsec
from suds.client import Client
from lxml        import etree
from .helpers import with_retry


        # Add the <ds:Reference/> node to the signature template.
        ref = xmlsec.template.add_reference(signode, digest_method=xmlsec.Transform.SHA1)

        # Add the enveloped transform descriptor.
        xmlsec.template.add_transform(ref, transform=xmlsec.Transform.ENVELOPED)

        # Add Key Value Info and x509 Data
        key_info = xmlsec.template.ensure_key_info(signode)
        xmlsec.template.add_key_value(key_info)
        xmlsec.template.add_x509_data(key_info)

        # Load Key and Certificate
        key = xmlsec.Key.from_file(self.key_path, xmlsec.KeyFormat.PEM)
        key.load_cert_from_file(self.cert_path,   xmlsec.KeyFormat.PEM)

        # Create Crypto Context and sign Signature Node
        ctx = xmlsec.SignatureContext()
        ctx.key = key
        ctx.sign(signode)

        return root

    def _fetch_token(self, request_etree):
        request_xml = etree.tostring(
            request_etree,
            method='xml',
            encoding='unicode'
        )


        return with_retry(lambda: self.sii_soap.service.getToken(request_xml))