Python requests.exceptions.HTTPError() Examples

The following are 30 code examples of requests.exceptions.HTTPError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module requests.exceptions , or try the search function .
Example #1
Source File: test_deployment.py    From Dallinger with MIT License 7 votes vote down vote up
def test_failure(self, debugger):
        from requests.exceptions import HTTPError

        with mock.patch("dallinger.deployment.HerokuLocalWrapper"):
            with mock.patch("dallinger.deployment.requests.post") as mock_post:
                mock_post.return_value = mock.Mock(
                    ok=False,
                    json=mock.Mock(return_value={"message": "msg!"}),
                    raise_for_status=mock.Mock(side_effect=HTTPError),
                    status_code=500,
                    text="Failure",
                )
                debugger.run()

        # Only one launch attempt should be made in debug mode
        debugger.out.error.assert_has_calls(
            [
                mock.call("Error accessing http://0.0.0.0:5000/launch (500):\nFailure"),
                mock.call("Experiment launch failed, check web dyno logs for details."),
                mock.call("msg!"),
            ]
        ) 
Example #2
Source File: test_databricks.py    From airflow with Apache License 2.0 7 votes vote down vote up
def test_do_api_call_waits_between_retries(self, mock_sleep):
        retry_delay = 5
        self.hook = DatabricksHook(retry_delay=retry_delay)

        for exception in [requests_exceptions.ConnectionError,
                          requests_exceptions.SSLError,
                          requests_exceptions.Timeout,
                          requests_exceptions.ConnectTimeout,
                          requests_exceptions.HTTPError]:
            with mock.patch('airflow.providers.databricks.hooks.databricks.requests') as mock_requests:
                with mock.patch.object(self.hook.log, 'error'):
                    mock_sleep.reset_mock()
                    setup_mock_requests(mock_requests, exception)

                    with self.assertRaises(AirflowException):
                        self.hook._do_api_call(SUBMIT_RUN_ENDPOINT, {})

                    self.assertEqual(len(mock_sleep.mock_calls), self.hook.retry_limit - 1)
                    calls = [
                        mock.call(retry_delay),
                        mock.call(retry_delay)
                    ]
                    mock_sleep.assert_has_calls(calls) 
Example #3
Source File: http.py    From osbs-client with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def request(self, url, *args, **kwargs):
        try:
            stream = HttpStream(url, *args, verbose=self.verbose, **kwargs)
            if kwargs.get('stream', False):
                return stream

            with stream as s:
                content = s.req.content
                return HttpResponse(s.status_code, s.headers, content)
        # Timeout will catch both ConnectTimout and ReadTimeout
        except (RetryError, Timeout) as ex:
            raise OsbsNetworkException(url, str(ex), '',
                                       cause=ex, traceback=sys.exc_info()[2])
        except HTTPError as ex:
            raise OsbsNetworkException(url, str(ex), ex.response.status_code,
                                       cause=ex, traceback=sys.exc_info()[2])
        except Exception as ex:
            raise OsbsException(cause=ex, traceback=sys.exc_info()[2]) 
Example #4
Source File: isUserHomeDeterminer.py    From appdaemon-scripts with MIT License 6 votes vote down vote up
def turn_off_callback(self, kwargs):
        """This is needed because the turn_off command can result in a HTTP 503 when homeassistant is restarting"""
        try:
            self.turn_off(kwargs["turn_off_entity"])
        except HTTPError as exception:
            self.log(
                "Error trying to turn off entity. Will try again in 1s. Error: {}".format(
                    exception
                ),
                level="WARNING",
            )
            self.timer_handle_list.append(
                self.run_in(
                    self.turn_off_callback, 1, turn_off_entity=kwargs["turn_off_entity"]
                )
            ) 
Example #5
Source File: alpaca.py    From pylivetrader with Apache License 2.0 6 votes vote down vote up
def skip_http_error(statuses):
    """
    A decorator to wrap with try..except to swallow
    specific HTTP errors.

    @skip_http_error((404, 503))
    def fetch():
        ...
    """

    assert isinstance(statuses, tuple)

    def decorator(func):
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except HTTPError as e:
                status_code = e.response.status_code
                if status_code in statuses:
                    log.warn(str(e))
                else:
                    raise
        return wrapper
    return decorator 
Example #6
Source File: isUserHomeDeterminer.py    From appdaemon-scripts with MIT License 6 votes vote down vote up
def turn_on_callback(self, kwargs):
        """This is needed because the turn_on command can result in a HTTP 503 when homeassistant is restarting"""
        try:
            self.turn_on(kwargs["turn_on_entity"])
        except HTTPError as exception:
            self.log(
                "Error trying to turn on entity. Will try again in 1s. Error: {}".format(
                    exception
                ),
                level="WARNING",
            )
            self.timer_handle_list.append(
                self.run_in(
                    self.turn_on_callback, 1, turn_on_entity=kwargs["turn_on_entity"]
                )
            ) 
Example #7
Source File: test_databricks.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_do_api_call_succeeds_after_retrying(self):
        for exception in [requests_exceptions.ConnectionError,
                          requests_exceptions.SSLError,
                          requests_exceptions.Timeout,
                          requests_exceptions.ConnectTimeout,
                          requests_exceptions.HTTPError]:
            with mock.patch('airflow.providers.databricks.hooks.databricks.requests') as mock_requests:
                with mock.patch.object(self.hook.log, 'error') as mock_errors:
                    setup_mock_requests(
                        mock_requests,
                        exception,
                        error_count=2,
                        response_content={'run_id': '1'}
                    )

                    response = self.hook._do_api_call(SUBMIT_RUN_ENDPOINT, {})

                    self.assertEqual(mock_errors.call_count, 2)
                    self.assertEqual(response, {'run_id': '1'}) 
Example #8
Source File: restclient.py    From tiingo-python with MIT License 6 votes vote down vote up
def _request(self, method, url, **kwargs):
        """Make HTTP request and return response object

            Args:
                method (str): GET, POST, PUT, DELETE
                url (str): path appended to the base_url to create request
                **kwargs: passed directly to a requests.request object
        """
        resp = self._session.request(method,
                                     '{}/{}'.format(self._base_url, url),
                                     headers=self._headers,
                                     **kwargs)

        try:
            resp.raise_for_status()
        except HTTPError as e:
            logging.error(resp.content)
            raise RestClientError(e)

        return resp 
Example #9
Source File: api.py    From insightconnect-plugins with MIT License 6 votes vote down vote up
def call_api(self, path: str, params: dict = None) -> dict:
        if params is None:
            params = {}

        api_url = self.base_url + path

        headers = {
            "Authorization": f"basic {self.token}"
        }

        try:
            response = request("GET", self.base_url + path, params=komand.helper.clean(params), headers=headers)
            response.raise_for_status()
            return komand.helper.clean(response.json())
        except HTTPError as httpError:
            raise PluginException(
                cause=f"Failed to get a valid response from AttackerKB at endpoint {api_url}",
                assistance=f"Response was {httpError.response.text}",
                data=httpError
            ) 
Example #10
Source File: turnOffBarAfterRestart.py    From appdaemon-scripts with MIT License 6 votes vote down vote up
def turn_green_callback(self, kwargs):
        """This is needed because the turn_on command can result in a HTTP 503 when homeassistant is restarting"""
        try:
            self.call_service(
                "light/turn_on",
                entity_id=self.light,
                rgb_color=[0, 255, 0],
                white_value=0,
            )
            self.log("Turning {} green".format(self.friendly_name(self.light)))
            self.timer_handle_list.append(self.run_in(self.turn_off_callback, 5))
        except HTTPError as exception:
            self.log(
                "Error trying to turn on entity. Will try again in 1s. Error: {}".format(
                    exception
                ),
                level="WARNING",
            )
            self.timer_handle_list.append(self.run_in(self.turn_green_callback, 1)) 
