iripau.httpx module¶
A wrapper of the httpx module with the functionality of simulating a
curl command. See curlify for more details.
In theory, the original httpx module can be replaced with httpx
keeping the original functionality. After that, the extra features can be leveraged.
Attention
If access to the httpx exceptions or the other 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 httpx will be shown as curl commands without
any further modifications to the existing code, if any. See Client and
AsyncClient for more details.
Attention
This is not real-time output. The simulation takes place after the request
finishes, because it needs the httpx.Response object.
- class iripau.httpx.Client(*args, **kwargs)[source]¶
Bases:
ClientA
httpx.Clientthat acceptscurlify.curlify()arguments in theClient.request()method.Note
The constructor arguments are the same as in the base
httpx.Client.Example
Echo the simulated
curlcommand and its output into the terminal:client = Client(verify=False) response = client.get("https://dummyjson.com/test", 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" } client = Client() response = client.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, client = Client() response = client.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 an
httpx.Request, prepares it and sends it. Thecurlify.curlify()arguments can also be used in here.- 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
httpx.Client.request().**kwargs – Passed to the base
httpx.Client.request().
- Return type:
Response- Returns:
The result of performing a request.
- delete(*args, **kwargs)[source]¶
Call the DELETE HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.delete()**kwargs – Passed to
Client.delete()
- Return type:
Response- Returns:
The result of performing a request.
- get(*args, **kwargs)[source]¶
Call the GET HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.get()**kwargs – Passed to
Client.get()
- Return type:
Response- Returns:
The result of performing a request.
- head(*args, **kwargs)[source]¶
Call the HEAD HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.head()**kwargs – Passed to
Client.head()
- Return type:
Response- Returns:
The result of performing a request.
- options(*args, **kwargs)[source]¶
Call the OPTIONS HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.options()**kwargs – Passed to
Client.options()
- Return type:
Response- Returns:
The result of performing a request.
- patch(*args, **kwargs)[source]¶
Call the PATCH HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.patch()**kwargs – Passed to
Client.patch()
- Return type:
Response- Returns:
The result of performing a request.
- post(*args, **kwargs)[source]¶
Call the POST HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.post()**kwargs – Passed to
Client.post()
- Return type:
Response- Returns:
The result of performing a request.
- put(*args, **kwargs)[source]¶
Call the PUT HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.put()**kwargs – Passed to
Client.put()
- Return type:
Response- Returns:
The result of performing a request.
- class iripau.httpx.AsyncClient(*args, **kwargs)[source]¶
Bases:
AsyncClientA
httpx.AsyncClientthat acceptscurlify.curlify()arguments in theAsyncClient.request()method.Note
The constructor arguments are the same as in the base
httpx.AsyncClient.Example
Echo the simulated
curlcommand and its output into the terminal:client = AsyncClient(verify=False) response = client.get("https://dummyjson.com/test", 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" } client = AsyncClient() response = client.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, client = AsyncClient() response = client.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.
- async 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 an
httpx.Request, prepares it and sends it. Thecurlify.curlify()arguments can also be used in here.- 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
httpx.AsyncClient.request().**kwargs – Passed to the base
httpx.AsyncClient.request().
- Return type:
Response- Returns:
The result of performing a request.
- async delete(*args, **kwargs)[source]¶
Call the DELETE HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
AsyncClient.delete()**kwargs – Passed to
AsyncClient.delete()
- Return type:
Response- Returns:
The result of performing a request.
- async get(*args, **kwargs)[source]¶
Call the GET HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
AsyncClient.get()**kwargs – Passed to
AsyncClient.get()
- Return type:
Response- Returns:
The result of performing a request.
- async head(*args, **kwargs)[source]¶
Call the HEAD HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
AsyncClient.head()**kwargs – Passed to
AsyncClient.head()
- Return type:
Response- Returns:
The result of performing a request.
- async options(*args, **kwargs)[source]¶
Call the OPTIONS HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
AsyncClient.options()**kwargs – Passed to
AsyncClient.options()
- Return type:
Response- Returns:
The result of performing a request.
- async patch(*args, **kwargs)[source]¶
Call the PATCH HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
AsyncClient.patch()**kwargs – Passed to
AsyncClient.patch()
- Return type:
Response- Returns:
The result of performing a request.
- async post(*args, **kwargs)[source]¶
Call the POST HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
AsyncClient.post()**kwargs – Passed to
AsyncClient.post()
- Return type:
Response- Returns:
The result of performing a request.
- async put(*args, **kwargs)[source]¶
Call the PUT HTTP verb and return the result. The
curlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
AsyncClient.put()**kwargs – Passed to
AsyncClient.put()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.request(*args, **kwargs)[source]¶
Create a
Client, call any HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.request()**kwargs – Passed to
Client.request()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.delete(*args, **kwargs)[source]¶
Create a
Client, call the DELETE HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.delete()**kwargs – Passed to
Client.delete()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.get(*args, **kwargs)[source]¶
Create a
Client, call the GET HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.get()**kwargs – Passed to
Client.get()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.head(*args, **kwargs)[source]¶
Create a
Client, call the HEAD HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.head()**kwargs – Passed to
Client.head()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.options(*args, **kwargs)[source]¶
Create a
Client, call the OPTIONS HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.options()**kwargs – Passed to
Client.options()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.patch(*args, **kwargs)[source]¶
Create a
Client, call the PATCH HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.patch()**kwargs – Passed to
Client.patch()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.post(*args, **kwargs)[source]¶
Create a
Client, call the POST HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.post()**kwargs – Passed to
Client.post()
- Return type:
Response- Returns:
The result of performing a request.
- iripau.httpx.put(*args, **kwargs)[source]¶
Create a
Client, call the PUT HTTP verb and return the result. Thecurlify.curlify()arguments can also be used here.- Parameters:
*args – Passed to
Client.put()**kwargs – Passed to
Client.put()
- Return type:
Response- Returns:
The result of performing a request.