Skip to main content

A simple CORS proxy utility with requests-like response

Project description

jupyterlite-simple-cors-proxy

Simple CORS proxy wrapper for making http requests from JupyterLite. Uses https://corsproxy.io/

Installation

pip install jupyterlite-simple-cors-proxy

Usage

from jupyterlite_simple_cors_proxy.proxy import cors_proxy_get, robust_get_request, furl, xurl

# Set up
url = "https://api.example.com/data"
# Optional params
params = {"key": "value"}

# Get a cross-origin proxied url
cross_origin_url = xurl(url) # xurl(url, params)

# Get a file like object
# (Make the request, then create a file like object
# from the response)
file_ob = furl(url) # furl(url, params)

# Make a request
response = cors_proxy_get(url, params)

# Use like requests
print(response.text)
data = response.json()
raw = response.content

The robust_get_request() will first try a simple request, then a proxied request: robust_get_request(url, params)

Features

  • Simple CORS proxy wrapper
  • Requests response object
  • Support for URL parameters

fastf1 cors proxy

A monkey patch for fastf1 is provided as:

import fast f1
from jupyterlite_simple_cors_proxy.fastf1_proxy import enable_cors_proxy

enable_cors_proxy(
#    domains=["api.formula1.com", "livetiming.formula1.com"],
#    debug=True,
#    proxy_url="https://corsproxy.io/",
)

CorsProxy with cache facility

Via claude.ai, the package is now further enriched.

Note that pyodide sqlite can't write to /drive so the cache path dir needs to be something like /tmp or a dir created on /.

I'm not convinced the following works in pyodide and xeus-python yet - requests-cache dependency issues etc. requests-cache has requirements attrs, cattrs,platformdirs, url-normalize.

from simple_cors_proxy.proxy import CorsProxy

# Create a cached proxy instance
proxy = CorsProxy(use_cache=True, expire_after=3600)  # Cache for 1 hour

# Use furl directly from your proxy instance
file_like = proxy.furl('https://example.com/somefile.csv')

#----
import pandas as pd
from simple_cors_proxy.cacheproxy import CorsProxy

proxy = CorsProxy(use_cache=True)
file_like = proxy.furl('https://example.com/data.csv')
df = pd.read_csv(file_like)

#----

from simple_cors_proxy.proxy import create_cached_proxy

proxy = create_cached_proxy(cache_name='my_cache', expire_after=86400)  # Cache for 1 day
file_like = proxy.furl('https://example.com/somefile.csv')

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

jupyterlite_simple_cors_proxy-0.1.17.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file jupyterlite_simple_cors_proxy-0.1.17.tar.gz.

File metadata

File hashes

Hashes for jupyterlite_simple_cors_proxy-0.1.17.tar.gz
Algorithm Hash digest
SHA256 c319da3363ebdf5a3c88f2ca3b6f205c596d1d1929c06f974e69eb6eb2e7eec6
MD5 6fb318ac379045af7e2c3c9949a037e7
BLAKE2b-256 b6c499fad7759dfd515f347957986d57de5964c6852416575a4e396bf289109e

See more details on using hashes here.

File details

Details for the file jupyterlite_simple_cors_proxy-0.1.17-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyterlite_simple_cors_proxy-0.1.17-py3-none-any.whl
Algorithm Hash digest
SHA256 494cb43ab95e1b3186794f07b5fa7e4ff8ec80893ad06bdc3503aa1ab2e97a26
MD5 725a87f8d0e85b884403f438f36656eb
BLAKE2b-256 4308051798ceb323432e558d5eea1fe53c419336023101f155582367b8fc8989

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