fix ox.api in python3
This commit is contained in:
parent
b70dfecccc
commit
f50b02dd64
2 changed files with 37 additions and 17 deletions
25
ox/api.py
25
ox/api.py
|
|
@ -5,7 +5,7 @@ from __future__ import with_statement
|
|||
|
||||
from six.moves import http_cookiejar as cookielib
|
||||
import gzip
|
||||
from six import BytesIO
|
||||
from six import BytesIO, PY2
|
||||
from six.moves import urllib
|
||||
from types import MethodType
|
||||
|
||||
|
|
@ -45,7 +45,10 @@ class API(object):
|
|||
def _add_method(self, method, name):
|
||||
if name is None:
|
||||
name = method.func_name
|
||||
setattr(self, name, MethodType(method, self, type(self)))
|
||||
if PY2:
|
||||
setattr(self, name, MethodType(method, self, type(self)))
|
||||
else:
|
||||
setattr(self, name, MethodType(method, self))
|
||||
|
||||
def _add_action(self, action):
|
||||
def method(self, *args, **kw):
|
||||
|
|
@ -57,18 +60,26 @@ class API(object):
|
|||
return self._request(action, kw)
|
||||
if 'doc' in self._properties[action]:
|
||||
method.__doc__ = self._properties[action]['doc']
|
||||
method.func_name = str(action)
|
||||
if PY2:
|
||||
method.func_name = str(action)
|
||||
else:
|
||||
method.func_name = action
|
||||
self._add_method(method, action)
|
||||
|
||||
def _json_request(self, url, form):
|
||||
result = {}
|
||||
try:
|
||||
body = str(form)
|
||||
request = urllib.request.Request(str(url))
|
||||
request.add_header('Content-type', form.get_content_type())
|
||||
body = form.body()
|
||||
if PY2:
|
||||
if not isinstance(url, bytes):
|
||||
url = url.encode('utf-8')
|
||||
request = urllib.request.Request(url)
|
||||
request.add_data(body)
|
||||
else:
|
||||
request = urllib.request.Request(url, data=body, method='POST')
|
||||
request.add_header('Content-Type', form.get_content_type())
|
||||
request.add_header('Content-Length', str(len(body)))
|
||||
request.add_header('Accept-Encoding', 'gzip, deflate')
|
||||
request.add_data(body)
|
||||
f = self._opener.open(request)
|
||||
result = f.read()
|
||||
if f.headers.get('content-encoding', None) == 'gzip':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue