fix ox.api

This commit is contained in:
j 2014-10-04 13:37:33 +02:00
parent 83cf8eea53
commit b70dfecccc

View file

@ -5,7 +5,7 @@ from __future__ import with_statement
from six.moves import http_cookiejar as cookielib from six.moves import http_cookiejar as cookielib
import gzip import gzip
from six import StringIO from six import BytesIO
from six.moves import urllib from six.moves import urllib
from types import MethodType from types import MethodType
@ -30,7 +30,7 @@ class API(object):
else: else:
self._cj = cookielib.CookieJar() self._cj = cookielib.CookieJar()
self._opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self._cj), self._opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self._cj),
urllib.HTTPHandler(debuglevel=self.debuglevel)) urllib.request.HTTPHandler(debuglevel=self.debuglevel))
self._opener.addheaders = [ self._opener.addheaders = [
('User-Agent', '%s/%s' % (self.__name__, self.__version__)) ('User-Agent', '%s/%s' % (self.__name__, self.__version__))
] ]
@ -64,7 +64,7 @@ class API(object):
result = {} result = {}
try: try:
body = str(form) body = str(form)
request = urllib.reuqest.Request(str(url)) request = urllib.request.Request(str(url))
request.add_header('Content-type', form.get_content_type()) request.add_header('Content-type', form.get_content_type())
request.add_header('Content-Length', str(len(body))) request.add_header('Content-Length', str(len(body)))
request.add_header('Accept-Encoding', 'gzip, deflate') request.add_header('Accept-Encoding', 'gzip, deflate')
@ -72,7 +72,7 @@ class API(object):
f = self._opener.open(request) f = self._opener.open(request)
result = f.read() result = f.read()
if f.headers.get('content-encoding', None) == 'gzip': if f.headers.get('content-encoding', None) == 'gzip':
result = gzip.GzipFile(fileobj=StringIO.StringIO(result)).read() result = gzip.GzipFile(fileobj=BytesIO(result)).read()
result = result.decode('utf-8') result = result.decode('utf-8')
return json.loads(result) return json.loads(result)
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e: