Esempio n. 1
0
    def get_events(self, sessionid):
        '''
        Fetches the events from the gameevents service for a given sessionid.
        :param sessionid:
        '''

        try:
            token = self.get_token()
            if token:
                #LOG.debug("Sending request for events...")
                #payload = {"token": token, "sessionid": sessionid}
                headers = {}
                headers["X-AUTH-TOKEN"] = token
                url = GAMEEVENTS_SERVICE_ENDPOINT + '/sessions/%s/events' % sessionid
                response = requests.get(url, headers=headers)
                if (response.status_code == 200):
                    clean_response = {}
                    try:
                        count = int(response.headers.get(
                            'X-Total-Count', None))

                        #Load the list of responses
                        json_response = response.json()
                        LOG.debug(json_response)

                        #Iterate over the responses and extract the interesting bit (gameevent key)
                        clean_response = {}
                        events = []

                        for item in json_response:
                            events.append(item["gameevent"])

                        clean_response["events"] = events
                        clean_response["count"] = count

                        return clean_response

                    except KeyError:
                        #LOG.debug("Server response: %s " % myresponse["message"])
                        raise RequestException(
                            "Unrecognized response from server")

                elif (response.status_code == 400):
                    raise RequestException("Badly formed request.")
                elif (response.status_code == 401):
                    raise RequestException("Not authorized.")
                else:
                    raise RequestException(
                        "Unknown error when trying to get token.")
            else:
                raise RequestException("Could not get a token.")
        except RequestException as e:
            LOG.error(e.args, exc_info=True)
            raise e
        except ValueError as e:
            LOG.error(e.args, exc_info=False)
            return False
        except Exception as e:
            LOG.error(e.args, exc_info=True)
            raise e
Esempio n. 2
0
def initial_request_to_conductor(rc, conductor_url, conductor_req_json):
    """First steps in the request-redirect chain in making a call to Conductor

    :param rc: REST client object for calling conductor
    :param conductor_url: conductor's base URL to submit a placement request
    :param conductor_req_json: request json object to send to Conductor
    :return: URL to check for follow up (similar to redirects);
             we keep checking these till we get a result/error
    """
    debug_log.debug("Payload to Conductor: {}".format(json.dumps(conductor_req_json)))
    raw_resp = rc.request(url=conductor_url, raw_response=True, method="POST",
                          json=conductor_req_json)
    resp = raw_resp.json()
    if resp["status"] != "template":
        raise RequestException(response=raw_resp, request=raw_resp.request)
    time.sleep(10)  # 10 seconds wait time to avoid being too quick!
    plan_url = resp["links"][0][0]["href"]
    debug_log.debug("Attempting to read the plan from "
                    "the conductor provided url {}".format(plan_url))
    raw_resp = rc.request(raw_response=True,
                          url=plan_url)
    resp = raw_resp.json()

    if resp["plans"][0]["status"] in ["error"]:
        raise RequestException(response=raw_resp, request=raw_resp.request)
    return resp, raw_resp  # now the caller of this will handle further follow-ups
    def test_unknown_error(self, request):
        e = RequestException("error")
        e.response = MagicMock()
        e.response.status_code = 404
        e.response.content = "Error Code"
        request().raise_for_status.side_effect = e

        self.assertRaises(TransportError, make_rest_api_call, "GET", "http://something.com/path/to/resource.txt")
Esempio n. 4
0
 def dummy_request_exception(self):
     e = RequestException("Web Request Exception Description")
     e.response = mock.MagicMock()
     e.request = Request(method="GET", url="SOME-URL")
     e.response.status_code = 400
     e.response.content = "Some request exception occurred"
     # request().raise_for_status.side_effect = e
     return e
