from __future__ import absolute_import

import jsonpickle
import requests

from .Config import Config
from .Utilities import RemoveFromJson


class Request (object):

    def __getstate__(self):
        body = RemoveFromJson(self.__dict__.copy())
        return body

    def __init__(self, category, proxy, endpoint, method):
        self.queryParams = {}
        self.url = ("https://%s/%s/sp2/%s/v%s/%s" %
                    (Config.baseEndpoint, category,
                     proxy, Config.version, endpoint))
        self.method = method

    def send(self):
        if (Config.doNotSend):
            body = jsonpickle.encode(self, unpicklable=False)
            if (Config.printRequest):
                print(body)
            return body
        else:
            queryParamString = ''
            if (self.queryParams):
                queryParamString = "?"
                for key, value in self.queryParams.iteritems():
                    queryParamString += key + "=" + value + "&"
                queryParamString = queryParamString[:-1]
            url = self.url + queryParamString
            body = jsonpickle.encode(self, unpicklable=False)
            if (Config.printRequest):
                print (body)
            try:
                headers = {'Authorization':
                           'VANTIV license='+"\""+Config.license+"\"",
                           'Content-Type': 'application/json'}
                response = requests.get(url,
                                        proxies=Config.proxy,
                                        data=body,
                                        headers=headers)
                code = response.status_code
                import ipdb; ipdb.set_trace() # DEBUG
                contents = response.json
            except Exception as error:
                contents = error.read()
            if (Config.printResponse):
                print(contents)

            return contents

@@ -1,143 +1,230 @@
+%0A
 from 
-__future__ import absolute_import%0A%0Aimport jsonpickle%0Aimport requests%0A%0Afrom .Config import Config%0Afrom .Utilities import RemoveFromJson
+.Config import Config%0Afrom .Utilities import RemoveFromJson%0Aimport jsonpickle%0Aimport ssl%0Aimport sys%0A%0Aif sys.version_info.major %3C 3:%0A    import urllib2 as urllib%0Aelse:%0A    import urllib.request, urllib.error, urllib.parse
 %0A%0A%0Ac
@@ -246,17 +246,16 @@
 bject):%0A
-%0A
     def 
@@ -436,16 +436,17 @@
 ms = %7B%7D%0A
+%0A
         
@@ -530,16 +530,37 @@
 ndpoint,
+%0A                    
  categor
@@ -593,24 +593,66 @@
 oxy,
- Config.version,
+%0A                     Config.version,%0A                    
  end
@@ -1069,12 +1069,8 @@
 ams.
-iter
 item
@@ -1386,40 +1386,480 @@
     
-try:%0A                headers = %7B
+if(Config.proxy):%0A                proxy = urllib.request.ProxyHandler(Config.proxy)%0A                auth = urllib.request.HTTPBasicAuthHandler()%0A                opener = urllib.request.build_opener(proxy,%0A                                                     auth,%0A                                                     urllib.request.HTTPHandler)%0A                urllib.request.install_opener(opener)%0A            req = urllib.request.Request(url)%0A            req.add_header(
 'Aut
@@ -1869,17 +1869,17 @@
 ization'
-:
+,
 %0A       
@@ -1919,14 +1919,18 @@
 se='
-+
+ + 
 %22%5C%22%22
-+
+ + 
 Conf
@@ -1943,42 +1943,44 @@
 ense
-+
+ + 
 %22%5C%22%22
-,%0A                           
+)%0A            req.add_header(
 'Con
@@ -1989,17 +1989,17 @@
 nt-Type'
-:
+,
  'applic
@@ -2009,17 +2009,17 @@
 on/json'
-%7D
+)
 %0A       
@@ -2027,42 +2027,51 @@
     
-    response = requests.get(url,%0A 
+context = ssl._create_unverified_context()%0A
     
@@ -2074,35 +2074,29 @@
             
-           
+try:%0A
             
@@ -2103,28 +2103,41 @@
     
-proxies=Config.proxy
+resp = urllib.request.urlopen(req
 ,%0A  
@@ -2178,17 +2178,34 @@
     
-data=body
+      body.encode('UTF-8')
 ,%0A  
@@ -2246,122 +2246,68 @@
     
-headers=headers)%0A                code = response.status_code%0A                import ipdb; ipdb.set_trace() # DEBUG
+      context=context)%0A                code = resp.getcode()
 %0A   
@@ -2338,17 +2338,15 @@
 resp
-onse.json
+.read()
 %0A   
@@ -2365,17 +2365,30 @@
 ept 
-Exception
+urllib.error.HTTPError
  as 

Change back to urllib/urllib2