bcicen / devpi-tools

Small Python library for interacting with devpi servers via web API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

if .json() fails, error is not captured nicely

jkkr2 opened this issue · comments

commented

Hi, thanks for providing the devpi-tools. I found an issue working with devpi-tool in a network environment, where a firewall is in charge and cannot be deactivated. In __init__.py in class DevpiClient in the get_json method in line 24
res = self.request(method, url, headers=headers, verify=verify_ssl).json()
I get a json parsing error, beacuse our firewall is sending back a blockpage in html in self.request(method, url, headers=headers, verify=verify_ssl).text. This error is captured downstrem in the json parser, but it would be nice to cover it in get_json.

This is my modified version:

class DevpiClient(requests.Session):
    """ A very small client for connecting to devpi web API """
    def __init__(self, base_url, disable_ssl_warning=False):
        super(DevpiClient, self).__init__()
        self.base_url = base_url
        self.disable_ssl_warning = disable_ssl_warning

    def get_json(self, path, method='GET', **params):
        url = self.base_url + path
        headers = {'Accept': 'application/json'}
        verify_ssl = True

        if self.disable_ssl_warning:
            requests.packages.urllib3.disable_warnings()
            verify_ssl = False
        res_raw = self.request(method, url, headers=headers, verify=verify_ssl)
        try:
            res = res_raw.json()
        except:
            raise DevpiApiError(f'Result is not in json format: {res_raw.text}')
        if 'message' in res.keys():
            raise DevpiApiError(res['message'])

        return res['result']

For shure not elegant, but maybe you can implement something like this.

BR,
Johannes