Esempio n. 5
0
    def _check_and_pack_response(self, r):
        # Review and use the different Error Codes from ISV API
        request_attrs = ('json', 'status_code', 'ok')

        for attr in request_attrs:
            if not hasattr(r, attr):
                raise RequestException(Message.RESPONSE_DOES_NOT_HAVE_ATTRIBUTE.format(attr, r.status_code))
            if int(r.status_code) >= 299 and int(r.status_code) != 400:
                raise RequestException(Message.RESPONSE_ERROR.format(r.status_code, r.text))
    def get_market_orders(self,
                          instrument_id,
                          order_type=None,
                          offer_type=None,
                          status=None,
                          max_count=None):
        """Gets the market orders with the ability to apply filters.

        :param instrument_id: Instrument identifier. Use get_trader_instruments() to retrieve them. Optional.
        :type instrument_id: int
        :param order_type: Order type. Possible values OrderType.LIMIT, OrderType.MARKET and OrderType.STOP. Optional.
        :type order_type: OrderType
        :param offer_type: Offer type. Possible values OfferType.BID and OfferType.ASK. Optional.
        :type offer_type: OfferType
        :param status: Order status. A comma separated list of integers with possible values 10(Pending), 15(Failed),
            20(Placed), 30(Rejected), 40(Cancelled), 50(PartiallyExecuted) and 60(Executed). Optional.
        :type status: string
        :param max_count: Maximum number of items returned. Default value is 100. Optional.
        :type max_count: int
        :returns: The list of orders.
        :rtype: list of dict. Each element has the following data:\n
            orderID (string)\n
            price (float)\n
            initialQuantity (float)\n
            quantity (float)\n
            dateCreated (string)\n
            offerType (int) - Possible values 1 (Bid) and 2 (Ask).\n
            type (int) - Possible values 1 (Limit), 2 (Market) and 3 (Stop).\n
            status (int) - Possible values 10 (Pending), 15 (Failed), 20 (Placed), 30 (Rejected), 40 (Cancelled),
            50 (PartiallyExecuted) and 60 (Executed).\n
            instrumentID (int)\n
            trades (list of dict)
        :raises: RequestException
        """
        data = {'apiID': self.api_id, 'instrumentID': instrument_id}
        if order_type is not None:
            if not isinstance(order_type, OrderType):
                raise ValueError('order_type must be of type OrderType')
            data['orderType'] = order_type.value
        if offer_type is not None:
            if not isinstance(offer_type, OfferType):
                raise ValueError('offer_type must be of type OfferType')
            data['offerType'] = offer_type.value
        if status is not None:
            data['status'] = status
        if max_count is not None:
            data['maxCount'] = max_count

        query_string = urlencode(data)
        response = requests.get(self.api_url + self.GET_MARKET_ORDERS_PATH +
                                query_string)
        if response.status_code == 200:
            orders = response.json()
            for order in orders:
                convert_order_number_fields(order)
            return orders
        else:
            exception_message = 'Failed to get the market orders. {error_message}'.format(
                error_message=get_error_message(response))
            raise RequestException(exception_message)
Esempio n. 7
0
    def invokeAPI(httpMethod, authUrl, payload, accessToken):
        nc = NorthApiClient()

        clientInfo = Constant().clientInfo()
        url = RestConstant.BASE_URL + clientInfo[
            'platformIp'] + ":" + clientInfo['platformPort'] + authUrl
        if accessToken == None:
            headers = {'Content-Type': 'application/json'}
        else:
            headers = {
                "app_key": clientInfo['appId'],
                "Authorization": "Bearer " + accessToken,
                "Content-Type": "application/json"
            }
        try:
            request = requests.Session()
            request.mount('https://',
                          HTTPAdapter(pool_connections=10, pool_maxsize=10))

            global response
            if httpMethod == "POST":
                response = requests.post(url,
                                         headers=headers,
                                         data=payload,
                                         cert=nc.cert,
                                         verify=False)
            elif httpMethod == "PUT":
                response = request.put(url,
                                       headers=headers,
                                       data=payload,
                                       cert=nc.cert,
                                       verify=False)
            elif httpMethod == "DELETE":
                response = request.delete(url,
                                          headers=headers,
                                          data=payload,
                                          cert=nc.cert,
                                          verify=False)
            elif httpMethod == "GET":
                response = request.get(url,
                                       headers=headers,
                                       params=payload,
                                       cert=nc.cert,
                                       verify=False)
            logging.info(url), logging.info(headers), logging.info(
                payload), logging.info(response.text)
            return response.text

        except ReadTimeout as e:
            logging.error(e)
            raise ReadTimeout(e)
        except ConnectionError as e:
            logging.error(e)
            raise ConnectionError(e)
        except RequestException as e:
            logging.error(e)
            raise RequestException(e)
        except Exception as e:
            logging.error(e)
            raise Exception(e)
