jupyterlite-simple-cors-proxy 0.1.11__py3-none-any.whl → 0.1.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,10 +7,16 @@ import platform
7
7
  from typing import Optional, Union
8
8
 
9
9
  PLATFORM = platform.system().lower()
10
+ CORS_PROXIES = {
11
+ "corsproxyio": {"url": "https://corsproxy.io/?url={}", "quote": False},
12
+ "allorigins": {"url": "https://api.allorigins.win/raw?url={}", "quote": True},
13
+ "none": {"url": "{}", "quote": False},
14
+ }
15
+
10
16
 
11
17
  class CorsProxy:
12
18
  """CORS Proxy with optional caching support."""
13
-
19
+
14
20
  def __init__(self, use_cache: bool = False, **cache_kwargs):
15
21
  """
16
22
  Initialize the CORS proxy.
@@ -30,21 +36,61 @@ class CorsProxy:
30
36
  self.session = requests_cache.CachedSession(**cache_kwargs)
31
37
  else:
32
38
  self.session = requests
39
+ if "proxy" in cache_kwargs:
40
+ self.proxy = cache_kwargs["proxy"]
41
+ else:
42
+ self.proxy = "corsproxyio"
43
+
44
+ def apply_cors_proxy(self, url, proxy="corsproxyio"):
45
+ """
46
+ Apply a CORS proxy to the given URL.
47
+
48
+ Args:
49
+ url (str): The original URL to proxy
50
+ proxy (str): The proxy identifier to use from CORS_PROXIES
51
+
52
+ Returns:
53
+ str: The proxied URL
54
+ """
55
+ if proxy not in CORS_PROXIES:
56
+ raise ValueError(
57
+ f"Unknown proxy: {proxy}. Available proxies: {', '.join(CORS_PROXIES.keys())}"
58
+ )
59
+
60
+ proxy_config = CORS_PROXIES[proxy]
61
+
62
+ if proxy_config["quote"]:
63
+ url = proxy_config["url"].format(quote(url))
64
+ else:
65
+ url = proxy_config["url"].format(url)
66
+
67
+ return url
33
68
 
34
- def xurl(self, url: str, params: Optional[dict] = None, force: bool = False) -> str:
69
+ def xurl(
70
+ self,
71
+ url: str,
72
+ params: Optional[dict] = None,
73
+ force: bool = False,
74
+ proxy: str = "",
75
+ ) -> str:
35
76
  """Generate a proxied URL."""
36
77
  if PLATFORM == "emscripten" or force:
37
78
  if params:
38
79
  url = f"{url}?{urlencode(params)}"
39
- url = f"https://corsproxy.io/{quote(url)}"
80
+ # url = f"https://corsproxy.io/{quote(url)}"
81
+ if not proxy:
82
+ proxy = self.proxy
83
+ url = self.apply_cors_proxy(url, proxy=proxy)
40
84
  return url
41
85
 
42
- def furl(self, url: str, params: Optional[dict] = None, force: bool = False) -> io.BytesIO:
86
+ def furl(self, url: str, params: Optional[dict] = None, force: bool = False, proxy: str = "") -> io.BytesIO:
43
87
  """Return file like object after calling the proxied URL."""
44
- r = self.cors_proxy_get(url, params, force)
88
+ r = self.cors_proxy_get(url, params, force, proxy)
89
+ # TO DO - something to consider?
90
+ # https://simonwillison.net/2025/Jan/31/save-memory-with-bytesio/
45
91
  return io.BytesIO(r.content)
46
92
 
47
- def cors_proxy_get(self, url: str, params: Optional[dict] = None, force: bool = False) -> requests.Response:
93
+ def cors_proxy_get(self, url: str, params: Optional[dict] = None, force: bool = False, proxy: str = "corsproxyio") -> requests.Response:
48
94
  """
49
95
  CORS proxy for GET resources with requests-like response.
50
96
 
@@ -59,16 +105,19 @@ class CorsProxy:
59
105
  proxy_url = self.xurl(url, params, force)
60
106
  return self.session.get(proxy_url)
61
107
 
