# Path: fnapy/compat.py
# def to_unicode(obj, encoding="utf-8"):
#     """
#     Convert ``obj`` to unicode"""
#     # unicode support
#     if isinstance(obj, str):
#         return obj
# 
#     # bytes support
#     if is_bytes(obj):
#         if hasattr(obj, "tobytes"):
#             return str(obj.tobytes(), encoding)
#         return str(obj, encoding)
# 
#     # string support
#     if isinstance(obj, basestring):
#         if hasattr(obj, "decode"):
#             # ignoring utf8 errors, as fnac send us malformed
#             # utf8 xml sometimes ...
#             return obj.decode(encoding, errors="ignore")
#         else:
#             return str(obj, encoding)
# 
#     return str(obj)
# 
# Path: fnapy/exceptions.py
# class FnapyUpdateOfferError(FnapyException):
#     """Raised when the update of an offer is not valid"""
# 
# class FnapyResponseError(FnapyException):
#     """Raised when the response is incorrect"""


import os
import re
import xmltodict
import requests
from codecs import open
from collections import OrderedDict
from xml.parsers.expat import ExpatError
from lxml import etree
from fnapy.config import *
from fnapy.compat import to_unicode
from fnapy.exceptions import FnapyUpdateOfferError, FnapyResponseError
  <key>{key}</key>
</auth>
    """.format(
        partner_id=partner_id, shop_id=shop_id, key=key
    )
    url = get_url(sandbox)
    response = post(url, "auth", xml)
    return parse_xml(response, "token")


def set_credentials(xml, sandbox=True):
    """Set the credentials in the given raw XML """
    credentials = get_credentials(sandbox)
    creds = {
        "shop_id": credentials["shop_id"],
        "partner_id": credentials["partner_id"],
        "token": get_token(sandbox),
    }

    for cred, value in creds.items():
        xml = re.sub(
            pattern='{0}="[^"]+"'.format(cred),
            repl='{0}="{1}"'.format(cred, value),
            string=xml,
            flags=0,
        )
    return xml


def post(url, service, request):

# Path: fnapy/compat.py
# def to_unicode(obj, encoding="utf-8"):
#     """
#     Convert ``obj`` to unicode"""
#     # unicode support
#     if isinstance(obj, str):
#         return obj
# 
#     # bytes support
#     if is_bytes(obj):
#         if hasattr(obj, "tobytes"):
#             return str(obj.tobytes(), encoding)
#         return str(obj, encoding)
# 
#     # string support
#     if isinstance(obj, basestring):
#         if hasattr(obj, "decode"):
#             # ignoring utf8 errors, as fnac send us malformed
#             # utf8 xml sometimes ...
#             return obj.decode(encoding, errors="ignore")
#         else:
#             return str(obj, encoding)
# 
#     return str(obj)
# 
# Path: fnapy/exceptions.py
# class FnapyUpdateOfferError(FnapyException):
#     """Raised when the update of an offer is not valid"""
# 
# class FnapyResponseError(FnapyException):
#     """Raised when the response is incorrect"""


# Path: fnapy/utils.py
import os
import re
import xmltodict
import requests
from codecs import open
from collections import OrderedDict
from xml.parsers.expat import ExpatError
from lxml import etree
from fnapy.config import *
from fnapy.compat import to_unicode
from fnapy.exceptions import FnapyUpdateOfferError, FnapyResponseError

  <key>{key}</key>
</auth>
    """.format(
        partner_id=partner_id, shop_id=shop_id, key=key
    )
    url = get_url(sandbox)
    response = post(url, "auth", xml)
    return parse_xml(response, "token")


def set_credentials(xml, sandbox=True):
    """Set the credentials in the given raw XML """
    credentials = get_credentials(sandbox)
    creds = {
        "shop_id": credentials["shop_id"],
        "partner_id": credentials["partner_id"],
        "token": get_token(sandbox),
    }

    for cred, value in creds.items():
        xml = re.sub(
            pattern='{0}="[^"]+"'.format(cred),
            repl='{0}="{1}"'.format(cred, value),
            string=xml,
            flags=0,
        )
    return xml


def post(url, service, request):

    request = to_unicode(request).encode("utf-8")