Esempio n. 8
0
def get_url_content(get: Callable, url_address: str):
    request_timeout = 5

    try:
        response = get(url_address,
                       params=(('format', 'j1'), ),
                       timeout=request_timeout)
        response.raise_for_status()

        if response.text:  # check response text is empty or not
            return response.text
        else:
            raise Exception("Response contains no data")
    except Timeout:
        raise Timeout(f"URL request timeout more than {request_timeout} sec")
    except URLRequired:
        raise URLRequired(f"{url_address} is an invalid URL")
    except ConnectionError:
        raise ConnectionError(
            "Refused connection or DNS failure etc. occurred")
    except HTTPError as http_err:
        raise HTTPError(f"HTTP error: {http_err}  occurred")
    except RequestException as e:
        raise RequestException(f"There was an ambiguous exception that "
                               f"occurred while handling request. Error: {e} ")
    except KeyboardInterrupt:
        raise KeyboardInterrupt("Script was interrupted by user")
Esempio n. 9
0
    def test_request_exception_is_wrapped(self):
        host = factories.HostFactory()
        self.mocked_api().host.get.side_effect = RequestException()
        self.assertRaises(ServiceBackendError, self.backend.create_host, host)

        self.mocked_api().host.get.side_effect = pyzabbix.ZabbixAPIException()
        self.assertRaises(ServiceBackendError, self.backend.create_host, host)
    def get_partner_instruments(self):
        """Gets the available instruments for the partner.

        :returns: The list of instruments.
        :rtype: list of dict. Each element has the following data:\n
            id (int)\n
            description (string)\n
            name (string)\n
            baseCurrencyID (int) - The currency you bid for, i.e. for the Bitcoin/Euro base currency is the Bitcoin.\n
            quoteCurrencyID (int) - The currency you pay with, i.e. for the Bitcoin/Euro quote currency is the Euro.\n
            minOrderAmount (float) - The minimum order amount for an order. Every order having an amount less than that,
            will be rejected.\n
            commissionFeePercent (float) - The percent of the commission fee when trading this instrument.
            The value is a decimal between 0 and 1.
        :raises: RequestException
        """
        data = {'apiID': self.api_id}
        query_string = urlencode(data)
        response = requests.get(self.api_url +
                                self.GET_PARTNER_INSTRUMENTS_PATH +
                                query_string)
        if response.status_code == 200:
            instruments = response.json()
            for instrument in instruments:
                convert_instrument_number_fields(instrument)
            return instruments
        else:
            exception_message = 'Failed to get the partner instruments. {error_message}'.format(
                error_message=get_error_message(response))
            raise RequestException(exception_message)
Esempio n. 11
0
    def test_sync(self):
        # create classifier but don't sync the intents
        c = Classifier.create(
            self.org,
            self.user,
            BothubType.slug,
            "Booker",
            {
                BothubType.CONFIG_ACCESS_TOKEN: "123456789",
                BothubType.INTENT_URL: "https://nlp.bothub.it/info/"
            },
            sync=False,
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')

            c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 1)

            mock_get.side_effect = RequestException("Network is unreachable",
                                                    response=MockResponse(
                                                        100, ""))
            c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 2)

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            intents = c.get_type().get_active_intents_from_api(c)

            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 3)
            self.assertEqual(3, len(intents))
            intent = intents[0]
            self.assertEqual("intent", intent.name)
            self.assertEqual("intent", intent.external_id)