Example #11
Source File: onelight.py    From ibllib with MIT License 6 votes vote down vote up
def figshare_request(
        endpoint=None, data=None, method='GET', url=None,
        binary=False, error_level=logging.ERROR):
    """Perform a REST request against the figshare API."""
    headers = {'Authorization': 'token ' + repository().get('token', '')}
    if data is not None and not binary:
        data = json.dumps(data)
    response = requests.request(
        method, url or _FIGSHARE_BASE_URL.format(endpoint=endpoint), headers=headers, data=data)
    try:
        response.raise_for_status()
        try:
            data = json.loads(response.content)
        except ValueError:
            data = response.content
    except HTTPError as error:
        logger.log(error_level, error)
        raise error
    return data 
Example #12
Source File: nfsession.py    From plugin.video.netflix with MIT License 6 votes vote down vote up
def parental_control_data(self, password):
        # Ask to the service if password is right and get the PIN status
        from requests import exceptions
        profile_guid = g.LOCAL_DB.get_active_profile_guid()
        try:
            response = self._post('profile_hub',
                                  data={'destination': 'contentRestrictions',
                                        'guid': profile_guid,
                                        'password': password,
                                        'task': 'auth'})
            if response.get('status') != 'ok':
                common.warn('Parental control status issue: {}', response)
                raise MissingCredentialsError
        except exceptions.HTTPError as exc:
            if exc.response.status_code == 500:
                # This endpoint raise HTTP error 500 when the password is wrong
                raise MissingCredentialsError
            raise
        # Warning - parental control levels vary by country or region, no fixed values can be used
        # Note: The language of descriptions change in base of the language of selected profile
        response_content = self._get('restrictions', data={'password': password}, append_to_address=profile_guid)
        extracted_content = website.extract_parental_control_data(response_content, response['maturity'])
        response['profileInfo']['profileName'] = website.parse_html(response['profileInfo']['profileName'])
        extracted_content['data'] = response
        return extracted_content 
Example #13
Source File: forecast.py    From pvlib-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def connect_to_catalog(self):
        self.catalog = TDSCatalog(self.catalog_url)
        self.fm_models = TDSCatalog(
            self.catalog.catalog_refs[self.model_type].href)
        self.fm_models_list = sorted(list(self.fm_models.catalog_refs.keys()))

        try:
            model_url = self.fm_models.catalog_refs[self.model_name].href
        except ParseError:
            raise ParseError(self.model_name + ' model may be unavailable.')

        try:
            self.model = TDSCatalog(model_url)
        except HTTPError:
            try:
                self.model = TDSCatalog(model_url)
            except HTTPError:
                raise HTTPError(self.model_name + ' model may be unavailable.')

        self.datasets_list = list(self.model.datasets.keys())
        self.set_dataset()
        self.connected = True 
Example #14
Source File: test_zotero.py    From dissemin with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_http_status_error(self, caplog):
        """
        If HTTP status, expect NONE and logging entry
        """
        doi = '10.100/spam'
        responses.add(
            responses.GET,
            'https://{}/zotero/{}'.format(settings.DOI_PROXY_DOMAIN, doi),
            status=500
            )
        r = fetch_zotero_by_DOI(doi)
        assert r is None
        log_entry = caplog.records[0]
        assert log_entry.name == 'dissemin.backend.zotero'
        assert log_entry.levelname == 'ERROR'
        assert isinstance(log_entry.msg, HTTPError) 
