Skip to content

Commit

Permalink
added requests arguments to session samgiles#105
Browse files Browse the repository at this point in the history
Added the ability to pass session wide parameters to the requests.Session object.
  • Loading branch information
IAlwaysBeCoding committed Aug 23, 2015
1 parent 4c4b7bc commit 3a54d56
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions slumber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,23 @@ class API(ResourceAttributesMixin, object):

resource_class = Resource

def __init__(self, base_url=None, auth=None, format=None, append_slash=True, session=None, serializer=None):
def __init__(self, base_url=None, auth=None, format=None, append_slash=True, session=None, serializer=None,session_params={}):
if serializer is None:
serializer = Serializer(default=format)

if session is None:
session = requests.session()


stream = session_params.pop('stream',False)
proxies = session_params.pop('proxies',{})
verify = session_params.pop('verify',True)
headers = session_params.pop('headers',{})

session.stream = stream
session.proxies = proxies
session.verify = verify
session.headers.update(headers)

if auth is not None:
session.auth = auth

Expand Down

3 comments on commit 3a54d56

@jhermann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What value does this actually provide? You can do this easily in your own code and then pass in the prepped session object. IMHO this is better solved by documentation, showing how to do it for typical use cases (e.g. the U-A header).

The above solution will always be restricted to what you provided as "magic" names there, and won't evolve with newer requests versions. Outside initialization has no such restrictions.

@IAlwaysBeCoding
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You make an interesting point. This is just a quick fix for the "lazy" ones. As you can see, my code doesn't break anything as session_params is an optional parameter that can be passed.Would you think it would be better to add this to the documentation on slumber and still leave this code on?

@jhermann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is a liability – it has to pay for its existence/maintenance in function or convenience. The above code falls into the 2nd category, and IMHO falls short, because the code the caller has to write to prepare that dict parameter is about the same as to prepare the session object (once you know via docs that this is the way). Also quoting The Zen:

There should be one-- and preferably only one --obvious way to do it.

So if you ask me docs only is the way to go here, via concrete examples from real use-cases.

Please sign in to comment.