Esempio n. 12
0
 def test_other_url_error(self):
     reason = Exception('Some other failure.')
     self.requests_mock.side_effect = RequestException(reason)
     tasks.fetch_manifest('url', self.upload.pk)
     self.check_validation(
         'No manifest was found at that URL. Check the address and try '
         'again.')
    def invokeAPI2(httpMethod, url, headers, payload):
        nc = NorthApiClient()

        try:
            request = requests.Session()
            request.mount('https://', HTTPAdapter(pool_connections=11, pool_maxsize=11))

            global response
            if httpMethod == "POST":
                response = request.post(url, headers=headers, data=payload, cert=nc.cert, verify=False, timeout=10)
            logging.info(url), logging.info(headers), logging.info(payload), logging.info(response.text)
            return response.text

        except ReadTimeout as e:
            logging.error(e)
            raise ReadTimeout(e)
        except ConnectionError as e:
            logging.error(e)
            raise ConnectionError(e)
        except RequestException as e:
            logging.error(e)
            raise RequestException(e)
        except Exception as e:
            logging.error(e)
            raise Exception(e)
    def test_get_request_exception_and_retry_and_success(self, Session):
        self.count = 0
        request_exception = RequestException('I raise RequestException!')

        def exception_raiser(method,
                             url,
                             data,
                             params,
                             headers=None,
                             allow_redirects=None):
            if self.count < 4:
                self.count += 1
                raise request_exception

            return MagicMock(status_code=200,
                             json=MagicMock(return_value={'success': 'True'}))

        Session.return_value = MagicMock(request=exception_raiser)

        ex = HttpExecutor('http://api.stormpath.com/v1', ('user', 'pass'))

        ShouldRetry = MagicMock(return_value=True)

        with patch('stormpath.http.HttpExecutor.should_retry', ShouldRetry):
            with self.assertRaises(Error):
                ex.get('/test')

        should_retry_calls = [
            call(0, request_exception),
            call(1, request_exception),
            call(2, request_exception),
            call(3, request_exception),
        ]
        ShouldRetry.assert_has_calls(should_retry_calls)
    def test_get_request_exception_and_retry_four_times(self, Session):
        request_exception = RequestException('I raise RequestException!')

        def exception_raiser(method,
                             url,
                             data,
                             params,
                             headers=None,
                             allow_redirects=None):
            raise request_exception

        Session.return_value = MagicMock(request=exception_raiser)

        ex = HttpExecutor('http://api.stormpath.com/v1', ('user', 'pass'))

        def try_four_times(retries, status):
            return retries <= 3

        ShouldRetry = MagicMock()
        ShouldRetry.side_effect = try_four_times

        with patch('stormpath.http.HttpExecutor.should_retry', ShouldRetry):
            with self.assertRaises(Error):
                ex.get('/test')

        should_retry_calls = [
            call(0, request_exception),
            call(1, request_exception),
            call(2, request_exception),
            call(3, request_exception),
            call(4, request_exception)
        ]
        ShouldRetry.assert_has_calls(should_retry_calls)
Esempio n. 16
0
def test_service_ok(mock_requests_get, fail_reason):
    url = "http://*****:*****@example.org"

    if fail_reason == "connection_error":
        mock_requests_get.side_effect = [RequestException(fail_reason)]
        expect_reason = "connection failed"
    elif fail_reason == "status_code":
        response = mock_requests_get.return_value
        response.ok = False
        response.status_code = 503
        response.reason = "Service Unavailable"
        response.url = url
        expect_reason = "503: Service Unavailable"
    else:
        expect_reason = None

    ok, reason = status._service_ok(url)

    if fail_reason:
        assert not ok
        assert reason == expect_reason
        assert url not in reason
    else:
        assert ok
        assert reason is None

    mock_requests_get.assert_called_once_with(url)
