gpkgstatus.utils.url_reader API documentation

# 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).

View Source

      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
     15from time import sleep
     16
     17import requests
     18
     19from termcolor import colored
     20
     21
     22# pylint:disable=too-few-public-methods
     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            sleep(1)
     70            logging.info("GET Request from %s succeeded", self._url)
     71
     72            return response.json()
     73        except requests.exceptions.JSONDecodeError:
     74            print(
     75                colored(
     76                    "Error: There is an issue parsing JSON from corresponding website. \
     77                        Check if given URL is valid JSON URL",
     78                    "red",
     79                )
     80            )
     81            sys.exit(1)
     82
     83    # pylint:disable=duplicate-code
     84    def save_as_file(self, filename: str, data: str = ""):
     85        """Saves JSON response in a file.
     86
     87        If PermissionError is raised, then the program halts with exit
     88        status 1 and immediately states that permission is denied.
     89
     90        Args:
     91            filename (str): Name of the file.
     92            data (str, optional): Corresponding JSON response/file.
     93            If no file is given, it takes JSON response as data from
     94            corresponding url.
     95        """
     96        if not data:
     97            data = self._load_json()
     98            logging.info("JSON loaded successfully from URL")
     99
    100        temp_file = join(gettempdir(), filename)
    101
    102        try:
    103            with open(temp_file, "w", encoding="utf-8") as file:
    104                json.dump(data, file, skipkeys=True, indent=4)
    105
    106            logging.info("JSON stored in %s", filename)
    107
    108        except PermissionError:
    109            print(
    110                colored(
    111                    "Error: Permission denied. Please check temp directory permissions.",
    112                    "red",
    113                )
    114            )
    115            sys.exit(1)

class URLReader: View Source

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

    40    def __init__(self, _url: str):
    41        try:
    42            response = requests.head(_url, timeout=15)
    43
    44            if response.status_code != 200:
    45                print(
    46                    colored("Error: Website isn't returning HTTP Status Code (200 OK)")
    47                )
    48                raise URLReader._StatusException()
    49
    50            self._url = _url
    51            logging.info("URL %s returned OK", _url)
    52
    53        except requests.ConnectionError:
    54            print(colored(f"Error: Could not connect to {_url}", "red"))
    55            sys.exit(1)

def save_as_file(self, filename: str, data: str = ''): View Source

     85    def save_as_file(self, filename: str, data: str = ""):
     86        """Saves JSON response in a file.
     87
     88        If PermissionError is raised, then the program halts with exit
     89        status 1 and immediately states that permission is denied.
     90
     91        Args:
     92            filename (str): Name of the file.
     93            data (str, optional): Corresponding JSON response/file.
     94            If no file is given, it takes JSON response as data from
     95            corresponding url.
     96        """
     97        if not data:
     98            data = self._load_json()
     99            logging.info("JSON loaded successfully from URL")
    100
    101        temp_file = join(gettempdir(), filename)
    102
    103        try:
    104            with open(temp_file, "w", encoding="utf-8") as file:
    105                json.dump(data, file, skipkeys=True, indent=4)
    106
    107            logging.info("JSON stored in %s", filename)
    108
    109        except PermissionError:
    110            print(
    111                colored(
    112                    "Error: Permission denied. Please check temp directory permissions.",
    113                    "red",
    114                )
    115            )
    116            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.