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: Client

A httpx.Client that accepts curlify.curlify() arguments in the Client.request() method.

Note

The constructor arguments are the same as in the base httpx.Client.

Example

Echo the simulated curl command 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 curl equivalent 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. The curlify.curlify() arguments can also be used in here.

Parameters:
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:
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:
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:
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:
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:
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:
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:
Return type:

Response

Returns:

The result of performing a request.

class iripau.httpx.AsyncClient(*args, **kwargs)[source]

Bases: AsyncClient

A httpx.AsyncClient that accepts curlify.curlify() arguments in the AsyncClient.request() method.

Note

The constructor arguments are the same as in the base httpx.AsyncClient.

Example

Echo the simulated curl command 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 curl equivalent 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. The curlify.curlify() arguments can also be used in here.

Parameters:
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:
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:
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:
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:
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:
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:
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:
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. The curlify.curlify() arguments can also be used here.

Parameters:
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. The curlify.curlify() arguments can also be used here.

Parameters:
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. The curlify.curlify() arguments can also be used here.

Parameters:
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. The curlify.curlify() arguments can also be used here.

Parameters:
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. The curlify.curlify() arguments can also be used here.

Parameters:
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. The curlify.curlify() arguments can also be used here.

Parameters:
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. The curlify.curlify() arguments can also be used here.

Parameters:
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. The curlify.curlify() arguments can also be used here.

Parameters:
Return type:

Response

Returns:

The result of performing a request.