Esempio n. 17
0
    def test_create_patient(self):
        """
        ValueSource instances with direction set to DIRECTION_IMPORT
        should not be exported.
        """
        requests = mock.Mock()
        requests.post.side_effect = RequestException()
        info = mock.Mock(
            updates={'sex': 'M', 'dob': '1918-07-18'},
            extra_fields={},
        )
        case_config = copy.deepcopy(CASE_CONFIG)
        case_config['patient_identifiers'] = {}
        case_config['person_preferred_name'] = {}
        case_config['person_preferred_address'] = {}
        case_config['person_attributes'] = {}

        case_config['person_properties']['gender']['direction'] = DIRECTION_IMPORT
        case_config['person_properties']['birthdate']['direction'] = DIRECTION_EXPORT
        case_config = OpenmrsCaseConfig(case_config)

        with self.assertRaises(RequestException):
            create_patient(requests, info, case_config)
        requests.post.assert_called_with(
            '/ws/rest/v1/patient/',
            json={'person': {'birthdate': '1918-07-18'}},
            raise_for_status=True,
        )
Esempio n. 18
0
 def test_url_timeout(self):
     reason = socket.timeout('too slow')
     self.requests_mock.side_effect = RequestException(reason)
     tasks.fetch_manifest('url', self.upload.pk)
     self.check_validation(
         'No manifest was found at that URL. Check the address and try '
         'again.')
Esempio n. 19
0
    def on_task_start(self, task, config):
        url = config['url']
        username = config['username']
        password = config['password']
        try:
            response = task.requests.send(construct_request(url, username=username, password=password))
            if not response.ok:
                raise RequestException(str(response))
            cookies = collect_cookies(response)
            if len(get_valid_cookies(cookies)) < 1:
                raise RequestException('No recognized WordPress cookies found. Perhaps username/password is invalid?')
            task.requests.add_cookiejar(cookies)

        except RequestException as err:
            log.error('%s', err)
            raise PluginError('WordPress Authentication at %s failed' % (url,))
Esempio n. 20
0
    def test_get_request_exception_and_retry_and_success(self, Session):
        self.count = 0
        request_exception = RequestException('mocked error')
        successful_response = MagicMock(
            status_code=200, json=MagicMock(return_value={'success': 'True'}))

        def faker(*args, **kwargs):
            if self.count < 3:
                self.count += 1
                raise request_exception

            return successful_response

        Session.return_value = MagicMock(request=faker)

        ex = HttpExecutor('http://api.stormpath.com/v1', ('user', 'pass'))
        should_retry = MagicMock(return_value=True)

        with patch('stormpath.http.HttpExecutor.should_retry', should_retry):
            resp = ex.get('/test')

        self.assertEqual(resp['sp_http_status'], 200)
        should_retry.assert_has_calls([
            call(0, request_exception),
            call(1, request_exception),
            call(2, request_exception),
        ])
Esempio n. 21
0
 def __init__(self,
              mgtSvr,
              port=8096,
              user=None,
              passwd=None,
              apiKey=None,
              securityKey=None,
              asyncTimeout=3600,
              logging=None,
              scheme='http',
              path='client/api'):
     self.loglevel()  #Turn off requests logs
     self.apiKey = apiKey
     self.securityKey = securityKey
     self.mgtSvr = mgtSvr
     self.port = port
     self.user = user
     self.passwd = passwd
     self.logging = logging
     self.path = path
     self.retries = 5
     self.asyncTimeout = asyncTimeout
     self.auth = True
     if port == 8096 or \
        (self.apiKey is None and self.securityKey is None):
         self.auth = False
     if scheme not in ['http', 'https']:
         raise RequestException("Protocol must be HTTP")
     self.protocol = scheme
     self.baseurl = "%s://%s:%d/%s"\
                    % (self.protocol, self.mgtSvr, self.port, self.path)
