RestReqFactory

ENV map to handle env variables


source

get_env

 get_env (x:str)
Type Details
x str key to filter
assert get_env('x') == ''

source

set_env

 set_env (key:str, val:str)
Type Details
key str key to map variable
val str value of the key
set_env('x', 3)
assert get_env('x') == 3

RestReq

Base class to hold attributes of the rest request.


source

RestReq

 RestReq (method:str, url:str, headers:Optional[dict]=None,
          params:Optional[dict]=None, body:Optional[dict]=None,
          kwargs:Optional[dict]=None)

Rest request building method

req = RestReq(
    method="POST",
    url='https://httpbin.org/post',
    headers={
    "accept": "application/json",
    },
)

To extract curl repr

req.curl
"curl -X POST 'https://httpbin.org/post' -H 'accept: application/json'"

To invoke the rest call

resp = req()
type(resp)
requests.models.Response

RestReqFactory

Class to bind the lambda function to create RestReq object`


source

RestReqFactory

 RestReqFactory (method:str, url_provider:<function <lambda>>,
                 headers_provider=<function <lambda>>,
                 params_provider=<function <lambda>>,
                 body_provider=<function <lambda>>)

Class to bind the lambda function to create RestReq object

Type Default Details
method str represents the request method
url_provider function returning url
headers_provider function function returning headers
params_provider function function returning params
body_provider function function returning params

Build basic RestReqFactory object

url = lambda: f"{get_env('url')}/post"

head = lambda : {
    "accept": f"{get_env('accept')}",
}

# Create the RestReqFactory instance
req = RestReqFactory(
    method="POST",
    url_provider=url,
    headers_provider=head,
)
assert req().curl == """curl -X POST '/post' -H 'accept: '"""

leveraging Dynammic nature of ENV

set_env('url', 'https://httpbin.org')
set_env('accept', 'application/json')
assert req().curl == """curl -X POST 'https://httpbin.org/post' -H 'accept: application/json'"""
resp = req()()
assert resp.status_code  == 200
req()
{
    "method": "POST",
    "url": "https://httpbin.org/post",
    "headers": {
        "accept": "application/json"
    },
    "params": "None",
    "body": "None",
    "kwargs": "None"
}

Cookies testing

url = lambda: f"{get_env('postman-url')}/get"

params = lambda : {
    "foo1": f"{get_env('foo1')}",
    "foo2": f"{get_env('foo2')}",
}

# Create the RestReqFactory instance
req = RestReqFactory(
    method="GET",
    url_provider=url,
    params_provider=params,
)
set_env('foo1', 'foo1')
set_env('foo2', 'foo2')
set_env('postman-url', 'https://postman-echo.com')

Setting up the kwargs field for the req object

res = req()()
assert res.status_code == 200
req.set_req_kwargs({'cookies' : res.cookies})

check __str__ and __repr__

req()
{
    "method": "GET",
    "url": "https://postman-echo.com/get",
    "headers": "None",
    "params": {
        "foo1": "foo1",
        "foo2": "foo2"
    },
    "body": "None",
    "kwargs": {
        "cookies": [
            {
                "name": "sails.sid",
                "value": "s%3Af2Ma6_qtUOO6o8rLJuS0_oGC3nf42AGn.4f5n7pa3spk6owtt0P1lt4lsELJVx%2FTZUwJ7w97sb6Y",
                "domain": "postman-echo.com",
                "path": "/"
            }
        ]
    }
}