forked from kenzuya/unshackle
fix(core): add retry and timeout to get_ip_info requests
- Import HTTPAdapter from requests.adapters - Configure session adapters for http and https with max_retries=3 - Add timeout of 1 second to the get request in get_ip_info - Ensure retries and timeout improve request reliability and responsiveness
This commit is contained in:
@@ -20,6 +20,7 @@ import requests
|
|||||||
from construct import ValidationError
|
from construct import ValidationError
|
||||||
from langcodes import Language, closest_match
|
from langcodes import Language, closest_match
|
||||||
from pymp4.parser import Box
|
from pymp4.parser import Box
|
||||||
|
from requests.adapters import HTTPAdapter
|
||||||
from unidecode import unidecode
|
from unidecode import unidecode
|
||||||
|
|
||||||
from unshackle.core.cacher import Cacher
|
from unshackle.core.cacher import Cacher
|
||||||
@@ -241,7 +242,10 @@ def get_ip_info(session: Optional[requests.Session] = None) -> dict:
|
|||||||
If you provide a Requests Session with a Proxy, that proxies IP information
|
If you provide a Requests Session with a Proxy, that proxies IP information
|
||||||
is what will be returned.
|
is what will be returned.
|
||||||
"""
|
"""
|
||||||
return (session or requests.Session()).get("https://ipinfo.io/json").json()
|
request = session or requests.Session()
|
||||||
|
request.adapters["http://"] = HTTPAdapter(max_retries=3)
|
||||||
|
request.adapters["https://"] = HTTPAdapter(max_retries=3)
|
||||||
|
return request.get("https://ipinfo.io/json", timeout=1).json()
|
||||||
|
|
||||||
|
|
||||||
def get_cached_ip_info(session: Optional[requests.Session] = None) -> Optional[dict]:
|
def get_cached_ip_info(session: Optional[requests.Session] = None) -> Optional[dict]:
|
||||||
|
|||||||
Reference in New Issue
Block a user