Skip to content

Commit

Permalink
Merge remote-tracking branch 'samgiles/master' into separate_serializers
Browse files Browse the repository at this point in the history
 * Add input_serializer param to new test
  • Loading branch information
onlynone committed Jun 23, 2017
2 parents a5bb1f3 + af0f9ef commit 0cb9024
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ language: python
python:
- 2.6
- 2.7
- 3.2
- 3.3
- 3.4
- 3.6
- pypy
install:
- pip install -r requirements.txt
Expand Down
9 changes: 0 additions & 9 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ and processing requests.
options
howitworks

Getting Help
============

There are two primary ways of getting help. I have an IRC channel
(`#slumber on irc.freenode.net`_) to get help, want to bounce idea or
generally shoot the breeze.

.. _#slumber on irc.freenode.net: irc://irc.freenode.net/slumber

QuickStart
==========

Expand Down
17 changes: 17 additions & 0 deletions docs/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,20 @@ Slashes
Slumber assumes by default that all urls should end with a slash. If you do not
want this behavior you can control it via the append_slash option which can be
set by passing append_slash to the ``slumber.API`` kwargs.

Raw Responses
=================

By default Slumber will return a decoded representation of the response body,
if one existed. If the `API` is constructed with `raw=True`, then instead
of returning a decoded representation, a tuple will be returned, where the
first item is the actual `requests.Response` object, and the second is the
decoded representation::

api = slumber.API("https://example.com/path/to/api", raw=True)
(response, decoded) = api.subresource.get()

Alternatively, this can be done on a per resource basis using the `as_raw`
method::

(response, decoded) = api.subresource.as_raw().get()
22 changes: 22 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@
author_email = "[email protected]",
maintainer = "Samuel Giles",
maintainer_email = "[email protected]",
classifiers=[
# See: https://pypi.python.org/pypi?:action=list_classifiers
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
# List of python versions and their support status:
# https://en.wikipedia.org/wiki/CPython#Version_history
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Topic :: Internet :: WWW/HTTP :: HTTP Servers',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Software Development :: Testing',
],
packages = ["slumber"],
zip_safe = False,
install_requires = install_requires,
Expand Down
57 changes: 33 additions & 24 deletions slumber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def _request(self, method, data=None, files=None, params=None):
headers = {"accept": output_type}

if not files:
headers["content-type"] = input_type
if data is not None:
headers["content-type"] = input_type
data = input_serializer.dumps(data)

resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers)
Expand Down Expand Up @@ -141,45 +141,42 @@ def _try_to_serialize_response(self, resp):
return resp.content

def _process_response(self, resp):
# TODO: something to expose headers and status

if 200 <= resp.status_code <= 299:
return self._try_to_serialize_response(resp)
decoded = self._try_to_serialize_response(resp)
else:
return # @@@ We should probably do some sort of error here? (Is this even possible?)
# @@@ We should probably do some sort of error here? (Is this even possible?)
decoded = None

def url(self):
url = self._store["base_url"]
if self._store["raw"]:
return (resp, decoded)

if self._store["append_slash"] and not url.endswith("/"):
url = url + "/"
return decoded

return url
def _do_verb_request(self, verb, data=None, files=None, params=None):
resp = self._request(verb, data=data, files=files, params=params)
return self._process_response(resp)

def as_raw(self):
self._store["raw"] = True
return self

# TODO: refactor these methods - lots of commonality
def get(self, **kwargs):
resp = self._request("GET", params=kwargs)
return self._process_response(resp)
return self._do_verb_request("GET", params=kwargs)

def options(self, **kwargs):
resp = self._request("OPTIONS", params=kwargs)
return self._process_response(resp)
return self._do_verb_request("OPTIONS", params=kwargs)

def head(self, **kwargs):
resp = self._request("HEAD", params=kwargs)
return self._process_response(resp)
return self._do_verb_request("HEAD", params=kwargs)

def post(self, data=None, files=None, **kwargs):
resp = self._request("POST", data=data, files=files, params=kwargs)
return self._process_response(resp)
return self._do_verb_request("POST", data=data, files=files, params=kwargs)

def patch(self, data=None, files=None, **kwargs):
resp = self._request("PATCH", data=data, files=files, params=kwargs)
return self._process_response(resp)
return self._do_verb_request("PATCH", data=data, files=files, params=kwargs)

def put(self, data=None, files=None, **kwargs):
resp = self._request("PUT", data=data, files=files, params=kwargs)
return self._process_response(resp)
return self._do_verb_request("PUT", data=data, files=files, params=kwargs)

def delete(self, **kwargs):
resp = self._request("DELETE", params=kwargs)
Expand All @@ -191,6 +188,15 @@ def delete(self, **kwargs):
else:
return False

def url(self):
url = self._store["base_url"]

if self._store["append_slash"] and not url.endswith("/"):
url = url + "/"

return url


def _get_resource(self, **kwargs):
return self.__class__(**kwargs)

Expand All @@ -199,7 +205,9 @@ class API(ResourceAttributesMixin, object):

resource_class = Resource

def __init__(self, base_url=None, auth=None, format=None, input_format=None, append_slash=True, session=None, serializer=None, input_serializer=None):
def __init__(self, base_url=None, auth=None,
format=None, input_format=None, append_slash=True,
session=None, serializer=None, input_serializer=None, raw=False):
if serializer is None:
serializer = Serializer(default=format)

Expand All @@ -220,6 +228,7 @@ def __init__(self, base_url=None, auth=None, format=None, input_format=None, app
"session": session,
"serializer": serializer,
"input_serializer": input_serializer,
"raw": raw,
}

# Do some Checks for Required Values
Expand Down
Loading

0 comments on commit 0cb9024

Please sign in to comment.