iripau.requests module¶
A wrapper of the requests module with the functionality of simulating a
curl command. See curlify for more details.
In theory, the original requests module can be replaced with requests
keeping the original functionality. After that, the extra features can be leveraged.
Attention
If access to the requests exceptions, “lower-level” or
“lower-lower-level” classes is required, they need to be imported directly
from the original module.
If a global configuration is made as in the examples mentioned in logging, the
requests made using requests will be shown as curl commands without
any further modifications to the existing code, if any. See Session
for more details.
Attention
This is not real-time output. The simulation takes place after the request
finishes, because it needs the requests.Response object.
- class iripau.requests.Session[source]¶
Bases:
SessionA
requests.Sessionthat acceptscurlify.curlify()arguments in theSession.request()method.Note
The constructor arguments are the same as in the base
requests.Session.Example
Echo the simulated
curlcommand and its output into the terminal:session = Session() response = session.get("https://dummyjson.com/test", verify=False, echo=True) # $ curl -H 'User-Agent: python-requests/2.32.3' -H 'Accept-Encoding: gzip, deflate' -H 'Accept: */*' -H 'Connection: keep-alive' --insecure https://dummyjson.com/test # 200 - OK # {"status":"ok","method":"GET"}
Example
Pass some headers to a request and echo the pretty
curlequivalent command into the terminal but hide the value of some headers and without showing other ones, such as the ones added automatically:headers = { "Accept": "application/json", "Authorization": "Bearer A_VERY_LONG_AND_SECRET_JWT_TOKEN", "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7412.EU" } session = Session() response = session.post( "https://dummyjson.com/test", headers=headers, json={"field": "value"}, headers_to_hide=["Authorization", "API-Key"], headers_to_omit=["Accept-Encoding", "Content-Type", "User-Agent"], pretty=True, echo=True ) # $ curl -H 'Accept-Encoding: gzip, deflate' \ # > -H 'Accept: application/json' \ # > -H 'Connection: keep-alive' \ # > -H 'Authorization: ***' \ # > -d '{"field": "value"}' \ # > https://dummyjson.com/test # 200 - OK # {"status":"ok","method":"POST"}
In that example, the intention was to hide the header
API-Key, but it was not actually used in the request, so it simply gets ignored.Example
Print the content of the response as a prettified JSON string:
from iripau.curlify import try_json_content, session = Session() response = session.get( "https://dummyjson.com/test", headers_to_omit=[ "Accept", "Accept-Encoding", "Connection", "Content-Length", "Content-Type", "User-Agent" ], output_processor=try_json_content, echo=True ) # $ curl https://dummyjson.com/test # 200 - OK # { # "status": "ok", # "method": "GET" # }
Hint
The names of the headers are case-insensitive.
- request(*args, compressed=False, pretty=False, output_processor=None, headers_to_hide=[], headers_to_omit=[], stdout_tees=[], add_global_stdout_tees=True, stderr_tees=[], add_global_stderr_tees=True, prompt_tees=[], add_global_prompt_tees=True, echo=None, **kwargs)[source]¶
Constructs a
requests.Request, prepares it and sends it. Thecurlify.curlify()arguments can also be used in here, as well as in the methods for the specific HTTP verbs:Session.delete()Session.get()Session.head()Session.options()Session.patch()Session.post()Session.put()
- Parameters:
compressed – The same as in
curlify.curlify().pretty – The same as in
curlify.curlify().output_processor – The same as in
curlify.curlify().headers_to_hide – The same as in
curlify.curlify().headers_to_omit – The same as in
curlify.curlify().stdout_tees – The same as in
subprocess.Popen.stderr_tees – The same as in
subprocess.Popen.prompt_tees – The same as in
subprocess.Popen.add_global_stdout_tees – The same as in
subprocess.Popen.add_global_stderr_tees – The same as in
subprocess.Popen.add_global_prompt_tees – The same as in
subprocess.Popen.echo – The same as in
subprocess.Popen.*args – Passed to the base
requests.Session.request().**kwargs – Passed to the base
requests.Session.request().
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.request(*args, **kwargs)[source]¶
Create a
Session, call any HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.request()**kwargs – Passed to
Session.request()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.delete(*args, **kwargs)[source]¶
Create a
Session, call the DELETE HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.delete()**kwargs – Passed to
Session.delete()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.get(*args, **kwargs)[source]¶
Create a
Session, call the GET HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.get()**kwargs – Passed to
Session.get()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.head(*args, **kwargs)[source]¶
Create a
Session, call the HEAD HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.head()**kwargs – Passed to
Session.head()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.options(*args, **kwargs)[source]¶
Create a
Session, call the OPTIONS HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.options()**kwargs – Passed to
Session.options()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.patch(*args, **kwargs)[source]¶
Create a
Session, call the PATCH HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.patch()**kwargs – Passed to
Session.patch()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.post(*args, **kwargs)[source]¶
Create a
Session, call the POST HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.post()**kwargs – Passed to
Session.post()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.requests.put(*args, **kwargs)[source]¶
Create a
Session, call the PUT HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Session.put()**kwargs – Passed to
Session.put()
- Return type:
Response- Returns:
The result of performing a request.