62
- def robust_get_request(self, url: str, params: Optional[dict] = None) -> requests.Response:
108
+ def robust_get_request(
109
+ self, url: str, params: Optional[dict] = None, proxy: str = ""
110
+ ) -> requests.Response:
63
111
  """
64
112
  Try to make a simple request else fall back to a proxy.
65
113
  """
66
114
  try:
67
115
  r = self.session.get(url, params=params)
68
116
  except:
69
- r = self.cors_proxy_get(url, params=params)
117
+ r = self.cors_proxy_get(url, params=params, proxy=proxy)
70
118
  return r
71
119
 
120
+
72
121
  # Create default instance
73
122
  _default_proxy = CorsProxy()
74
123
 
@@ -81,4 +130,4 @@ robust_get_request = _default_proxy.robust_get_request
81
130
  # Convenience function to create a cached proxy
82
131
  def create_cached_proxy(**cache_kwargs):
83
132
  """Create a new CorsProxy instance with caching enabled."""
84
- return CorsProxy(use_cache=True, **cache_kwargs)
133
+ return CorsProxy(use_cache=True, **cache_kwargs)
@@ -6,27 +6,61 @@ import io
6
6
  import platform
7
7
 
8
8
  PLATFORM = platform.system().lower()
9
+ CORS_PROXIES = {
10
+ "corsproxyio": {"url": "https://corsproxy.io/?url={}", "quote": False},
11
+ "allorigins": {"url": "https://api.allorigins.win/raw?url={}", "quote": True},
12
+ "none": {"url": "{}", "quote": False},
13
+ }
9
14
 
10
15
 
11
- def xurl(url, params=None, force=False):
16
+ def apply_cors_proxy(url, proxy="corsproxyio"):
17
+ """
18
+ Apply a CORS proxy to the given URL.
19
+
20
+ Args:
21
+ url (str): The original URL to proxy
22
+ proxy (str): The proxy identifier to use from CORS_PROXIES
23
+
24
+ Returns:
25
+ str: The proxied URL
26
+ """
27
+ if proxy not in CORS_PROXIES:
28
+ raise ValueError(
29
+ f"Unknown proxy: {proxy}. Available proxies: {', '.join(CORS_PROXIES.keys())}"
30
+ )
31
+
32
+ proxy_config = CORS_PROXIES[proxy]
33
+
34
+ if proxy_config["quote"]:
35
+ url = proxy_config["url"].format(quote(url))
36
+ else:
37
+ url = proxy_config["url"].format(url)
38
+
39
+ return url
40
+
41
+
42
+ def xurl(url, params=None, force=False, proxy="corsproxyio"):
12
43
  """Generate a proxied URL."""
13
44
  if PLATFORM == "emscripten" or force:
14
45
  if params:
15
46
  url = f"{url}?{urlencode(params)}"
16
- url = f"https://corsproxy.io/{quote(url)}"
47
+ # url = f"https://corsproxy.io/{quote(url)}"
48
+ url = apply_cors_proxy(url, proxy=proxy)
17
49
 
18
50
  return url
19
51
 
20
52
 
21
- def furl(url, params=None, force=False):
53
+ def furl(url, params=None, force=False, proxy="corsproxyio"):
22
54
  """Return file like object after calling the proxied URL."""
23
- r = cors_proxy_get(url, params, force)
55
+ r = cors_proxy_get(url, params, force, proxy=proxy)
24
56
 
25
57
  # Return a file-like object from the JSON string
58
+ # TO DO - something to consider?
59
+ # https://simonwillison.net/2025/Jan/31/save-memory-with-bytesio/
26
60
  return io.BytesIO(r.content)
27
61
 
28
62
 
29
- def cors_proxy_get(url, params=None, force=False):
63
+ def cors_proxy_get(url, params=None, force=False, proxy="corsproxyio"):
30
64
  """
31
65
  CORS proxy for GET resources with requests-like response.
32
66
 
@@ -37,19 +71,19 @@ def cors_proxy_get(url, params=None, force=False):
37
71
  Returns:
38
72
  A requests response object.
39
73
  """
40
- proxy_url = xurl(url, params, force)
74
+ proxy_url = xurl(url, params, force, proxy=proxy)
41
75
 
42
76
  # Do a simple requests get and
43
77
  # just pass through the entire response object
44
78
  return requests.get(proxy_url)
