2626import os
2727from socket import gaierror
2828import requests
29+ import re
2930
3031_BINDING_VERSION = '1.1.1'
3132_GZIP_BYTEARRAY = bytearray ([0x1F , 0x8b , 0x08 ])
@@ -366,6 +367,17 @@ def info(self):
366367 identifying data."""
367368 url = self .service_url + "info"
368369 headers = {'Accept' : 'application/json' , 'X-RosetteAPI-Binding' : 'python' , 'X-RosetteAPI-Binding-Version' : _BINDING_VERSION }
370+
371+ customHeaders = self .api .getCustomHeaders ()
372+ pattern = re .compile ('^X-RosetteAPI-' )
373+ if customHeaders is not None :
374+ for key in customHeaders .keys ():
375+ if pattern .match (key ) is not None :
376+ headers [key ] = customHeaders [key ]
377+ else :
378+ raise RosetteException ("badHeader" , "Custom header name must begin with \" X-RosetteAPI-\" " , key )
379+ self .api .clearCustomHeaders ()
380+
369381 if self .debug :
370382 headers ['X-RosetteAPI-Devel' ] = 'true'
371383 self .logger .info ('info: ' + url )
@@ -382,6 +394,17 @@ def ping(self):
382394
383395 url = self .service_url + 'ping'
384396 headers = {'Accept' : 'application/json' , 'X-RosetteAPI-Binding' : 'python' , 'X-RosetteAPI-Binding-Version' : _BINDING_VERSION }
397+
398+ customHeaders = self .api .getCustomHeaders ()
399+ pattern = re .compile ('^X-RosetteAPI-' )
400+ if customHeaders is not None :
401+ for key in customHeaders .keys ():
402+ if pattern .match (key ) is not None :
403+ headers [key ] = customHeaders [key ]
404+ else :
405+ raise RosetteException ("badHeader" , "Custom header name must begin with \" X-RosetteAPI-\" " , key )
406+ self .api .clearCustomHeaders ()
407+
385408 if self .debug :
386409 headers ['X-RosetteAPI-Devel' ] = 'true'
387410 self .logger .info ('Ping: ' + url )
@@ -426,9 +449,21 @@ def call(self, parameters):
426449 params_to_serialize = parameters .serialize (self .api .options )
427450 headers = {}
428451 if self .user_key is not None :
452+
453+ customHeaders = self .api .getCustomHeaders ()
454+ pattern = re .compile ('^X-RosetteAPI-' )
455+ if customHeaders is not None :
456+ for key in customHeaders .keys ():
457+ if pattern .match (key ) is not None :
458+ headers [key ] = customHeaders [key ]
459+ else :
460+ raise RosetteException ("badHeader" , "Custom header name must begin with \" X-RosetteAPI-\" " , key )
461+ self .api .clearCustomHeaders ()
462+
429463 headers ["X-RosetteAPI-Key" ] = self .user_key
430464 headers ["X-RosetteAPI-Binding" ] = "python"
431465 headers ["X-RosetteAPI-Binding-Version" ] = _BINDING_VERSION
466+
432467 if self .useMultipart :
433468 params = dict (
434469 (key ,
@@ -502,6 +537,7 @@ def __init__(
502537 self .connection_refresh_duration = refresh_duration
503538 self .http_connection = None
504539 self .options = {}
540+ self .customHeaders = {}
505541
506542 def _connect (self , parsedUrl ):
507543 """ Simple connection method
@@ -638,6 +674,30 @@ def clearOptions(self):
638674 """
639675 self .options .clear ()
640676
677+ def setCustomHeaders (self , name , value ):
678+ """
679+ Sets custom headers
680+
681+ @param headers: array of custom headers to be set
682+ """
683+ if value is None :
684+ self .customHeaders .pop (name , None )
685+ else :
686+ self .customHeaders [name ] = value
687+
688+ def getCustomHeaders (self ):
689+ """
690+ Get custom headers
691+ """
692+ return self .customHeaders
693+
694+ def clearCustomHeaders (self ):
695+ """
696+ Clears custom headers
697+ """
698+
699+ self .customHeaders .clear ()
700+
641701 def ping (self ):
642702 """
643703 Create a ping L{EndpointCaller} for the server and ping it.
0 commit comments