Runner

Callback

  • Basic callbacks for Runner. Callbacks can occur at any of these times before_run, before_request, after_request, after_run. Each order of the new derived callback should be greated that 0.
  • Each class have the access to epoch, resp the response returned the respinse, reqs_bunch the req list, resps_bunch response list, kwargs_bunch kwargs dictionary.
  • it also the access to both set_kwargs and set_resp

Runner

Init the runners with list of callbacks.


source

Runner

 Runner (cb_funcs=None)

Initialize self. See help(type(self)) for accurate signature.

Bellow are two callbacks we implemented with its functionality
1. CookieCallback is used for chaining the cookies from a rasponse to setnext request’s kwargs
1. ResponseStoreCallback for storing each request’s response

# Create the RestReqFactory instance
req1 = RestReqFactory(
    method="GET",
    url_provider= lambda: f"{get_env('url1')}/get",
    params_provider= lambda : {
        "foo1": f"{get_env('foo1')}",
        "foo2": f"{get_env('foo2')}",
    }
)
req2 = RestReqFactory(
    method="GET",
    url_provider= lambda : f"{get_env('url2')}/api/users/2",
)
reqs = ReqRespBunch([req1, req2])
len(reqs)
2
set_env('foo1', 'foo1')
set_env('foo2', 'foo2')
set_env('url1', 'https://postman-echo.com')
set_env('url2', 'https://reqres.in')
runner = Runner()
runner.run(reqs, debug=True)
=================================== epoch=1 ===================================
request:
--------
{
    "method": "GET",
    "url": "https://postman-echo.com/get",
    "headers": "None",
    "params": {
        "foo1": "foo1",
        "foo2": "foo2"
    },
    "body": "None",
    "kwargs": "None"
}
kwargs:
-------
{}
execution time: 875.37ms
response:
---------
{
    "url": "https://postman-echo.com/get?foo1=foo1&foo2=foo2",
    "status": 200,
    "headers": {
        "Date": "Mon, 10 Jun 2024 14:32:22 GMT",
        "Content-Type": "application/json; charset=utf-8",
        "Content-Length": "512",
        "Connection": "close",
        "Server": "nginx/1.25.3",
        "ETag": "W/\"200-u8K/5We2ycAZZmfyGOJShXcIc88\"",
        "set-cookie": "sails.sid=s%3AbccKPy3Ni4wAGdPwtqitIniFOpF-JHp6.eRZq87hnH6nr%2FgaN%2BR6ug%2BX5sx6NpLtOEQj%2B%2BiZamIE; Path=/; HttpOnly"
    },
    "cookies": {
        "sails.sid": "s%3AbccKPy3Ni4wAGdPwtqitIniFOpF-JHp6.eRZq87hnH6nr%2FgaN%2BR6ug%2BX5sx6NpLtOEQj%2B%2BiZamIE"
    },
    "elapsed": "0:00:00.869218",
    "text": "{\n  \"args\": {\n    \"foo1\": \"foo1\",\n    \"foo2\": \"foo2\"\n  },\n  \"headers\": {\n    \"host\": \"postman-echo.com\",\n    \"x-forwarded-proto\": \"http\",\n    \"x-request-start\": \"t=1718029942.757\",\n    \"connection\": \"close\",\n    \"content-length\": \"4\",\n    \"x-forwarded-port\": \"443\",\n    \"x-amzn-trace-id\": \"Root=1-66670e76-2660a8501e67c6470d137f23\",\n    \"user-agent\": \"python-requests/2.31.0\",\n    \"accept-encoding\": \"gzip, deflate, br, zstd\",\n    \"accept\": \"*/*\"\n  },\n  \"url\": \"http://postman-echo.com/get?foo1=foo1&foo2=foo2\"\n}",
    "json": {
        "args": {
            "foo1": "foo1",
            "foo2": "foo2"
        },
        "headers": {
            "host": "postman-echo.com",
            "x-forwarded-proto": "http",
            "x-request-start": "t=1718029942.757",
            "connection": "close",
            "content-length": "4",
            "x-forwarded-port": "443",
            "x-amzn-trace-id": "Root=1-66670e76-2660a8501e67c6470d137f23",
            "user-agent": "python-requests/2.31.0",
            "accept-encoding": "gzip, deflate, br, zstd",
            "accept": "*/*"
        },
        "url": "http://postman-echo.com/get?foo1=foo1&foo2=foo2"
    }
}
=================================== epoch=2 ===================================
request:
--------
{
    "method": "GET",
    "url": "https://reqres.in/api/users/2",
    "headers": "None",
    "params": "None",
    "body": "None",
    "kwargs": {
        "cookies": [
            {
                "name": "sails.sid",
                "value": "s%3AbccKPy3Ni4wAGdPwtqitIniFOpF-JHp6.eRZq87hnH6nr%2FgaN%2BR6ug%2BX5sx6NpLtOEQj%2B%2BiZamIE",
                "domain": "postman-echo.com",
                "path": "/"
            }
        ]
    }
}
kwargs:
-------
{'cookies': <RequestsCookieJar[Cookie(version=0, name='sails.sid', value='s%3AbccKPy3Ni4wAGdPwtqitIniFOpF-JHp6.eRZq87hnH6nr%2FgaN%2BR6ug%2BX5sx6NpLtOEQj%2B%2BiZamIE', port=None, port_specified=False, domain='postman-echo.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)]>}
execution time: 160.78ms
response:
---------
{
    "url": "https://reqres.in/api/users/2",
    "status": 200,
    "headers": {
        "Date": "Mon, 10 Jun 2024 14:32:23 GMT",
        "Content-Type": "application/json; charset=utf-8",
        "Transfer-Encoding": "chunked",
        "Connection": "keep-alive",
        "Report-To": "{\"group\":\"heroku-nel\",\"max_age\":3600,\"endpoints\":[{\"url\":\"https://nel.heroku.com/reports?ts=1718010669&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=jI4DN4S34hLQCMFUREXx0gJJEl%2BEwua9NFbOTVfQ8pg%3D\"}]}",
        "Reporting-Endpoints": "heroku-nel=https://nel.heroku.com/reports?ts=1718010669&sid=c4c9725f-1ab0-44d8-820f-430df2718e11&s=jI4DN4S34hLQCMFUREXx0gJJEl%2BEwua9NFbOTVfQ8pg%3D",
        "Nel": "{\"report_to\":\"heroku-nel\",\"max_age\":3600,\"success_fraction\":0.005,\"failure_fraction\":0.05,\"response_headers\":[\"Via\"]}",
        "X-Powered-By": "Express",
        "Access-Control-Allow-Origin": "*",
        "Etag": "W/\"118-pbdwwFo9SKNhD3Lx5iHJyngpq00\"",
        "Via": "1.1 vegur",
        "Cache-Control": "max-age=14400",
        "CF-Cache-Status": "HIT",
        "Age": "4827",
        "Vary": "Accept-Encoding",
        "Server": "cloudflare",
        "CF-RAY": "891a1207cd5480c2-BOM",
        "Content-Encoding": "br"
    },
    "cookies": {},
    "elapsed": "0:00:00.159517",
    "text": "{\"data\":{\"id\":2,\"email\":\"janet.weaver@reqres.in\",\"first_name\":\"Janet\",\"last_name\":\"Weaver\",\"avatar\":\"https://reqres.in/img/faces/2-image.jpg\"},\"support\":{\"url\":\"https://reqres.in/#support-heading\",\"text\":\"To keep ReqRes free, contributions towards server costs are appreciated!\"}}",
    "json": {
        "data": {
            "id": 2,
            "email": "janet.weaver@reqres.in",
            "first_name": "Janet",
            "last_name": "Weaver",
            "avatar": "https://reqres.in/img/faces/2-image.jpg"
        },
        "support": {
            "url": "https://reqres.in/#support-heading",
            "text": "To keep ReqRes free, contributions towards server costs are appreciated!"
        }
    }
}