@@ -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