jupyterlite-simple-cors-proxy 0.1.8__tar.gz → 0.1.9__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (14) hide show
  1. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/PKG-INFO +16 -1
  2. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/README.md +15 -0
  3. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/jupyterlite_simple_cors_proxy/__init__.py +1 -1
  4. jupyterlite_simple_cors_proxy-0.1.9/jupyterlite_simple_cors_proxy/proxy.py +83 -0
  5. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/jupyterlite_simple_cors_proxy.egg-info/PKG-INFO +16 -1
  6. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/setup.py +1 -1
  7. jupyterlite_simple_cors_proxy-0.1.8/jupyterlite_simple_cors_proxy/proxy.py +0 -52
  8. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/LICENSE +0 -0
  9. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/jupyterlite_simple_cors_proxy/fastf1_proxy.py +0 -0
  10. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/jupyterlite_simple_cors_proxy.egg-info/SOURCES.txt +0 -0
  11. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/jupyterlite_simple_cors_proxy.egg-info/dependency_links.txt +0 -0
  12. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/jupyterlite_simple_cors_proxy.egg-info/requires.txt +0 -0
  13. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/jupyterlite_simple_cors_proxy.egg-info/top_level.txt +0 -0
  14. {jupyterlite_simple_cors_proxy-0.1.8 → jupyterlite_simple_cors_proxy-0.1.9}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: jupyterlite-simple-cors-proxy
3
- Version: 0.1.8
3
+ Version: 0.1.9
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
@@ -65,3 +65,18 @@ The `robust_get_request()` will first try a simple request, then a proxied reque
65
65
  - Simple CORS proxy wrapper
66
66
  - Requests response object
67
67
  - Support for URL parameters
68
+
69
+ ## `fastf1` cors proxy
70
+
71
+ A monkey patch for `fastf1` is provided as:
72
+
73
+ ```python
74
+ import fast f1
75
+ from jupyterlite_simple_cors_proxy.fastf1_proxy import enable_cors_proxy
76
+
77
+ enable_cors_proxy(
78
+ # domains=["api.formula1.com", "livetiming.formula1.com"],
79
+ # debug=True,
80
+ # proxy_url="https://corsproxy.io/",
81
+ )
82
+ ```
@@ -41,3 +41,18 @@ The `robust_get_request()` will first try a simple request, then a proxied reque
41
41
  - Simple CORS proxy wrapper
42
42
  - Requests response object
43
43
  - Support for URL parameters
44
+
45
+ ## `fastf1` cors proxy
46
+
47
+ A monkey patch for `fastf1` is provided as:
48
+
49
+ ```python
50
+ import fast f1
51
+ from jupyterlite_simple_cors_proxy.fastf1_proxy import enable_cors_proxy
52
+
53
+ enable_cors_proxy(
54
+ # domains=["api.formula1.com", "livetiming.formula1.com"],
55
+ # debug=True,
56
+ # proxy_url="https://corsproxy.io/",
57
+ )
58
+ ```
@@ -3,5 +3,5 @@ from .proxy import cors_proxy_get, robust_get_request, xurl, furl
3
3
 
4
4
  # from .fastf1_proxy import enable_cors_proxy as fastf1_cors_proxy
5
5
 
6
- __version__ = "0.1.8"
6
+ __version__ = "0.1.9"
7
7
  __all__ = ["cors_proxy_get", "robust_get_request", "xurl", "furl"]