Esempio n. 22
0
    def test_channel_log(self):
        channel = self.create_channel("WA", "WhatsApp: 1234", "1234")

        exception = RequestException("Network is unreachable",
                                     response=MockResponse(100, ""))
        start = timezone.now()

        log1 = HTTPLog.create_from_exception(
            HTTPLog.WHATSAPP_TEMPLATES_SYNCED,
            "https://graph.facebook.com/v3.3/1234/message_templates",
            exception,
            start,
            channel=channel,
        )

        self.login(self.admin)
        log_url = reverse("request_logs.httplog_read", args=[log1.id])
        response = self.client.get(log_url)
        self.assertContains(response, "200")
        self.assertContains(response, "Connection Error")
        self.assertContains(
            response, "https://graph.facebook.com/v3.3/1234/message_templates")

        # and can't be from other org
        self.login(self.admin2)
        response = self.client.get(log_url)
        self.assertLoginRedirect(response)
Esempio n. 23
0
def mocked_requests_get(*args, **kwargs):
    class MockResponse:
        def __init__(self, json_data, status_code):
            self.json_data = json_data
            self.status_code = status_code

        def json(self):
            return self.json_data

    with open(
            os.path.join(current_directory,
                         'http_responses/response_200.json')) as f:
        json_200 = json.load(f)
    with open(
            os.path.join(current_directory,
                         'http_responses/response_400.json')) as f:
        json_400 = json.load(f)
    print(args[0])
    if args[0] == 'https://airapi.airly.eu/v2/measurements/installation?installationId=4444':
        return MockResponse(json_200, 200)
    elif args[
            0] == 'https://airapi.airly.eu/v2/measurements/installation?installationId=666':
        return MockResponse(json_400, 400)
    elif args[
            0] == 'https://airapi.airly.eu/v2/measurements/installation?installationId=888':
        raise RequestException()
    return MockResponse(None, 404)
Esempio n. 24
0
    def test_sync(self):
        c = Classifier.create(
            self.org,
            self.user,
            WitType.slug,
            "Booker",
            {
                WitType.CONFIG_APP_ID: "12345",
                WitType.CONFIG_ACCESS_TOKEN: "sesame"
            },
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 2)
            with self.assertRaises(Exception):
                c.get_type().get_active_intents_from_api(c)
                self.assertEqual(
                    HTTPLog.objects.filter(classifier=c).count(), 3)

            mock_get.side_effect = RequestException("Network is unreachable",
                                                    response=MockResponse(
                                                        100, ""))
            c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 4)

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            intents = c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 5)
            self.assertEqual(2, len(intents))
            car = intents[0]
            self.assertEqual("book_car", car.name)
            self.assertEqual("book_car", car.external_id)
Esempio n. 25
0
    def test_post_to_frontend_repeated_all_attempts_failed(self, mask_post_to_fe, mc_time):
        self.ptf.side_effect = RequestException()

        with pytest.raises(RequestException):
            self.fc._post_to_frontend_repeatedly(self.data, self.url_path)

        assert mc_time.sleep.called
    def test_transmit_failure(self, client_mock, track_selection_reverse_mock):
        client_mock.get_oauth_access_token.return_value = "token", datetime.datetime.utcnow(
        )
        client_mock_instance = client_mock.return_value
        client_mock_instance.send_course_import.side_effect = RequestException(
            'error occurred')
        track_selection_reverse_mock.return_value = '/course_modes/choose/course-v1:edX+DemoX+Demo_Course/'

        course_exporter_mock = mock.MagicMock(courses=self.payload)
        course_exporter_mock.get_serialized_data_blocks.return_value = [
            (json.dumps(self.payload), 2)
        ]
        course_exporter_mock.resolve_removed_courses.return_value = {}

        transmitter = courses.SuccessFactorsCourseTransmitter(
            self.enterprise_config)

        catalog_transmission_audit = transmitter.transmit(course_exporter_mock)

        client_mock_instance.send_course_import.assert_called_with(
            json.dumps(self.payload))
        course_exporter_mock.get_serialized_data_blocks.assert_called()
        course_exporter_mock.resolve_removed_courses.assert_called_with({})
        assert catalog_transmission_audit.enterprise_customer_uuid == self.enterprise_config.enterprise_customer.uuid
        assert catalog_transmission_audit.total_courses == len(self.payload)
        assert catalog_transmission_audit.status == '500'
        assert catalog_transmission_audit.error_message == 'error occurred'
