hadapi, util: Re-locate body parsing
We'll need it in the server for requesting POST/PUT bodies too.
This commit is contained in:
parent
6b5c4e8b6a
commit
6eb99f2653
@ -10,13 +10,13 @@ from tornado.gen import coroutine, Return, sleep
|
|||||||
from tornado.locks import Semaphore
|
from tornado.locks import Semaphore
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
from ..util import decode_body
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib import parse as urlparse
|
from urllib import parse as urlparse
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import urlparse
|
import urlparse
|
||||||
|
|
||||||
from cgi import parse_header
|
|
||||||
|
|
||||||
|
|
||||||
class UserSortBy(Enum):
|
class UserSortBy(Enum):
|
||||||
influence='influence'
|
influence='influence'
|
||||||
@ -120,12 +120,8 @@ class HackadayAPI(object):
|
|||||||
"""
|
"""
|
||||||
Decode a given reponse body.
|
Decode a given reponse body.
|
||||||
"""
|
"""
|
||||||
# Ideally, encoding should be in the content type
|
return decode_body(response.headers['Content-Type'], response.body,
|
||||||
(ct, ctopts) = parse_header(response.headers['Content-Type'])
|
default_encoding)
|
||||||
encoding = ctopts.get('charset', default_encoding)
|
|
||||||
|
|
||||||
# Return the decoded payload along with the content-type.
|
|
||||||
return (ct, ctopts, response.body.decode(encoding))
|
|
||||||
|
|
||||||
@coroutine
|
@coroutine
|
||||||
def _api_call(self, uri, query=None, token=None, api_key=True, **kwargs):
|
def _api_call(self, uri, query=None, token=None, api_key=True, **kwargs):
|
||||||
|
12
hadsh/util.py
Normal file
12
hadsh/util.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from cgi import parse_header
|
||||||
|
|
||||||
|
def decode_body(content_type, body_data, default_encoding='UTF-8'):
|
||||||
|
"""
|
||||||
|
Decode a given reponse body.
|
||||||
|
"""
|
||||||
|
# Ideally, encoding should be in the content type
|
||||||
|
(ct, ctopts) = parse_header(content_type)
|
||||||
|
encoding = ctopts.get('charset', default_encoding)
|
||||||
|
|
||||||
|
# Return the decoded payload along with the content-type.
|
||||||
|
return (ct, ctopts, body_data.decode(encoding))
|
Reference in New Issue
Block a user