IBM / redstone

A Pythonic IBM Cloud SDK
Apache License 2.0
4 stars 9 forks source link

Add pre_send_fn functionality #21

Closed mrodden closed 1 year ago

mrodden commented 1 year ago
import redstone

def main():

    def add_header(req):
        req.headers["Authorization"] = "blah"
        req.headers["X-Trace-ID"] = "abcd1234"
        return req

    rc = redstone.service("ResourceController")
    rc.set_pre_send_fn(add_header)

    headers = rc.session.get("http://httpbin.org/headers")
    print(headers.text)

if __name__ == "__main__":
    import logging
    logging.basicConfig(level=logging.DEBUG)
    main()
$ python test_hook.py
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): iam.cloud.ibm.com:443
DEBUG:urllib3.connectionpool:[https://iam.cloud.ibm.com:443](https://iam.cloud.ibm.com/) "POST /oidc/token HTTP/1.1" 200 2368
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org:80
DEBUG:urllib3.connectionpool:[http://httpbin.org:80](http://httpbin.org/) "GET /headers HTTP/1.1" 200 286
{
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Authorization": "blah",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.25.1",
    "X-Amzn-Trace-Id": "Root=1-63dab281-1b836a767bdb79722c145d9b",
    "X-Trace-Id": "abcd1234"
  }
}