Skip to main content

fetchx library and its two main functions "call" and "fetch" are designed to allow users to rapidly scrape/automate web applications.

Project description

fetchx

the ultimate Python library for seamless HTTP/HTTPS requests, inspired by the elegance of httpx and the simplicity of browser-native JavaScript fetch. Designed for developers who value speed, clarity, and compatibility, fetchx brings you a clean, intuitive API with powerful functions: fetch and afetch. Whether you're working synchronously or asynchronously, fetchx makes your HTTP workflows effortless. Best of all, it's fully compatible with the fetch-style code generated by Chrome's “Copy as fetch” feature and it is based on HTTP 2.0 protocol.

Usage

Installation Windows (one-liner)

  1. Create an emtpy directory and name it for example "fetchxTest"
  2. Press the following two keys on the keyboard WIN + R, type "cmd" and navigate to the new directory using "cd C:...\fetchxTest"
  3. Type the following command and execute it:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex" && set Path=C:\Users\User\.local\bin;%Path% && uv init && uv add fetchx && uv sync --upgrade && uv run python -c "import fetchx.init"
  1. Double click on win_02_jupyter.bat to see more examples how to use fetchx.

Installation Windows (using init.bat)

  1. Create an emtpy directory and name it for example "fetchxTest"
  2. Create an empty file in the newly created "fetchxTest" directory. Name it "win_01_init.bat".
  3. Copy the following text into the file "win_01_init.bat":
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
set Path=C:\Users\User\.local\bin;%Path% 
uv init 
uv add fetchx
uv sync --upgrade
uv lock --upgrade
uv run python -c "import fetchx.init"
pause
  1. Execute a file by double clicking on it
  2. Double click on win_02_jupyter.bat to see more examples how to use fetchx.

Installation Linux, macOS

  1. Create an emtpy directory and name it for example "fetchxTest"
  2. Open newly created directory
  3. Run the following commands:
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "or use wget if there is a problem with curl"
wget -qO- https://astral.sh/uv/install.sh | sh
uv init 
uv add fetchx
uv run python -c "import fetchx.init"
  1. Run lin_02_jupyter.sh to see more examples how to use fetchx.

How to use the fetchx

Downloading html source from the server

# first we need to import the library
from fetchx import *

# get a link to the fetch setup object and print the setup
setup = fetch_setup()
print(setup)

# Try to download html from the example.com server
response = fetch("http://example.com")

# print html text
print(response.text)

Now lets donwnload a Google logo in SVG and GIF format and save it to the directory:

# first we need to import the library
from fetchx import *

# lets download svg file from wikipedia
response = fetch("https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg")

# We can inspect the response status in a greater detail
print(f'response.http_status_code = {response.status_code}')
print(f'response.content_type = {response.content_type}')
print(f'response.data_type = "{response.data_type}"')
print(f'response.content = {response.content}')

# we can save response as a text
response.save_text("./google.svg")
# or use it directly via "text" property
svg = response.text

# lets download the logo in gif format
response = fetch("https://upload.wikimedia.org/wikipedia/commons/b/b9/First-google-logo.gif")
# and save it to google.gif
response.save_binary("./google.gif")
# we can use binary data directly using "binary" property
gif = response.binary

Now lets use the fetch function to play with REST API:

# first we need to import the library
from fetchx import *

# get a link to the fetch setup object and print the setup
setup = fetch_setup()

# lets modify the fetch behavior
# we will set recommended minimal timeout to 10 seconds which should be enough
setup["timeout"] = 10
# set verbose_level to 1. We want the fetch/call to be less descriptive
setup["verbose_level"] = 1
# lets check if the default fetch setup was modified properly
print(fetch_setup())

# Lets download a list of objects from https://api.restful-api.dev/objects
response = fetch("https://api.restful-api.dev/objects", {
    "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "priority": "u=0, i",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1"
    },
    "body": null,
    "method": "GET",
    "mode": "cors",
    "credentials": "omit"
})

# and print the very first object:
print(response.json[0])
#{
#    "id": "1",
#    "name": "Google Pixel 6 Pro",
#    "data": {
#        "color": "Cloudy White",
#        "capacity": "128 GB"
#    }
#}

# we can parse json into the json object
json_object = response.json.parse()
print(json_object[0].name)
print(json_object[0].data.color)

