Skip to content

Allow passing custom default headers to server (closes #91) #92

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

Merged
merged 2 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion backslash/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

class API(object):

def __init__(self, client, url, runtoken, timeout_seconds=60):
def __init__(self, client, url, runtoken, timeout_seconds=60, headers=None):
super(API, self).__init__()
self.client = client
self.url = URL(url)
Expand All @@ -56,6 +56,8 @@ def __init__(self, client, url, runtoken, timeout_seconds=60):
'X-Backslash-run-token': self.runtoken,
'X-Backslash-client-version': BACKSLASH_CLIENT_VERSION,
})
if headers is not None:
self.session.headers.update(headers)
self.call = CallProxy(self)
self._cached_info = None
self._timeout = timeout_seconds
Expand Down
4 changes: 2 additions & 2 deletions backslash/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

class Backslash(object):

def __init__(self, url, runtoken):
def __init__(self, url, runtoken, headers=None):
super(Backslash, self).__init__()
if not url.startswith('http'):
url = 'http://{0}'.format(url)
self._url = URL(url)
self.api = API(self, url, runtoken)
self.api = API(self, url, runtoken, headers=headers)

@property
def url(self):
Expand Down
10 changes: 9 additions & 1 deletion backslash/contrib/slash_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,15 @@ def get_default_config(self):
def activate(self):
if self._runtoken is None:
self._runtoken = self._ensure_run_token()
self.client = BackslashClient(URL(self._get_backslash_url()), self._runtoken)
self.client = BackslashClient(
URL(self._get_backslash_url()),
self._runtoken, headers=self._get_default_headers())

def _get_default_headers(self):
"""Override this method to control the headers sent to the Backslash server
on each reequest
"""
return None

def deactivate(self):
if self._keepalive_thread is not None:
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ testing =
slash>=1.5.0
Flask
Flask-Loopback
pylint~=1.7.0
pylint~=1.9.0; python_version<'3.0'
pylint~=2.2.0; python_version>='3.0'
pytest; python_version < '3.0'
pytest>4.0; python_version >= '3.0'
pytest-cov<2.6; python_version < '3.0'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_backslash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# py.test style tests here

def test_import():
import backslash # pylint: disable=trailing-newlines, unused-variable,unused-import
import backslash # pylint: disable=trailing-newlines, unused-variable, unused-import