@@ -0,0 +1,83 @@
1
+ # File: simple_cors_proxy/proxy.py
2
+ from urllib.parse import urlencode, quote
3
+ import requests
4
+ import requests_cache
5
+ import io
6
+ import platform
7
+ from typing import Optional, Union
8
+
9
+ PLATFORM = platform.system().lower()
10
+
11
+ class CorsProxy:
12
+ """CORS Proxy with optional caching support."""
13
+
14
+ def __init__(self, use_cache: bool = False, **cache_kwargs):
15
+ """
16
+ Initialize the CORS proxy.
17
+
18
+ Args:
19
+ use_cache: Whether to enable request caching
20
+ **cache_kwargs: Arguments passed to requests_cache.CachedSession
21
+ (e.g., cache_name, backend, expire_after)
22
+ """
23
+ if use_cache:
24
+ # Set some sensible defaults if not provided
25
+ if 'cache_name' not in cache_kwargs:
26
+ cache_kwargs['cache_name'] = 'cors_proxy_cache'
27
+ if 'expire_after' not in cache_kwargs:
28
+ cache_kwargs['expire_after'] = 3600 # 1 hour default
29
+ self.session = requests_cache.CachedSession(**cache_kwargs)
30
+ else:
31
+ self.session = requests
32
+
33
+ def xurl(self, url: str, params: Optional[dict] = None, force: bool = False) -> str:
34
+ """Generate a proxied URL."""
35
+ if PLATFORM == "emscripten" or force:
36
+ if params:
37
+ url = f"{url}?{urlencode(params)}"
38
+ url = f"https://corsproxy.io/{quote(url)}"
39
+ return url
40
+
41
+ def furl(self, url: str, params: Optional[dict] = None, force: bool = False) -> io.BytesIO:
42
+ """Return file like object after calling the proxied URL."""
43
+ r = self.cors_proxy_get(url, params, force)
44
+ return io.BytesIO(r.content)
45
+
46
+ def cors_proxy_get(self, url: str, params: Optional[dict] = None, force: bool = False) -> requests.Response:
47
+ """
48
+ CORS proxy for GET resources with requests-like response.
49
+
50
+ Args:
51
+ url: The URL to fetch
52
+ params: Query parameters to include
53
+ force: Force using the proxy even on non-emscripten platforms
54
+
55
+ Returns:
56
+ A requests response object.
57
+ """
58
+ proxy_url = self.xurl(url, params, force)
59
+ return self.session.get(proxy_url)
60
+
61
+ def robust_get_request(self, url: str, params: Optional[dict] = None) -> requests.Response:
62
+ """
63
+ Try to make a simple request else fall back to a proxy.
64
+ """
65
+ try:
66
+ r = self.session.get(url, params=params)
67
+ except:
68
+ r = self.cors_proxy_get(url, params=params)
69
+ return r
70
+
71
+ # Create default instance
72
+ _default_proxy = CorsProxy()
73
+
74
+ # Legacy function-based interface
75
+ xurl = _default_proxy.xurl
76
+ furl = _default_proxy.furl
77
+ cors_proxy_get = _default_proxy.cors_proxy_get
78
+ robust_get_request = _default_proxy.robust_get_request
79
+
80
+ # Convenience function to create a cached proxy
81
+ def create_cached_proxy(**cache_kwargs):
82
+ """Create a new CorsProxy instance with caching enabled."""
83
+ return CorsProxy(use_cache=True, **cache_kwargs)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: jupyterlite-simple-cors-proxy
3
- Version: 0.1.8
3
+ Version: 0.1.9
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
@@ -65,3 +65,18 @@ The `robust_get_request()` will first try a simple request, then a proxied reque
65
65
  - Simple CORS proxy wrapper
66
66
  - Requests response object
67
67
  - Support for URL parameters
68
+
69
+ ## `fastf1` cors proxy
70
+
71
+ A monkey patch for `fastf1` is provided as:
72
+
73
+ ```python
74
+ import fast f1
75
+ from jupyterlite_simple_cors_proxy.fastf1_proxy import enable_cors_proxy
76
+
77
+ enable_cors_proxy(
78
+ # domains=["api.formula1.com", "livetiming.formula1.com"],
79
+ # debug=True,
80
+ # proxy_url="https://corsproxy.io/",
81
+ )
82
+ ```
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
3
3
 
4
4
  setup(
5
5
  name="jupyterlite-simple-cors-proxy",
6
- version="0.1.8",
6
+ version="0.1.9",
7
7
  packages=find_packages(),
8
8
  install_requires=[
9
9
  "requests",
@@ -1,52 +0,0 @@
1
- # File: simple_cors_proxy/proxy.py
2
- from urllib.parse import urlencode, quote
3
- import requests
4
- import io
5
-
6
- import platform
7
- PLATFORM = platform.system().lower()
8
-
9
- def xurl(url, params=None, force=False):
10
- """Generate a proxied URL."""
11
- if PLATFORM=="emscripten" or force:
12
- if params:
13
- url = f"{url}?{urlencode(params)}"
14
- url = f"https://corsproxy.io/{quote(url)}"
15
-
16
- return url
17
-
18
- def furl(url, params=None, force=False):
19
- """Return file like object after calling the proxied URL."""
20
- r = cors_proxy_get(url, params, force)
21
-
22
- # Return a file-like object from the JSON string
23
- return io.BytesIO(r.content)
24
-
25
-
26
- def cors_proxy_get(url, params=None, force=False):
27
- """
28
- CORS proxy for GET resources with requests-like response.
29
-
30
- Args:
31
- url (str): The URL to fetch
32
- params (dict, optional): Query parameters to include
33
-
34
- Returns:
35
- A requests response object.
36
- """
37
- proxy_url = xurl(url, params, force)
38
-
39
- # Do a simple requests get and
40
- # just pass through the entire response object
41
- return requests.get(proxy_url)
42
-
43
-
44
- def robust_get_request(url, params=None):
45
- """
46
- Try to make a simple request else fall back to a proxy.
47
- """
48
- try:
49
- r = requests.get(url, params=params)
50
- except:
51
- r = cors_proxy_get(url, params=params)
52
- return r