# Path: MultiPartForm.py
# class MultiPartForm(object):
#     def __init__(self):
#         self.boundary = uuid.uuid4().hex
#         self.content_type = 'multipart/form-data; boundary={}'.format(self.boundary)
# 
#     @classmethod
#     def u(cls, s):
#         if sys.hexversion < 0x03000000 and isinstance(s, str):
#             s = s.decode('utf-8')
#         if sys.hexversion >= 0x03000000 and isinstance(s, bytes):
#             s = s.decode('utf-8')
#         return s
# 
#     def iter(self, fields, files):
#         """
#         fields is a sequence of (name, value) elements for regular form fields.
#         files is a sequence of (name, filename, file-type) elements for data to be uploaded as files
#         Yield body's chunk as bytes
#         """
#         encoder = codecs.getencoder('utf-8')
#         for (key, value) in fields.items():
#             key = self.u(key)
#             yield encoder('--{}\r\n'.format(self.boundary))
#             yield encoder(self.u('Content-Disposition: form-data; name="{}"\r\n').format(key))
#             yield encoder('\r\n')
#             if isinstance(value, int) or isinstance(value, float):
#                 value = str(value)
#             yield encoder(self.u(value))
#             yield encoder('\r\n')
#         for (key, filename, fd) in files:
#             key = self.u(key)
#             filename = self.u(filename)
#             yield encoder('--{}\r\n'.format(self.boundary))
#             yield encoder(self.u('Content-Disposition: form-data; name="{}"; filename="{}"\r\n').format(key, filename))
#             yield encoder('Content-Type: {}\r\n'.format(mimetypes.guess_type(filename)[0] or 'application/octet-stream'))
#             yield encoder('\r\n')
#             with fd:
#                 buff = fd.read()
#                 yield (buff, len(buff))
#             yield encoder('\r\n')
#         yield encoder('--{}--\r\n'.format(self.boundary))
# 
#     def encode(self, fields, files):
#         body = io.BytesIO()
#         for chunk, chunk_len in self.iter(fields, files):
#             body.write(chunk)
#         return self.content_type, body.getvalue()


import base64, json, ntpath
    import urllib2, urllib
    from urllib import request as urllib2
    from urllib import parse as urllib
from MultiPartForm import MultiPartForm
            path = path.decode('ascii', 'ignore') + "?_method=" + method

        if not isinstance(path, basestring):
            path = path.decode("ascii", "ignore")

        if (path.split(":")[0] == 'https'): #S3 based upload
            request = urllib2.Request(path)
        else:
            request = urllib2.Request(self.__baseuri + path)
            base64string = base64.encodebytes(('%s:%s' % (self.__application.token, self.__application.private_key)).encode()).decode().replace('\n', '')
            request.add_header("Authorization", "Basic %s" % base64string)

        if (method == "GET"):
            try: 
                result = urllib2.urlopen(request, None, timeout)
                return result
            except urllib2.HTTPError as e:
                return e

        else:
            if (data == None):
                data = {}

            if (file == None):
                data = urllib.urlencode(data)
                binary_data = data.encode("ascii")

                try:
                    result = urllib2.urlopen(request, binary_data, timeout)
                    return result

# Path: MultiPartForm.py
# class MultiPartForm(object):
#     def __init__(self):
#         self.boundary = uuid.uuid4().hex
#         self.content_type = 'multipart/form-data; boundary={}'.format(self.boundary)
# 
#     @classmethod
#     def u(cls, s):
#         if sys.hexversion < 0x03000000 and isinstance(s, str):
#             s = s.decode('utf-8')
#         if sys.hexversion >= 0x03000000 and isinstance(s, bytes):
#             s = s.decode('utf-8')
#         return s
# 
#     def iter(self, fields, files):
#         """
#         fields is a sequence of (name, value) elements for regular form fields.
#         files is a sequence of (name, filename, file-type) elements for data to be uploaded as files
#         Yield body's chunk as bytes
#         """
#         encoder = codecs.getencoder('utf-8')
#         for (key, value) in fields.items():
#             key = self.u(key)
#             yield encoder('--{}\r\n'.format(self.boundary))
#             yield encoder(self.u('Content-Disposition: form-data; name="{}"\r\n').format(key))
#             yield encoder('\r\n')
#             if isinstance(value, int) or isinstance(value, float):
#                 value = str(value)
#             yield encoder(self.u(value))
#             yield encoder('\r\n')
#         for (key, filename, fd) in files:
#             key = self.u(key)
#             filename = self.u(filename)
#             yield encoder('--{}\r\n'.format(self.boundary))
#             yield encoder(self.u('Content-Disposition: form-data; name="{}"; filename="{}"\r\n').format(key, filename))
#             yield encoder('Content-Type: {}\r\n'.format(mimetypes.guess_type(filename)[0] or 'application/octet-stream'))
#             yield encoder('\r\n')
#             with fd:
#                 buff = fd.read()
#                 yield (buff, len(buff))
#             yield encoder('\r\n')
#         yield encoder('--{}--\r\n'.format(self.boundary))
# 
#     def encode(self, fields, files):
#         body = io.BytesIO()
#         for chunk, chunk_len in self.iter(fields, files):
#             body.write(chunk)
#         return self.content_type, body.getvalue()


# Path: ZiggeoConnect.py
import base64, json, ntpath
    import urllib2, urllib
    from urllib import request as urllib2
    from urllib import parse as urllib
from MultiPartForm import MultiPartForm

            path = path.decode('ascii', 'ignore') + "?_method=" + method

        if not isinstance(path, basestring):
            path = path.decode("ascii", "ignore")

        if (path.split(":")[0] == 'https'): #S3 based upload
            request = urllib2.Request(path)
        else:
            request = urllib2.Request(self.__baseuri + path)
            base64string = base64.encodebytes(('%s:%s' % (self.__application.token, self.__application.private_key)).encode()).decode().replace('\n', '')
            request.add_header("Authorization", "Basic %s" % base64string)

        if (method == "GET"):
            try: 
                result = urllib2.urlopen(request, None, timeout)
                return result
            except urllib2.HTTPError as e:
                return e

        else:
            if (data == None):
                data = {}

            if (file == None):
                data = urllib.urlencode(data)
                binary_data = data.encode("ascii")

                try:
                    result = urllib2.urlopen(request, binary_data, timeout)
                    return result

                except urllib2.HTTPError as e: