assert get_env('x') == ''
RestReqFactory
ENV
map to handle env variables
get_env
get_env (x:str)
Type | Details | |
---|---|---|
x | str | key to filter |
set_env
set_env (key:str, val:str)
Type | Details | |
---|---|---|
key | str | key to map variable |
val | str | value of the key |
'x', 3)
set_env(assert get_env('x') == 3
RestReq
Base class to hold attributes of the rest request.
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
= RestReq(
req ="POST",
method='https://httpbin.org/post',
url={
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
= req()
resp type(resp)
requests.models.Response
RestReqFactory
Class to bind the lambda function to create RestReq
object`
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
= lambda: f"{get_env('url')}/post"
url
= lambda : {
head "accept": f"{get_env('accept')}",
}
# Create the RestReqFactory instance
= RestReqFactory(
req ="POST",
method=url,
url_provider=head,
headers_provider )
assert req().curl == """curl -X POST '/post' -H 'accept: '"""
leveraging Dynammic nature of ENV
'url', 'https://httpbin.org')
set_env('accept', 'application/json') set_env(
assert req().curl == """curl -X POST 'https://httpbin.org/post' -H 'accept: application/json'"""
= req()()
resp assert resp.status_code == 200
req()
{
"method": "POST",
"url": "https://httpbin.org/post",
"headers": {
"accept": "application/json"
},
"params": "None",
"body": "None",
"kwargs": "None"
}