Created
May 13, 2023 19:23
-
-
Save ShabbirHasan1/137d824ab480438dc8d99a19c3bb1713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from io import BytesIO | |
| from http import HTTPMethod | |
| from json import JSONDecoder | |
| from requests import Timeout | |
| from requests import HTTPError | |
| from requests import ConnectionError | |
| from requests import JSONDecodeError | |
| from requests import RequestException | |
| from requests import TooManyRedirects | |
| try: | |
| import pandas as pd | |
| except (ImportError, ModuleNotFoundError): | |
| import os, sys | |
| os.system(f"{sys.executable} -m pip install -U pandas") | |
| finally: | |
| import pandas as pd | |
| HEADERS = {"Content-Type": "application/json"} | |
| def get_data( | |
| url: str, | |
| method: HTTPMethod = HTTPMethod.GET, | |
| params: Dict[str, str] = dict(), | |
| data: Dict[str, str] = dict(), | |
| no_of_retries: int = 7, | |
| ) -> pd.DataFrame: | |
| global session | |
| while 1 <= no_of_retries <= 7: | |
| try: | |
| if method in (HTTPMethod.GET, HTTPMethod.DELETE): | |
| r = session.send( | |
| requests.Request( | |
| method=method, url=url, params=params | |
| ).prepare() | |
| ) | |
| else: | |
| r = session.send( | |
| requests.Request( | |
| method=method, url=url, params=params, json=data | |
| ).prepare() | |
| ) | |
| r.raise_for_status() | |
| except ConnectionError as errc: | |
| print("Error Connecting:", errc, "Retrying...", sep=" | ") | |
| no_of_retries -= 1 | |
| except HTTPError as errh: | |
| print("Http Error:", errh, "Retrying...", sep=" | ") | |
| no_of_retries -= 1 | |
| except Timeout as errt: | |
| print("Timeout Error:", errt, "Retrying...", sep=" | ") | |
| no_of_retries -= 1 | |
| except TooManyRedirects as errto: | |
| print("TooManyRedirects Error:", errto, "Retrying...", sep=" | ") | |
| no_of_retries -= 1 | |
| except RequestException as err: | |
| print("Some Other Error:", err, "Retrying...", sep=" | ") | |
| no_of_retries -= 1 | |
| else: | |
| try: | |
| data = pd.read_csv(BytesIO(r.content)) | |
| except Exception as err: | |
| print( | |
| "While Converting Response dato to DF", | |
| f"Met With an Exception: {err}", | |
| sep="\n", | |
| ) | |
| no_of_retries -= 1 | |
| else: | |
| return data | |
| if __name__ == "__main__": | |
| session = requests.session() | |
| session.headers.update(HEADERS) | |
| data = get_data("URL") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment