Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra_data parameter + Add request_url property in UnirestResponse #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 64 additions & 13 deletions unirest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
register_openers()


def __request(method, url, params={}, headers={}, auth=None, callback=None):
def __request(method, url, params={}, headers={}, auth=None, callback=None, extra_data=None):

# Encode URL
url_parts = url.split("\\?")
Expand Down Expand Up @@ -89,17 +89,27 @@ def __request(method, url, params={}, headers={}, auth=None, callback=None):
res = urlfetch.fetch(url, payload=data, headers=headers, method=method, deadline=_timeout)
_unirestResponse = UnirestResponse(res.status_code,
res.headers,
res.content)
res.content,
url,
extra_data)
else:
req = urllib2.Request(url, data, headers)
req.get_method = lambda: method

try:
response = urllib2.urlopen(req, timeout=_timeout)
_unirestResponse = UnirestResponse(response.code, response.headers, response.read())
_unirestResponse = UnirestResponse(response.code,
response.headers,
response.read(),
url,
extra_data)
except urllib2.HTTPError, e:
response = e
_unirestResponse = UnirestResponse(response.code, response.headers, response.read())
_unirestResponse = UnirestResponse(response.code,
response.headers,
response.read(),
url,
extra_data)
except urllib2.URLError, e:
_unirestResponse = UnirestResponse(0, {}, str(e.reason))

Expand All @@ -120,6 +130,7 @@ def __request(method, url, params={}, headers={}, auth=None, callback=None):
CALLBACK_KEY = 'callback'
PARAMS_KEY = 'params'
AUTH_KEY = 'auth'
EXTRA_DATA_KEY = 'extra_data'


def get_parameters(kwargs):
Expand All @@ -137,24 +148,53 @@ def get(url, **kwargs):
else:
url += "&"
url += utils.dict2query(dict((k, v) for k, v in params.iteritems() if v is not None)) # Removing None values/encode unicode objects

return __dorequest("GET", url, {}, kwargs.get(HEADERS_KEY, {}), kwargs.get(AUTH_KEY, None), kwargs.get(CALLBACK_KEY, None))
return __dorequest("GET",
url,
{},
kwargs.get(HEADERS_KEY, {}),
kwargs.get(AUTH_KEY, None),
kwargs.get(CALLBACK_KEY, None),
kwargs.get(EXTRA_DATA_KEY, None))


def post(url, **kwargs):
return __dorequest("POST", url, get_parameters(kwargs), kwargs.get(HEADERS_KEY, {}), kwargs.get(AUTH_KEY, None), kwargs.get(CALLBACK_KEY, None))
return __dorequest("POST",
url,
get_parameters(kwargs),
kwargs.get(HEADERS_KEY, {}),
kwargs.get(AUTH_KEY, None),
kwargs.get(CALLBACK_KEY, None),
kwargs.get(EXTRA_DATA_KEY, None))


def put(url, **kwargs):
return __dorequest("PUT", url, get_parameters(kwargs), kwargs.get(HEADERS_KEY, {}), kwargs.get(AUTH_KEY, None), kwargs.get(CALLBACK_KEY, None))
return __dorequest("PUT",
url,
get_parameters(kwargs),
kwargs.get(HEADERS_KEY, {}),
kwargs.get(AUTH_KEY, None),
kwargs.get(CALLBACK_KEY, None),
kwargs.get(EXTRA_DATA_KEY, None))


def delete(url, **kwargs):
return __dorequest("DELETE", url, get_parameters(kwargs), kwargs.get(HEADERS_KEY, {}), kwargs.get(AUTH_KEY, None), kwargs.get(CALLBACK_KEY, None))
return __dorequest("DELETE",
url,
get_parameters(kwargs),
kwargs.get(HEADERS_KEY, {}),
kwargs.get(AUTH_KEY, None),
kwargs.get(CALLBACK_KEY, None),
kwargs.get(EXTRA_DATA_KEY, None))


def patch(url, **kwargs):
return __dorequest("PATCH", url, get_parameters(kwargs), kwargs.get(HEADERS_KEY, {}), kwargs.get(AUTH_KEY, None), kwargs.get(CALLBACK_KEY, None))
return __dorequest("PATCH",
url,
get_parameters(kwargs),
kwargs.get(HEADERS_KEY, {}),
kwargs.get(AUTH_KEY, None),
kwargs.get(CALLBACK_KEY, None),
kwargs.get(EXTRA_DATA_KEY, None))


def default_header(name, value):
Expand All @@ -170,7 +210,7 @@ def timeout(seconds):
_timeout = seconds


def __dorequest(method, url, params, headers, auth, callback=None):
def __dorequest(method, url, params, headers, auth, callback=None, extra_data=None):
if callback is None:
return __request(method, url, params, headers, auth)
else:
Expand All @@ -180,15 +220,18 @@ def __dorequest(method, url, params, headers, auth, callback=None):
params,
headers,
auth,
callback))
callback,
extra_data))
thread.start()
return thread


class UnirestResponse(object):
def __init__(self, code, headers, body):
def __init__(self, code, headers, body, request_url, extra_data=None):
self._code = code
self._headers = headers
self._extra_data = extra_data
self._request_url = request_url

if headers.get("Content-Encoding") == 'gzip':
buf = StringIO(body)
Expand Down Expand Up @@ -219,3 +262,11 @@ def raw_body(self):
@property
def headers(self):
return self._headers

@property
def extra_data(self):
return self._extra_data

@property
def request_url(self):
return self._request_url