Automatically adding headers to python Requests requests -


i trying create rest api client talking 1 of our services. every request needs include authorisation header compromised of epoch times, request verb, data, path etc.

i'm trying use python requests module seamlessly possible, unsure best way "inject" header every request.

there seems concept of "hooks" in requests, there "response" hook.

i considering extending session object , overriding "send" method, adding header , passing super (session.send) method.

my python isn't fantastic when comes oop , inheritance, have tried

 class mysession(session):     def __init__(self, access_id=none, access_key=none):         self.access_id = access_id         self.access_key = access_key         super(mysession, self).__init__()      def send(self, request, **kwargs):         method = request.method         path = urlparse(request.url).path          request.headers['authorization'] = self.__create_security_header(method, path)         request.headers['content-type'] = "application/json"          return session.send(self, request, **kwargs) 

i guess don't need override send method, since have overriden __init__.

class mysession(session):      def __init__(self, access_id=none, access_key=none):         super(mysession, self).__init__()         self.access_id, self.access_key = access_id, access_key          # provided __create_security_header method defined         self.headers['authorization'] = self.__create_security_header(method, path)         self.headers['content-type'] = "application/json" 

most should do.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

python - Error: Unresolved reference 'selenium' What is the reason? -

php - Need to store a large amount of data in session with CI 3 but on storing large data in session it is itself destorying automatically -