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 possibility to add header elements to any request #135

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
35 changes: 20 additions & 15 deletions slumber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __call__(self, id=None, format=None, url_override=None):

return self._get_resource(**kwargs)

def _request(self, method, data=None, files=None, params=None):
def _request(self, method, data=None, files=None, extra_headers=None, params=None):
serializer = self._store["serializer"]
url = self.url()

Expand All @@ -94,6 +94,11 @@ def _request(self, method, data=None, files=None, params=None):
if data is not None:
data = serializer.dumps(data)

if extra_headers is not None:
for key in extra_headers:
value = extra_headers[key]
headers[key] = value

resp = self._store["session"].request(method, url, data=data, params=params, files=files, headers=headers)

if 400 <= resp.status_code <= 499:
Expand Down Expand Up @@ -151,32 +156,32 @@ def url(self):
return url

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

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

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

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

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

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

def delete(self, **kwargs):
resp = self._request("DELETE", params=kwargs)
def delete(self, headers=None, **kwargs):
resp = self._request("DELETE", extra_headers=headers, params=kwargs)
if 200 <= resp.status_code <= 299:
if resp.status_code == 204:
return True
Expand Down