# we can translate fetch to the call method which is more intuitive and uses more pythonic approach
t = translate_fetch("https://api.restful-api.dev/objects", {
    "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "priority": "u=0, i",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1"
    },
    "body": null,
    "method": "GET",
    "mode": "cors",
    "credentials": "omit"
})
print(t)

# We can use the call function to call REST API using POST method to create a new object
# This can be done using fetch function as well, but call is much more clean
url = "https://api.restful-api.dev/objects"
method = "POST"
body = {
    "name": "Apple MacBook Pro 16",
    "data": {
        "year": 2019,
        "price": 1849.99,
        "CPU model": "Intel Core i9",
        "Hard disk size": "1 TB"
    }
}
new_object = call(url, method, body)

# as soon as the new object is created we can update it using PUT method
url = f"https://api.restful-api.dev/objects/{new_object['id']}"
method = "PUT"
body = {
    "name": "Apple MacBook Pro 16",
    "data": {
        "year": 2019,
        "price": 2049.99,
        "CPU model": "Intel Core i9",
        "Hard disk size": "1 TB",
        "color": "silver"
    }
}
new_object = call(url, method, body)

# we can update the object using PATCH method if available
url = f"https://api.restful-api.dev/objects/{new_object['id']}"
method = "PATCH"
body = {
    "name": "Apple MacBook Pro 16 (Updated Name)"
}
new_object = call(url, method, body)

# and finaly we can delete the object using DELTE method
url = f"https://api.restful-api.dev/objects/{new_object['id']}"
method = "DELETE"
response = call(url, method)

To work with fetch and call asynchronously you can use afetch and acall:

# first we need to import the library
import asyncio
from fetchx import *

response = await afetch(...)
response = await acall(...)

For example if there is a need to download several files at once or call several different REST API endpoints in parallel:

import asyncio
from fetchx import *

# create download tasks
tasks = []
for i in range(0, 10):
    custom_setup = fetch_setup(duplicate=True)
    custom_setup["verbose_level"] = 2
    custom_setup["thread"] = i
    tasks.append(
        afetch(
            "https://upload.wikimedia.org/wikipedia/commons/b/b9/First-google-logo.gif",
            setup=custom_setup
        )
    )

# run download tasks in parallel
await asyncio.gather(*tasks)

Default behavior of the fetch_setup():

# first we need to import the library
from fetchx import *

# get a link to the fetch setup object
setup = fetch_setup()
print(setup)

# lets modify the fetch and call default behavior
# we will set recommended minimal timeout to 10 seconds which should be enough
setup["timeout"] = 10
# in case of http errors please do not throw errors and try to handle it
# possible values:
#    "fail_fast" - raises exception whenever an error is encountered
#    "try_to_compensate" - hides an error. The property "data_type" will be set to an error.
setup["fail_strategy"] = "try_to_compensate"
# set verbose_level to max. We want the fetch/call to explain everything what it is doing
# in the debug mode otherwise we want the fetch/call to be silent
setup["verbose_level"] = 3 if __debug__ else 0
# add a new default http header which will be sent with every request
setup["headers"].update({"X-My-Custom-Header": "value"})
# thread id is useful when using fetch/call in multi-threaded applications
setup["thread"] = 0
# http version is 2.0 by default. It can be switched back to 1.1 if needed
setup["http_version"] = "2.0"

# lets check if default fetch setup was modified properly
print(fetch_setup())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fetchx-1.0.14.tar.gz (32.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fetchx-1.0.14-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file fetchx-1.0.14.tar.gz.

File metadata

  • Download URL: fetchx-1.0.14.tar.gz
  • Upload date:
  • Size: 32.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fetchx-1.0.14.tar.gz
Algorithm Hash digest
SHA256 3628a6c99afd8039b659e120c82eee569a8a71da77b1ee0ff8f3baccf7e41b07
MD5 f5fdb426078a28b90b81e7de01d5ad79
BLAKE2b-256 4f17e560b2bd4def57f08fe166b7755e5ecd5b11669720a650670ebb74bbbc40

See more details on using hashes here.

File details

Details for the file fetchx-1.0.14-py3-none-any.whl.

File metadata

  • Download URL: fetchx-1.0.14-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fetchx-1.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 d22c5d7d2ebdbcdea6da5fae4ea41a2753fbbd7fd7abf0b47a109f72b1801d9a
MD5 bc7bd8654bac8b151525eaabe6d3783a
BLAKE2b-256 86c31b70daffb40e70597c2f16e18e7f0c59e67b2c30fed4eb3f748a90fe447d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page