Esempio n. 27
0
def __if_status_is_not_OK_raise_error(url, response):
    from requests import RequestException

    if response.status_code != 200:
        raise RequestException(
            'Failed to query url {} Returned status: {}'.format(
                url, response.status_code))
Esempio n. 28
0
    def test_extract_data(self):
        response_file = 'test/adapters/dcae/des_response.json'
        response_json = json_from_file(response_file)

        des_config = self.osdf_config.core['PCI']['DES']
        service_id = des_config['service_id']
        data = des_config['filter']
        expected = response_json['result']
        response = mock.MagicMock()
        response.status_code = 200
        response.ok = True
        response.json.return_value = response_json
        self.patcher_req = patch('requests.request', return_value=response)
        self.Mock_req = self.patcher_req.start()
        self.assertEqual(expected, des.extract_data(service_id, data))
        self.patcher_req.stop()

        response = mock.MagicMock()
        response.status_code = 404
        response.raise_for_status.side_effect = HTTPError("404")
        self.patcher_req = patch('requests.request', return_value=response)
        self.Mock_req = self.patcher_req.start()
        self.assertRaises(DESException, des.extract_data, service_id, data)
        self.patcher_req.stop()

        self.patcher_req = patch('requests.request',
                                 side_effect=RequestException("error"))
        self.Mock_req = self.patcher_req.start()
        self.assertRaises(DESException, des.extract_data, service_id, data)
        self.patcher_req.stop()
Esempio n. 29
0
 def test_connection_error(self):
     reason = socket.gaierror(8, 'nodename nor servname provided')
     self.requests_mock.side_effect = RequestException(reason)
     tasks.fetch_manifest('url', self.upload.pk)
     self.check_validation(
         'No manifest was found at that URL. Check the address and try '
         'again.')
Esempio n. 30
0
    def test_should_propagate_any_exception(self):
        # given
        mocked_request_function = Mock(side_effect=RequestException())

        # when
        with pytest.raises(RequestException):
            _wrapper(mocked_request_function, "GET", "https://example.net")
Esempio n. 31
0
    def handle(self, *args, **options):
        ethereum_client = EthereumClientProvider()
        app_name = apps.get_app_config('history').verbose_name
        network_name = ethereum_client.get_network().name.capitalize()
        startup_message = f'Starting {app_name} version {__version__} on {network_name}'
        self.stdout.write(self.style.SUCCESS(startup_message))

        if settings.SLACK_API_WEBHOOK:
            try:
                r = requests.post(settings.SLACK_API_WEBHOOK,
                                  json={'text': startup_message})
                if r.ok:
                    self.stdout.write(
                        self.style.SUCCESS(
                            f'Slack configured, "{startup_message}" sent'))
                else:
                    raise RequestException()
            except RequestException as e:
                self.stdout.write(
                    self.style.ERROR(
                        f'Cannot send slack notification to webhook '
                        f'({settings.SLACK_API_WEBHOOK}): "{e}"'))
        else:
            self.stdout.write(
                self.style.SUCCESS('Slack not configured, ignoring'))
Esempio n. 32
0
 def __init__(self, status, *args, **kwargs):
     RequestException.__init__(self, args, kwargs)
     self.strerror = __STATMSG__.format(status)