Example #15
Source File: network_helper.py    From f5-openstack-agent with Apache License 2.0 6 votes vote down vote up
def delete_all_fdb_entries(
            self,
            bigip,
            tunnel_name,
            partition=const.DEFAULT_PARTITION):
        """Delete all fdb entries."""
        try:
            t = bigip.tm.net.fdb.tunnels.tunnel
            obj = t.load(name=tunnel_name, partition=partition)
            obj.modify(records=None)
        except HTTPError as err:
            LOG.error("Error deleting all fdb entries %s. "
                      "Repsponse status code: %s. Response "
                      "message: %s." % (tunnel_name,
                                        err.response.status_code,
                                        err.message)) 
Example #16
Source File: config_manager.py    From python-sdk with Apache License 2.0 6 votes vote down vote up
def _handle_response(self, response):
        """ Helper method to handle response containing datafile.

        Args:
            response: requests.Response
        """
        try:
            response.raise_for_status()
        except requests_exceptions.HTTPError as err:
            self.logger.error('Fetching datafile from {} failed. Error: {}'.format(self.datafile_url, str(err)))
            return

        # Leave datafile and config unchanged if it has not been modified.
        if response.status_code == http_status_codes.not_modified:
            self.logger.debug('Not updating config as datafile has not updated since {}.'.format(self.last_modified))
            return

        self.set_last_modified(response.headers)
        self._set_config(response.content) 
Example #17
Source File: yaml_ext.py    From custodia with GNU General Public License v3.0 6 votes vote down vote up
def demo():
    constructor = CustodiaSimpleConstructor(
        'http+unix://%2E%2Fserver_socket/secrets'
    )
    constructor.client.headers['REMOTE_USER'] = 'user'

    # create entries
    try:
        c = constructor.client.list_container('test')
    except HTTPError:
        constructor.client.create_container('test')
        c = []
    if 'key' not in c:
        constructor.client.set_secret('test/key', 'secret password')

    yaml.add_constructor(CustodiaSimpleConstructor.yaml_tag,
                         constructor)
    yaml_str = 'password: !custodia/simple test/key'
    print(yaml_str)
    result = yaml.load(yaml_str)
    print(result) 
Example #18
Source File: test_pseudo_agentdb_binding.py    From networking-odl with Apache License 2.0 6 votes vote down vote up
def _get_raised_response(self, json_data, status_code):

        class MockHTTPError(HTTPError):
            def __init__(self, json_data, status_code):
                self.json_data = json_data
                self.status_code = status_code
                self.response = self

        class MockResponse(object):
            def __init__(self, json_data, status_code):
                self.raise_obj = MockHTTPError(json_data, status_code)

            def raise_for_status(self):
                raise self.raise_obj

        return MockResponse(json_data, status_code) 
Example #19
Source File: network_helper.py    From f5-openstack-agent with Apache License 2.0 6 votes vote down vote up
def arp_delete(self,
                   bigip,
                   ip_address,
                   partition=const.DEFAULT_PARTITION):
        if ip_address:
            address = urllib.quote(self._remove_route_domain_zero(ip_address))
            arp = bigip.tm.net.arps.arp
            try:
                if arp.exists(name=address, partition=partition):
                    obj = arp.load(name=address, partition=partition)
                    obj.delete()
            except HTTPError as err:
                LOG.error("Error deleting arp %s. "
                          "Repsponse status code: %s. Response "
                          "message: %s." % (address,
                                            err.response.status_code,
                                            err.message))

            return True

        return False 
Example #20
Source File: test_models.py    From advent-of-code-data with MIT License 5 votes vote down vote up
def test_get_stats_400(requests_mock):
    requests_mock.get(
        url="https://adventofcode.com/2015/leaderboard/self", status_code=400,
    )
    user = User("testtoken")
    with pytest.raises(HTTPError):
        user.get_stats() 
Example #21
Source File: test_resource.py    From pysnow with MIT License 5 votes vote down vote up
def test_http_error_get_one(self):
        """:meth:`one` of :class:`pysnow.Response` should raise an HTTPError exception if a
        non-200 response code was encountered"""

        httpretty.register_uri(
            httpretty.GET,
            self.mock_url_builder_base,
            status=500,
            content_type="application/json",
        )

        response = self.resource.get(self.dict_query)
        self.assertRaises(HTTPError, response.one) 