45
79
 
46
80
 
47
- def robust_get_request(url, params=None):
81
+ def robust_get_request(url, params=None, proxy="corsproxyio"):
48
82
  """
49
83
  Try to make a simple request else fall back to a proxy.
50
84
  """
51
85
  try:
52
86
  r = requests.get(url, params=params)
53
87
  except:
54
- r = cors_proxy_get(url, params=params)
88
+ r = cors_proxy_get(url, params=params, proxy=proxy)
55
89
  return r
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: jupyterlite-simple-cors-proxy
3
- Version: 0.1.11
3
+ Version: 0.1.13
4
4
  Summary: A simple CORS proxy utility with requests-like response
5
5
  Home-page: https://github.com/innovationOUtside/jupyterlite-simple-cors-proxy
6
6
  Author: Tony Hirst
@@ -0,0 +1,9 @@
1
+ jupyterlite_simple_cors_proxy/__init__.py,sha256=vE2WKSaR1d-lu1EoKN3obCHFUB-oowD7I1jwEl5qvok,280
2
+ jupyterlite_simple_cors_proxy/cacheproxy.py,sha256=MEoSScE-AwliQnERxDJf9QIBOrpholsmvYJyw9C8-y4,4465
3
+ jupyterlite_simple_cors_proxy/fastf1_proxy.py,sha256=FglRogTIlSJvHOu6lFS3S-EHDb37M93aYjQpoKc1QYs,7614
4
+ jupyterlite_simple_cors_proxy/proxy.py,sha256=HkPMY1Tq8Cm4w7nNnzdw8uiRpVuCBj0PtW5Dhk5xQ2c,2478
5
+ jupyterlite_simple_cors_proxy-0.1.13.dist-info/LICENSE,sha256=Ogw7GUmeZIxmDNiKWsu_N07svNoGnPB7lWyiXHX_rMY,1074
6
+ jupyterlite_simple_cors_proxy-0.1.13.dist-info/METADATA,sha256=UZcKfjuVk0ID5DBghL_wQXLjmpZuE_GOfJaw8qPf1fU,3144
7
+ jupyterlite_simple_cors_proxy-0.1.13.dist-info/WHEEL,sha256=beeZ86-EfXScwlR_HKu4SllMC9wUEj_8Z_4FJ3egI2w,91
8
+ jupyterlite_simple_cors_proxy-0.1.13.dist-info/top_level.txt,sha256=Oh0oQrSmRnBq5u675coiKMbkb2ASg8AGF8ZiZTzUS5Q,30
9
+ jupyterlite_simple_cors_proxy-0.1.13.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (76.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,9 +0,0 @@
1
- jupyterlite_simple_cors_proxy/__init__.py,sha256=vE2WKSaR1d-lu1EoKN3obCHFUB-oowD7I1jwEl5qvok,280
2
- jupyterlite_simple_cors_proxy/cacheproxy.py,sha256=Rf3tR6knz70N-Bu9M-HhWJY88cI3WpW0MmIJBgpm008,3002
3
- jupyterlite_simple_cors_proxy/fastf1_proxy.py,sha256=FglRogTIlSJvHOu6lFS3S-EHDb37M93aYjQpoKc1QYs,7614
4
- jupyterlite_simple_cors_proxy/proxy.py,sha256=uFpevhGlkbcNqVb1d43uBrqVdvAbOQ2m1PLqxsML6BE,1346
5
- jupyterlite_simple_cors_proxy-0.1.11.dist-info/LICENSE,sha256=Ogw7GUmeZIxmDNiKWsu_N07svNoGnPB7lWyiXHX_rMY,1074
6
- jupyterlite_simple_cors_proxy-0.1.11.dist-info/METADATA,sha256=SMAXcQejInZ_6a00wZAqVS_NI8A_Kh-cfvBMIBDJUBo,3144
7
- jupyterlite_simple_cors_proxy-0.1.11.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
8
- jupyterlite_simple_cors_proxy-0.1.11.dist-info/top_level.txt,sha256=Oh0oQrSmRnBq5u675coiKMbkb2ASg8AGF8ZiZTzUS5Q,30
9
- jupyterlite_simple_cors_proxy-0.1.11.dist-info/RECORD,,