gpkgstatus.utils.url_reader
A URL Reader module that reads a JSON url and saves its response as a file in tempdir.
Raises:
- URLReader._StatusException: Manual exception raised if HTTP status code is not OK (200).
1"""A URL Reader module that reads a JSON url and saves its response \ 2 as a file in tempdir. 3 4Raises: 5 URLReader._StatusException: Manual exception raised if \ 6 HTTP status code is not OK (200). 7 8""" 9import json 10import logging 11import sys 12 13from os.path import join 14from tempfile import gettempdir 15 16import requests 17 18from termcolor import colored 19 20 21# pylint:disable=too-few-public-methods 22class URLReader: 23 """A Custom URL Reader class created using requests package. 24 25 The class initializer uses a HEAD request to determine if given 26 JSON url exists. 27 28 Raises: 29 URLReader._StatusException: Manual exception raised if \ 30 HTTP status code is not OK (200). 31 """ 32 33 class _StatusException(Exception): 34 pass 35 36 _url: str = "" 37 38 def __init__(self, _url: str): 39 try: 40 response = requests.head(_url, timeout=15) 41 42 if response.status_code != 200: 43 print( 44 colored("Error: Website isn't returning HTTP Status Code (200 OK)") 45 ) 46 raise URLReader._StatusException() 47 48 self._url = _url 49 logging.info("URL %s returned OK", _url) 50 51 except requests.ConnectionError: 52 print(colored(f"Error: Could not connect to {_url}", "red")) 53 sys.exit(1) 54 55 def _load_json(self): 56 """Loads the given JSON url. 57 58 If corresponding url is not a JSON url or if JSON decoding fails, then 59 the program halts with exit status 1 and immediately states that 60 there is an issue parsing JSON from corresponding url and might 61 not be a valid JSON url. 62 63 Returns: 64 Any: Returns JSON response of corresponding JSON url. 65 """ 66 try: 67 response = requests.get(self._url, timeout=15) 68 logging.info("GET Request from %s succeeded", self._url) 69 70 return response.json() 71 except requests.exceptions.JSONDecodeError: 72 print( 73 colored( 74 "Error: There is an issue parsing JSON from corresponding website. \ 75 Check if given URL is valid JSON URL", 76 "red", 77 ) 78 ) 79 sys.exit(1) 80 81 # pylint:disable=duplicate-code 82 def save_as_file(self, filename: str, data: str = ""): 83 """Saves JSON response in a file. 84 85 If PermissionError is raised, then the program halts with exit 86 status 1 and immediately states that permission is denied. 87 88 Args: 89 filename (str): Name of the file. 90 data (str, optional): Corresponding JSON response/file. 91 If no file is given, it takes JSON response as data from 92 corresponding url. 93 """ 94 if not data: 95 data = self._load_json() 96 logging.info("JSON loaded successfully from URL") 97 98 temp_file = join(gettempdir(), filename) 99 100 try: 101 with open(temp_file, "w", encoding="utf-8") as file: 102 json.dump(data, file, skipkeys=True, indent=4) 103 104 logging.info("JSON stored in %s", filename) 105 106 except PermissionError: 107 print( 108 colored( 109 "Error: Permission denied. Please check temp directory permissions.", 110 "red", 111 ) 112 ) 113 sys.exit(1)
class
URLReader:
23class URLReader: 24 """A Custom URL Reader class created using requests package. 25 26 The class initializer uses a HEAD request to determine if given 27 JSON url exists. 28 29 Raises: 30 URLReader._StatusException: Manual exception raised if \ 31 HTTP status code is not OK (200). 32 """ 33 34 class _StatusException(Exception): 35 pass 36 37 _url: str = "" 38 39 def __init__(self, _url: str): 40 try: 41 response = requests.head(_url, timeout=15) 42 43 if response.status_code != 200: 44 print( 45 colored("Error: Website isn't returning HTTP Status Code (200 OK)") 46 ) 47 raise URLReader._StatusException() 48 49 self._url = _url 50 logging.info("URL %s returned OK", _url) 51 52 except requests.ConnectionError: 53 print(colored(f"Error: Could not connect to {_url}", "red")) 54 sys.exit(1) 55 56 def _load_json(self): 57 """Loads the given JSON url. 58 59 If corresponding url is not a JSON url or if JSON decoding fails, then 60 the program halts with exit status 1 and immediately states that 61 there is an issue parsing JSON from corresponding url and might 62 not be a valid JSON url. 63 64 Returns: 65 Any: Returns JSON response of corresponding JSON url. 66 """ 67 try: 68 response = requests.get(self._url, timeout=15) 69 logging.info("GET Request from %s succeeded", self._url) 70 71 return response.json() 72 except requests.exceptions.JSONDecodeError: 73 print( 74 colored( 75 "Error: There is an issue parsing JSON from corresponding website. \ 76 Check if given URL is valid JSON URL", 77 "red", 78 ) 79 ) 80 sys.exit(1) 81 82 # pylint:disable=duplicate-code 83 def save_as_file(self, filename: str, data: str = ""): 84 """Saves JSON response in a file. 85 86 If PermissionError is raised, then the program halts with exit 87 status 1 and immediately states that permission is denied. 88 89 Args: 90 filename (str): Name of the file. 91 data (str, optional): Corresponding JSON response/file. 92 If no file is given, it takes JSON response as data from 93 corresponding url. 94 """ 95 if not data: 96 data = self._load_json() 97 logging.info("JSON loaded successfully from URL") 98 99 temp_file = join(gettempdir(), filename) 100 101 try: 102 with open(temp_file, "w", encoding="utf-8") as file: 103 json.dump(data, file, skipkeys=True, indent=4) 104 105 logging.info("JSON stored in %s", filename) 106 107 except PermissionError: 108 print( 109 colored( 110 "Error: Permission denied. Please check temp directory permissions.", 111 "red", 112 ) 113 ) 114 sys.exit(1)
A Custom URL Reader class created using requests package.
The class initializer uses a HEAD request to determine if given JSON url exists.
Raises:
- URLReader._StatusException: Manual exception raised if HTTP status code is not OK (200).
URLReader(_url: str)
39 def __init__(self, _url: str): 40 try: 41 response = requests.head(_url, timeout=15) 42 43 if response.status_code != 200: 44 print( 45 colored("Error: Website isn't returning HTTP Status Code (200 OK)") 46 ) 47 raise URLReader._StatusException() 48 49 self._url = _url 50 logging.info("URL %s returned OK", _url) 51 52 except requests.ConnectionError: 53 print(colored(f"Error: Could not connect to {_url}", "red")) 54 sys.exit(1)
def
save_as_file(self, filename: str,
data: str = ''):
83 def save_as_file(self, filename: str, data: str = ""): 84 """Saves JSON response in a file. 85 86 If PermissionError is raised, then the program halts with exit 87 status 1 and immediately states that permission is denied. 88 89 Args: 90 filename (str): Name of the file. 91 data (str, optional): Corresponding JSON response/file. 92 If no file is given, it takes JSON response as data from 93 corresponding url. 94 """ 95 if not data: 96 data = self._load_json() 97 logging.info("JSON loaded successfully from URL") 98 99 temp_file = join(gettempdir(), filename) 100 101 try: 102 with open(temp_file, "w", encoding="utf-8") as file: 103 json.dump(data, file, skipkeys=True, indent=4) 104 105 logging.info("JSON stored in %s", filename) 106 107 except PermissionError: 108 print( 109 colored( 110 "Error: Permission denied. Please check temp directory permissions.", 111 "red", 112 ) 113 ) 114 sys.exit(1)
Saves JSON response in a file.
If PermissionError is raised, then the program halts with exit status 1 and immediately states that permission is denied.
Arguments:
- filename (str): Name of the file.
- data (str, optional): Corresponding JSON response/file.
- If no file is given, it takes JSON response as data from
- corresponding url.