Skip to content

Commit f4d3e36

Browse files
author
Mike Kistler
authored
Merge pull request #270 from watson-developer-cloud/mdk/default-headers
Add support for default_headers
2 parents c12c9aa + 323eef7 commit f4d3e36

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

test/test_alchemy_language_v1.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ def test_api_key(self):
1010
default_url = 'https://gateway-a.watsonplatform.net/calls'
1111
inited = watson_developer_cloud.AlchemyLanguageV1(url=default_url, api_key='boguskey',
1212
x_watson_learning_opt_out=True)
13-
assert inited.x_watson_learning_opt_out
1413
assert inited.api_key == 'boguskey'
1514
assert inited.url == default_url
1615
inited.set_url(url="http://google.com")

test/test_conversation_v1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ def test_message():
719719

720720
conversation = watson_developer_cloud.ConversationV1(
721721
username="username", password="password", version='2016-09-20')
722+
conversation.set_default_headers({'x-watson-learning-opt-out': "true"})
722723

723724
workspace_id = 'f8fdbc65-e0bd-4e43-b9f8-2975a366d4ec'
724725
message_url = '%s/v1/workspaces/%s/message' % (base_url, workspace_id)
@@ -752,6 +753,8 @@ def test_message():
752753

753754
assert message is not None
754755
assert responses.calls[0].request.url == message_url1
756+
assert 'x-watson-learning-opt-out' in responses.calls[0].request.headers
757+
assert responses.calls[0].request.headers['x-watson-learning-opt-out'] == 'true'
755758
assert responses.calls[0].response.text == json.dumps(message_response)
756759

757760
# test context

watson_developer_cloud/watson_developer_cloud_service.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ def __init__(self, vcap_services_name, url, username=None, password=None,
101101
self.api_key = None
102102
self.username = None
103103
self.password = None
104-
self.x_watson_learning_opt_out = x_watson_learning_opt_out
104+
self.default_headers = None
105+
106+
if x_watson_learning_opt_out:
107+
self.default_headers = {'x-watson-learning-opt-out': 'true'}
105108

106109
if api_key is not None:
107110
if username is not None or password is not None:
@@ -153,6 +156,16 @@ def set_api_key(self, api_key):
153156
def set_url(self, url):
154157
self.url = url
155158

159+
def set_default_headers(self, headers):
160+
"""
161+
Set http headers to be sent in every request.
162+
:param headers: A dictionary of header names and values
163+
"""
164+
if isinstance(headers, dict):
165+
self.default_headers = headers
166+
else:
167+
raise WatsonException("headers parameter must be a dictionary")
168+
156169
# Could make this compute the label_id based on the variable name of the
157170
# dictionary passed in (using **kwargs), but
158171
# this might be confusing to understand.
@@ -262,6 +275,8 @@ def request(self, method, url, accept_json=False, headers=None,
262275

263276
headers = CaseInsensitiveDict(
264277
{'user-agent': 'watson-developer-cloud-python-' + __version__})
278+
if self.default_headers is not None:
279+
headers.update(self.default_headers)
265280
if accept_json:
266281
headers['accept'] = 'application/json'
267282
headers.update(input_headers)
@@ -293,9 +308,6 @@ def request(self, method, url, accept_json=False, headers=None,
293308
else:
294309
params['api_key'] = self.api_key
295310

296-
if self.x_watson_learning_opt_out:
297-
headers['x-watson-learning-opt-out'] = 'true'
298-
299311
response = requests.request(method=method, url=full_url,
300312
cookies=self.jar, auth=auth,
301313
headers=headers,

0 commit comments

Comments
 (0)