Example #22
Source File: test_databricks.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_do_api_call_retries_with_retryable_error(self):
        for exception in [requests_exceptions.ConnectionError,
                          requests_exceptions.SSLError,
                          requests_exceptions.Timeout,
                          requests_exceptions.ConnectTimeout,
                          requests_exceptions.HTTPError]:
            with mock.patch('airflow.providers.databricks.hooks.databricks.requests') as mock_requests:
                with mock.patch.object(self.hook.log, 'error') as mock_errors:
                    setup_mock_requests(mock_requests, exception)

                    with self.assertRaises(AirflowException):
                        self.hook._do_api_call(SUBMIT_RUN_ENDPOINT, {})

                    self.assertEqual(mock_errors.call_count, self.hook.retry_limit) 
Example #23
Source File: cache.py    From shipwright with Apache License 2.0 5 votes vote down vote up
def _get_manifest(self, tag):
        name, ref = tag
        try:
            return self._cache[tag]
        except KeyError:
            try:
                m = self.drc.get_manifest(name, ref)
            except requests_exceptions.HTTPError:
                return None
            else:
                self._cache[tag] = m
                return m 
Example #24
Source File: test_alpaca.py    From pylivetrader with Apache License 2.0 5 votes vote down vote up
def test_skip_http_error():

    @alpaca.skip_http_error((404, ))
    def not_found():
        raise HTTPError(response=Mock(status_code=404))

    ret = not_found()
    assert ret is None

    @alpaca.skip_http_error((404, ))
    def internal_server_error():
        raise HTTPError(response=Mock(status_code=503))

    with pytest.raises(HTTPError):
        internal_server_error() 
Example #25
Source File: main.py    From pimotion with MIT License 5 votes vote down vote up
def callback(path):
    client = M2XClient(Config.get('m2x', 'api_key'))
    try:
        api = CloudAppAPI(Config.get('cloudapp', 'username'), Config.get('cloudapp', 'password'))
        url = api.upload(path)
        print 'Public URL: ' + url

        stream = client.device(Config.get('m2x', 'device_id')).stream(Config.get('m2x', 'stream'))
        result = stream.add_value(url)
        print "Posted URL to M2X stream %s" % Config.get('m2x', 'stream')
    except HTTPError, e:
        print 'ERROR: '
        print '  STATUS: %s' % client.last_response.status
        print '  HEADERS: %s' % client.last_response.headers
        print '  BODY: %s' % client.last_response.json 
Example #26
Source File: test_network.py    From f5-openstack-agent with Apache License 2.0 5 votes vote down vote up
def delete_resource(resource):
    try:
        resource.delete()
    except HTTPError as err:
        if err.response.status_code != 404:
            raise 
Example #27
Source File: cluster_manager.py    From f5-openstack-agent with Apache License 2.0 5 votes vote down vote up
def save_config(self, bigip):
        try:
            # invalid for the version of f5-sdk in requirements
            # c = bigip.tm.sys.config
            # c.save()
            bigip.tm.util.bash.exec_cmd(
                command='run',
                utilCmdArgs="-c 'tmsh save sys config'"
            )
        except HTTPError as err:
            LOG.error("Error saving config."
                      "Repsponse status code: %s. Response "
                      "message: %s." % (err.response.status_code,
                                        err.message)) 
Example #28
Source File: network_helper.py    From f5-openstack-agent with Apache License 2.0 5 votes vote down vote up
def delete_tunnel(
            self,
            bigip,
            tunnel_name,
            partition=const.DEFAULT_PARTITION):
        """Delete a vxlan or gre tunnel."""
        t = bigip.tm.net.fdb.tunnels.tunnel
        try:
            if t.exists(name=tunnel_name, partition=partition):
                obj = t.load(name=tunnel_name, partition=partition)

                if const.FDB_POPULATE_STATIC_ARP and hasattr(obj, "records"):
                    for record in obj.records:
                        self.arp_delete_by_mac(
                            bigip,
                            record['name'],
                            partition=partition
                        )

                    obj.modify(records=[])
        except HTTPError as err:
            LOG.error("Error updating tunnel %s. "
                      "Repsponse status code: %s. Response "
                      "message: %s." % (tunnel_name,
                                        err.response.status_code,
                                        err.message))

        try:
            ts = bigip.tm.net.tunnels.tunnels.tunnel
            if ts.exists(name=tunnel_name, partition=partition):
                obj = ts.load(name=tunnel_name, partition=partition)
                obj.delete()
        except HTTPError as err:
            LOG.error("Error deleting tunnel %s. "
                      "Repsponse status code: %s. Response "
                      "message: %s." % (tunnel_name,
                                        err.response.status_code,
                                        err.message)) 
Example #29
Source File: network_helper.py    From f5-openstack-agent with Apache License 2.0 5 votes vote down vote up
def get_virtual_service_insertion(
            self,
            bigip,
            partition=const.DEFAULT_PARTITION):
        """Returns list of virtual server addresses"""
        vs_helper = resource_helper.BigIPResourceHelper(
            resource_helper.ResourceType.virtual)
        va_helper = resource_helper.BigIPResourceHelper(
            resource_helper.ResourceType.virtual_address)
        virtual_servers = vs_helper.get_resources(bigip, partition=partition)
        virtual_services = []

        for virtual_server in virtual_servers:
            name = virtual_server.name
            virtual_address = {name: {}}
            dest = os.path.basename(virtual_server.destination)
            (vip_addr, vip_port) = self.split_addr_port(dest)

            # Get virtual address associated with the virtual server.
            # split_addr_port can return a tuple where the vip addr is either
            # a name of the virt address, or the actual IP address.
            # This code gets a reference to the virt address in the event the
            # virtual server destination is a named resource, not an IP addr.
            try:
                vaddr = va_helper.load(
                    bigip, name=vip_addr, partition=partition)
            except HTTPError:
                continue
            else:
                # there was no exception: legit VA, so use address from VA
                vip_addr = vaddr.raw['address']

            virtual_address[name]['address'] = vip_addr
            virtual_address[name]['netmask'] = virtual_server.mask
            virtual_address[name]['protocol'] = virtual_server.ipProtocol
            virtual_address[name]['port'] = vip_port
            virtual_services.append(virtual_address)

        return virtual_services 
Example #30
Source File: network_helper.py    From f5-openstack-agent with Apache License 2.0 5 votes vote down vote up
def _arp_delete_by_network(self, bigip, partition, network):
        """Delete ARP entry if address in network"""
        if not network:
            return []
        mac_addresses = []
        ac = bigip.tm.net.arps
        params = {'params': get_filter(bigip, 'partition', 'eq', partition)}
        try:
            arps = ac.get_collection(requests_params=params)
        except HTTPError as err:
            LOG.error("Error getting ARPs."
                      "Repsponse status code: %s. Response "
                      "message: %s." % (err.response.status_code,
                                        err.message))
            return mac_addresses

        for arp in arps:
            address_index = arp.ipAddress.find('%')
            if address_index > -1:
                address = netaddr.IPAddress(arp.ipAddress[0:address_index])
            else:
                address = netaddr.IPAddress(arp.ipAddress)

            if address in network:
                mac_addresses.append(arp.macAddress)
                try:
                    arp.delete()
                except HTTPError as err:
                    LOG.error("Error deleting ARP %s."
                              "Repsponse status code: %s. Response "
                              "message: %s." % (arp.ipAddress,
                                                err.response.status_code,
                                                err.message))
        return mac_addresses