Python requests.Response() Examples

The following are code examples for showing how to use requests.Response(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: drydock   Author: airshipit   File: promenade_client.py    Apache License 2.0 6 votes vote down vote up
def get(self, route, query=None, timeout=None):
        """
        Send a GET request to Promenade.

        :param string route: The URL string following the hostname and API prefix
        :param dict query: A dict of k, v pairs to add to the query string
        :param timeout: A single or tuple value for connect, read timeout.
            A single value indicates the read timeout only
        :return: A requests.Response object
        """
        auth_refresh = False
        while True:
            url = self.base_url + route
            self.logger.debug('GET ' + url)
            self.logger.debug('Query Params: ' + str(query))
            resp = self.__session.get(
                url, params=query, timeout=self._timeout(timeout))

            if resp.status_code == 401 and not auth_refresh:
                self.set_auth()
                auth_refresh = True
            else:
                break

        return resp 
Example 2
Project: drydock   Author: airshipit   File: session.py    Apache License 2.0 6 votes vote down vote up
def get(self, endpoint, query=None, timeout=None):
        """
        Send a GET request to Drydock.

        :param string endpoint: The URL string following the hostname and API prefix
        :param dict query: A dict of k, v pairs to add to the query string
        :param timeout: A single or tuple value for connect, read timeout.
            A single value indicates the read timeout only
        :return: A requests.Response object
        """
        auth_refresh = False
        while True:
            url = self.base_url + endpoint
            self.logger.debug('GET ' + url)
            self.logger.debug('Query Params: ' + str(query))
            resp = self.__session.get(
                url, params=query, timeout=self._timeout(timeout))

            if resp.status_code == 401 and not auth_refresh:
                self.set_auth()
                auth_refresh = True
            else:
                break

        return resp 
Example 3
Project: kegg   Author: bio2bel   File: description.py    MIT License 6 votes vote down vote up
def process_protein_info_to_model(response: Response):
    """Process description.

    :param response: response from KEGG API
    :type: dict
    :return: protein model attributes
    """
    # Get protein description from KEGG API
    description = parse_description(response)
    # Filters out db link columns
    protein_as_dict = get_description_properties(
        description=description,
        description_property=DBLINKS,
        columns=PROTEIN_RESOURCES
    )
    # Adapt the dict keys to match protein model columns
    return kegg_properties_to_models(protein_as_dict) 
Example 4
Project: bigquerylayers   Author: smandaric   File: upload.py    GNU General Public License v3.0 6 votes vote down vote up
def transmit(self, transport, data, content_type):
        """Transmit the resource to be uploaded.

        Args:
            transport (~requests.Session): A ``requests`` object which can
                make authenticated requests.
            data (bytes): The resource content to be uploaded.
            content_type (str): The content type of the resource, e.g. a JPEG
                image has content type ``image/jpeg``.

        Returns:
            ~requests.Response: The HTTP response returned by ``transport``.
        """
        method, url, payload, headers = self._prepare_request(data, content_type)
        response = _helpers.http_request(
            transport,
            method,
            url,
            data=payload,
            headers=headers,
            retry_strategy=self._retry_strategy,
        )
        self._process_response(response)
        return response 
Example 5
Project: botbuilder-python   Author: microsoft   File: direct_line_client.py    MIT License 6 votes vote down vote up
def send_message(self, text: str, retry_count: int = 3) -> Response:
        """Send raw text to bot framework using direct line api"""

        url = "/".join(
            [self._base_url, "conversations", self._conversation_id, "activities"]
        )
        json_payload = {
            "conversationId": self._conversation_id,
            "type": "message",
            "from": {"id": "user1"},
            "text": text,
        }

        success = False
        current_retry = 0
        bot_response = None
        while not success and current_retry < retry_count:
            bot_response = requests.post(url, headers=self._headers, json=json_payload)
            current_retry += 1
            if bot_response.status_code == 200:
                success = True

        return bot_response 
Example 6
Project: Lomapy   Author: AlanTaranti   File: manipulador_requisicoes.py    MIT License 6 votes vote down vote up
def validar_reposta(resposta: requests.Response) -> dict:
    """
    Valida a resposta da API do Lomadee e retorna os dados

    :param resposta: Reposta a ser validada
    :type resposta: requests.Response

    :raises Exception: Falha na requisição

    :return: Dados da Resposta
    :rtype: dict
    """
    if resposta.status_code == 200:
        return resposta.json()
    else:
        return erro(resposta) 
Example 7
Project: Lomapy   Author: AlanTaranti   File: manipulador_requisicoes.py    MIT License 6 votes vote down vote up
def erro(resposta: requests.Response):
    """
    Gera uma exceção padronizada

    :param resposta: Resposta
    :type resposta: requests.Response

    :raises Exception: Falha na requisição
    """
    dados = {
        "codigo": resposta.status_code,
        "motivo": resposta.reason,
        "resposta": None
    }
    try:
        dados["resposta"] = resposta.json()
    except JSONDecodeError:
        pass

    if dados["codigo"] == 404:
        raise RespostaVaziaException()
    else:
        raise Exception(dados) 
Example 8
Project: ezapi-yelp   Author: zehengl   File: fusion.py    MIT License 6 votes vote down vote up
def process_response(wrapped, instance, args, kwargs):
    """
    Decorator to process requests.Response

    Raises:
        Exception: Service Unavailable

    Returns:
        dict: json data
    """

    try:
        resp = wrapped(*args, **kwargs)

    except (requests.ConnectionError, requests.Timeout) as e:
        raise Exception("Service Unavailable") from e

    else:
        resp.raise_for_status()
        return resp.json() 
Example 9
Project: pyatn-client   Author: ATNIO   File: atn.py    MIT License 6 votes vote down vote up
def call_dbot_api(self, dbot_address: str, uri: str, method: str, **requests_kwargs) -> Response:
        """Send the API's HTTP request

        Channel will be auto created if no channel or be topuped if
        insufficient balance in channel.
        The deposit value is determined by `deposit_strategy`.
        A signature of balance will be sent to DBot server to pay the price of the API.

        :param dbot_address: address of the DBot contract
        :param uri: uri of the endpoint
        :param method: method of the endpoint
        :param requests_kwargs: the other args for http request is same with `requests`
        :return: :class:`Response <Response>` object, http response of the API
        :rtype: requests.Response
        """
        dbot_address = Web3.toChecksumAddress(dbot_address)
        price = self.get_price(dbot_address, uri, method)
        channel = self._get_suitable_channel(dbot_address, price)
        channel.create_transfer(price)
        domain = self.get_dbot_domain(dbot_address)
        dbot_url = domain if domain.lower().startswith('http') else 'http://{}'.format(domain)
        url = '{}/call/{}/{}'.format(dbot_url, dbot_address, remove_slash_prefix(uri))
        return self._request(channel, method, url, **requests_kwargs) 
Example 10
Project: kiefer   Author: andygoldschmidt   File: test_client.py    MIT License 6 votes vote down vote up
def setup(mocker):
    class Setup:
        resp = mocker.patch.object(requests.Response, '__init__')
        resp.status_code = 200
        resp.json = lambda: {'meta': {'error_type': 'CustomError',
                                      'error_detail': 'custom error detail'}}
        req_get = mocker.patch('requests.get')
        req_get.return_value = resp

        req_post = mocker.patch('requests.post')
        req_post.return_value = resp

        req_delete = mocker.patch('requests.delete')
        req_delete.return_value = resp

        client = KieferClient('access_token')
        headers = {'Authorization': 'Bearer access_token'}
    return Setup 
Example 11
Project: zun   Author: openstack   File: fake_requests.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, status_code, content=None, headers=None):
        """A requests.Response that can be used as a mock return_value.

        A key feature is that the instance will evaluate to True or False like
        a real Response, based on the status_code.

        Properties like ok, status_code, text, and content, and methods like
        json(), work as expected based on the inputs.

        :param status_code: Integer HTTP response code (200, 404, etc.)
        :param content: String supplying the payload content of the response.
                        Using a json-encoded string will make the json() method
                        behave as expected.
        :param headers: Dict of HTTP header values to set.
        """
        super(FakeResponse, self).__init__()
        self.status_code = status_code
        if content:
            self._content = content.encode('utf-8')
            self.encoding = 'utf-8'
        if headers:
            self.headers = headers 
Example 12
Project: django-cavalry   Author: valohai   File: poster.py    MIT License 6 votes vote down vote up
def post_stats(request: WSGIRequest, response: HttpResponse, data: dict) -> Response:
    es_url_template = get_request_es_url_template(request)
    if not es_url_template:
        return None
    payload = build_payload(data, request, response)
    es_url = es_url_template.format_map(
        dict(payload, ymd=datetime.utcnow().strftime('%Y-%m-%d')),
    )
    body = force_bytes(json.dumps(payload, cls=PayloadJSONEncoder))
    try:
        resp = sess.post(
            es_url, data=body, headers={'Content-Type': 'application/json'}, timeout=0.5
        )
        if resp.status_code != 201:
            log.warning(
                'Unable to post data to %s (error %s): %s',
                es_url,
                resp.status_code,
                resp.text,
            )
        return resp
    except Exception as e:
        log.warning('Unable to post data to %s: %s', es_url, e) 
Example 13
Project: instaloader   Author: instaloader   File: instaloadercontext.py    MIT License 6 votes vote down vote up
def get_raw(self, url: str, _attempt=1) -> requests.Response:
        """Downloads a file anonymously.

        :raises QueryReturnedNotFoundException: When the server responds with a 404.
        :raises QueryReturnedForbiddenException: When the server responds with a 403.
        :raises ConnectionException: When download failed.

        .. versionadded:: 4.2.1"""
        with self.get_anonymous_session() as anonymous_session:
            resp = anonymous_session.get(url, stream=True)
        if resp.status_code == 200:
            resp.raw.decode_content = True
            return resp
        else:
            if resp.status_code == 403:
                # suspected invalid URL signature
                raise QueryReturnedForbiddenException("403 when accessing {}.".format(url))
            if resp.status_code == 404:
                # 404 not worth retrying.
                raise QueryReturnedNotFoundException("404 when accessing {}.".format(url))
            raise ConnectionException("HTTP error code {}.".format(resp.status_code)) 
Example 14
Project: snet-marketplace-service   Author: singnet   File: test_verification_service.py    MIT License 6 votes vote down vote up
def test_get_document_types(self, mock_requests):
        mock_response_json = {
            "DE": [
                "DrivingLicence",
                "IdentityCard",
                "Passport",
                "ResidencePermit"
            ]
        }
        response_obj = Response()
        response_obj.__setattr__("status_code", 200)
        response_obj.__setattr__("_content", json.dumps(
            mock_response_json).encode("utf-8"))
        mock_requests.return_value = response_obj

        country_code = "DE"

        reponse = VerificationService().get_document_types(country_code)

        assert(reponse == {
               "DE": ["DrivingLicence", "IdentityCard", "Passport", "ResidencePermit"]
               }) 
Example 15
Project: python-functional-showcase   Author: turekj   File: test_requests.py    MIT License 6 votes vote down vote up
def test_that_next_value_is_returned_on_no_error(self, request):
        """
        Test that a value from request is returned when no error is
        raised.
        """
        result = None

        def on_next(value):
            nonlocal result
            result = value

        response = requests.Response()
        response.status_code = 200
        request.return_value = response
        r = rx_request('get', 'url')

        r.subscribe(on_next=on_next)

        self.assertEqual(response, result) 
Example 16
Project: python-functional-showcase   Author: turekj   File: test_requests.py    MIT License 6 votes vote down vote up
def test_that_completed_is_invoked_on_no_error(self, request):
        """
        Test that completed callback is invoked on no error.
        """
        completed = False

        def on_completed():
            nonlocal completed
            completed = True

        response = requests.Response()
        response.status_code = 200
        request.return_value = response
        r = rx_request('get', 'url')

        r.subscribe(on_completed=on_completed)

        self.assertTrue(completed) 
Example 17
Project: python-functional-showcase   Author: turekj   File: test_requests.py    MIT License 6 votes vote down vote up
def test_that_error_callback_is_not_invoked_on_no_error(self, request):
        """
        Test that error callback is not invoked on no error.
        """
        error = False

        def on_error(_):
            nonlocal error
            error = True

        response = requests.Response()
        response.status_code = 200
        request.return_value = response
        r = rx_request('get', 'url')

        r.subscribe(on_error=on_error)

        self.assertFalse(error) 
Example 18
Project: codechef-cli   Author: sk364   File: helpers.py    GNU General Public License v3.0 6 votes vote down vote up
def request(session, method, url, **kwargs):
    """
    :desc: Custom wrapper method to add a timeout message
           when there is a `requests.exceptions.ConnectionError`
           or `requests.ReadTimeout` exception.
    :param: `session` requests.Session object
            `method` HTTP method to use
            `url` name of the URL
    :return: requests.Response object.
    """

    try:
        return session.request(method=method, url=url, timeout=(5, 5), **kwargs)
    except (ConnectionError, ReadTimeout):
        print(INTERNET_DOWN_MSG)
        sys.exit(1) 
Example 19
Project: codechef-cli   Author: sk364   File: helpers.py    GNU General Public License v3.0 6 votes vote down vote up
def print_response(data_type='text', code=200, data=None, extra=None, pager=False, inverse=False):
    """
    :desc: Prints response to user.
    :param: `data_type` Type of data
            `data` Data to print
            `extra` Extra messages to print
            `code` Response code
    """

    color = None

    if code == 503:
        data = SERVER_DOWN_MSG
        color = 'FAIL'
    elif code == 404 or code == 400:
        color = 'WARNING'
    elif code == 401:
        color = 'FAIL'
        data = UNAUTHORIZED_MSG

    print_response_util(data, extra, data_type, color, is_pager=pager, inverse=inverse) 
Example 20
Project: drydock   Author: airshipit   File: promenade_client.py    Apache License 2.0 5 votes vote down vote up
def put(self, endpoint, query=None, body=None, data=None, timeout=None):
        """
        Send a PUT request to Promenade. If both body and data are specified,
        body will be used.

        :param string endpoint: The URL string following the hostname and API prefix
        :param dict query: A dict of k, v parameters to add to the query string
        :param string body: A string to use as the request body. Will be treated as raw
        :param data: Something json.dumps(s) can serialize. Result will be used as the request body
        :param timeout: A single or tuple value for connect, read timeout.
            A single value indicates the read timeout only
        :return: A requests.Response object
        """
        auth_refresh = False
        url = self.base_url + endpoint
        while True:
            self.logger.debug('PUT ' + url)
            self.logger.debug('Query Params: ' + str(query))
            if body is not None:
                self.logger.debug(
                    "Sending PUT with explicit body: \n%s" % body)
                resp = self.__session.put(
                    self.base_url + endpoint,
                    params=query,
                    data=body,
                    timeout=self._timeout(timeout))
            else:
                self.logger.debug(
                    "Sending PUT with JSON body: \n%s" % str(data))
                resp = self.__session.put(
                    self.base_url + endpoint,
                    params=query,
                    json=data,
                    timeout=self._timeout(timeout))
            if resp.status_code == 401 and not auth_refresh:
                self.set_auth()
                auth_refresh = True
            else:
                break

        return resp 
Example 21
Project: drydock   Author: airshipit   File: promenade_client.py    Apache License 2.0 5 votes vote down vote up
def post(self, endpoint, query=None, body=None, data=None, timeout=None):
        """
        Send a POST request to Drydock. If both body and data are specified,
        body will be used.

        :param string endpoint: The URL string following the hostname and API prefix
        :param dict query: A dict of k, v parameters to add to the query string
        :param string body: A string to use as the request body. Will be treated as raw
        :param data: Something json.dumps(s) can serialize. Result will be used as the request body
        :param timeout: A single or tuple value for connect, read timeout.
            A single value indicates the read timeout only
        :return: A requests.Response object
        """
        auth_refresh = False
        url = self.base_url + endpoint
        while True:
            self.logger.debug('POST ' + url)
            self.logger.debug('Query Params: ' + str(query))
            if body is not None:
                self.logger.debug(
                    "Sending POST with explicit body: \n%s" % body)
                resp = self.__session.post(
                    self.base_url + endpoint,
                    params=query,
                    data=body,
                    timeout=self._timeout(timeout))
            else:
                self.logger.debug(
                    "Sending POST with JSON body: \n%s" % str(data))
                resp = self.__session.post(
                    self.base_url + endpoint,
                    params=query,
                    json=data,
                    timeout=self._timeout(timeout))
            if resp.status_code == 401 and not auth_refresh:
                self.set_auth()
                auth_refresh = True
            else:
                break

        return resp 
Example 22
Project: drydock   Author: airshipit   File: session.py    Apache License 2.0 5 votes vote down vote up
def post(self, endpoint, query=None, body=None, data=None, timeout=None):
        """
        Send a POST request to Drydock. If both body and data are specified,
        body will will be used.

        :param string endpoint: The URL string following the hostname and API prefix
        :param dict query: A dict of k, v parameters to add to the query string
        :param string body: A string to use as the request body. Will be treated as raw
        :param data: Something json.dumps(s) can serialize. Result will be used as the request body
        :param timeout: A single or tuple value for connect, read timeout.
            A single value indicates the read timeout only
        :return: A requests.Response object
        """
        auth_refresh = False
        url = self.base_url + endpoint
        while True:
            self.logger.debug('POST ' + url)
            self.logger.debug('Query Params: ' + str(query))
            if body is not None:
                self.logger.debug(
                    "Sending POST with explicit body: \n%s" % body)
                resp = self.__session.post(
                    self.base_url + endpoint,
                    params=query,
                    data=body,
                    timeout=self._timeout(timeout))
            else:
                self.logger.debug(
                    "Sending POST with JSON body: \n%s" % str(data))
                resp = self.__session.post(
                    self.base_url + endpoint,
                    params=query,
                    json=data,
                    timeout=self._timeout(timeout))
            if resp.status_code == 401 and not auth_refresh:
                self.set_auth()
                auth_refresh = True
            else:
                break

        return resp 
Example 23
Project: zmirror   Author: aploium   File: threadlocal.py    MIT License 5 votes vote down vote up
def remote_response(self):
        """
        远程服务器的响应, 对象, requests.Response
        :rtype: requests.Response
        """
        return self.__getattribute__("_remote_response") 
Example 24
Project: zmirror   Author: aploium   File: threadlocal.py    MIT License 5 votes vote down vote up
def remote_response(self, value):
        """:type value: requests.Response"""
        self.__setattr__("_remote_response", value) 
Example 25
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 5 votes vote down vote up
def test_homepage(self):
        """https://httpbin.org/"""

        self.rv = self.client.get(
            self.url("/"),
            environ_base=env(),
            headers=headers(),
        )  # type: Response
        self.assertIn(b'httpbin', self.rv.data, msg=self.dump()) 
Example 26
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 5 votes vote down vote up
def test__enable_keep_alive_per_domain(self):
        """https://httpbin.org/"""
        self.reload_zmirror({"enable_keep_alive_per_domain": True})

        self.rv = self.client.get(
            self.url("/"),
            environ_base=env(),
            headers=headers(),
        )  # type: Response
        self.assertIn(b'httpbin', self.rv.data, msg=self.dump()) 
Example 27
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 5 votes vote down vote up
def test_main_domain_as_external(self):
        self.rv = self.client.get(
            self.url("/extdomains//" + self.C.target_domain),
            environ_base=env(),
            headers=headers(),
        )  # type: Response
        self.assertEqual(307, self.rv.status_code, self.dump()) 
Example 28
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 5 votes vote down vote up
def test_user_agent(self):
        """https://httpbin.org/user-agent"""

        self.rv = self.client.get(
            self.url("/user-agent"),
            environ_base=env(),
            headers=headers(),
        )  # type: Response

        self.assertEqual(load_rv_json(self.rv)['user-agent'], DEFAULT_USER_AGENT, msg=self.dump()) 
Example 29
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 5 votes vote down vote up
def test_remote_set_cookie(self):
        """https://httpbin.org/cookies/set?name=value"""
        self.rv = self.client.get(
            self.url("/cookies/set?k1=value1&k2=value2"),
            environ_base=env(),
            headers=headers(),
        )  # type: Response

        self.assertEqual(2, len(self.rv.headers.get_all("Set-Cookie")), msg=self.dump())
        for set_cookie_header in self.rv.headers.get_all("Set-Cookie"):
            if not ("k1=value1" in set_cookie_header
                    or "k2=value2" in set_cookie_header):
                raise ValueError("cookie set error" + self.dump())
        self.assertEqual(302, self.rv.status_code, msg=self.dump()) 
Example 30
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 5 votes vote down vote up
def test_relative_redirect_to(self):
        """https://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F"""
        self.rv = self.client.get(
            self.url("/redirect-to"),
            query_string="url=http%3A%2F%2Fexample.com%2F",
            environ_base=env(),
            headers=headers(),
        )  # type: Response

        self.assertIn("example.com", self.rv.location, msg=self.dump()) 
Example 31
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 5 votes vote down vote up
def test_relative_redirect_to_2(self):
        """https://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F"""
        self.rv = self.client.get(
            self.url("/redirect-to"),
            query_string="url=http%3A%2F%2Feu.httpbin.org%2F",
            environ_base=env(),
            headers=headers(),
        )  # type: Response
        self.assertEqual(self.url("/extdomains/eu.httpbin.org/"), self.rv.location, msg=self.dump()) 
Example 32
Project: anipy   Author: twissell-   File: core.py    MIT License 5 votes vote down vote up
def get(self, endpoint=None, data=None, headers=None):
        """
        *Helper.* Calls :any:`request` with `method='GET'`

        :param endpoint: See :any:`request`.
        :param data: See :any:`request`.
        :param headers: See :any:`request`.
        :return: (:obj:`dict`) Response.
        """

        return self.request('GET', endpoint=endpoint, data=data, headers=headers) 
Example 33
Project: anipy   Author: twissell-   File: core.py    MIT License 5 votes vote down vote up
def post(self, endpoint=None, data=None, headers=None):
        """
        *Helper.* Calls :any:`request` with `method='POST'`

        :param endpoint: See :any:`request`.
        :param data: See :any:`request`.
        :param headers: See :any:`request`.
        :return: (:obj:`dict`) Response.
        """

        return self.request('POST', endpoint=endpoint, data=data, headers=headers) 
Example 34
Project: anipy   Author: twissell-   File: core.py    MIT License 5 votes vote down vote up
def put(self, endpoint=None, data=None, headers=None):
        """
        *Helper.* Calls :any:`request` with `method='PUT'`

        :param endpoint: See :any:`request`.
        :param data: See :any:`request`.
        :param headers: See :any:`request`.
        :return: (:obj:`dict`) Response.
        """

        return self.request('PUT', endpoint=endpoint, data=data, headers=headers) 
Example 35
Project: anipy   Author: twissell-   File: core.py    MIT License 5 votes vote down vote up
def delete(self, endpoint=None, data=None, headers=None):
        """
        *Helper.* Calls :any:`request` with `method='DELETE'`

        :param endpoint: See :any:`request`.
        :param data: See :any:`request`.
        :param headers: See :any:`request`.
        :return: (:obj:`dict`) Response.
        """

        return self.request('DELETE', endpoint=endpoint, data=data, headers=headers) 
Example 36
Project: kegg   Author: bio2bel   File: description.py    MIT License 5 votes vote down vote up
def parse_description(response: Response):
    """Parse the several properties in the description file given an KEGG identifier using the KEGG API.

    :rtype: dict
    :return: description dictionary
    """
    description = {}

    for line in response.iter_lines():
        line = line.decode('utf-8')

        if not line.startswith(' '):
            keyword = get_first_word(line)

        if keyword == 'ENTRY':
            description['ENTRY'] = parse_entry_line(line)

        elif keyword == 'NAME':
            entry_name = parse_entry_line(line)
            if entry_name:
                # If there is a name, take the first element of the tuple and strip semi colon
                # in case there are multiple names
                description['ENTRY_NAME'] = entry_name[0].strip(';')

        elif keyword == 'PATHWAY':

            if 'PATHWAY' not in description:
                description['PATHWAY'] = [parse_pathway_line(line)]
            else:
                description['PATHWAY'].append(parse_pathway_line(line))

        elif keyword == 'DBLINKS':

            if 'DBLINKS' not in description:
                description['DBLINKS'] = [parse_link_line(line)]
            else:
                description['DBLINKS'].append(parse_link_line(line))

    return description 
Example 37
Project: PickTrue   Author: winkidney   File: abstract.py    MIT License 5 votes vote down vote up
def get(self, url, **kwargs):
        """
        :rtype: requests.Response
        """
        if 'timeout' in kwargs:
            kwargs.pop('timeout')
        return self.session.get(url, timeout=(2, 30), **kwargs) 
Example 38
Project: bigquerylayers   Author: smandaric   File: requests.py    GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, url, method='GET', body=None, headers=None,
                 timeout=None, **kwargs):
        """Make an HTTP request using requests.

        Args:
            url (str): The URI to be requested.
            method (str): The HTTP method to use for the request. Defaults
                to 'GET'.
            body (bytes): The payload / body in HTTP request.
            headers (Mapping[str, str]): Request headers.
            timeout (Optional[int]): The number of seconds to wait for a
                response from the server. If not specified or if None, the
                requests default timeout will be used.
            kwargs: Additional arguments passed through to the underlying
                requests :meth:`~requests.Session.request` method.

        Returns:
            google.auth.transport.Response: The HTTP response.

        Raises:
            google.auth.exceptions.TransportError: If any exception occurred.
        """
        try:
            _LOGGER.debug('Making request: %s %s', method, url)
            response = self.session.request(
                method, url, data=body, headers=headers, timeout=timeout,
                **kwargs)
            return _Response(response)
        except requests.exceptions.RequestException as caught_exc:
            new_exc = exceptions.TransportError(caught_exc)
            six.raise_from(new_exc, caught_exc) 
Example 39
Project: bigquerylayers   Author: smandaric   File: upload.py    GNU General Public License v3.0 5 votes vote down vote up
def transmit(self, transport, data, metadata, content_type):
        """Transmit the resource to be uploaded.

        Args:
            transport (~requests.Session): A ``requests`` object which can
                make authenticated requests.
            data (bytes): The resource content to be uploaded.
            metadata (Mapping[str, str]): The resource metadata, such as an
                ACL list.
            content_type (str): The content type of the resource, e.g. a JPEG
                image has content type ``image/jpeg``.

        Returns:
            ~requests.Response: The HTTP response returned by ``transport``.
        """
        method, url, payload, headers = self._prepare_request(
            data, metadata, content_type
        )
        response = _helpers.http_request(
            transport,
            method,
            url,
            data=payload,
            headers=headers,
            retry_strategy=self._retry_strategy,
        )
        self._process_response(response)
        return response 
Example 40
Project: bigquerylayers   Author: smandaric   File: upload.py    GNU General Public License v3.0 5 votes vote down vote up
def recover(self, transport):
        """Recover from a failure.

        This method should be used when a :class:`ResumableUpload` is in an
        :attr:`~ResumableUpload.invalid` state due to a request failure.

        This will verify the progress with the server and make sure the
        current upload is in a valid state before :meth:`transmit_next_chunk`
        can be used again.

        Args:
            transport (~requests.Session): A ``requests`` object which can
                make authenticated requests.

        Returns:
            ~requests.Response: The HTTP response returned by ``transport``.
        """
        method, url, payload, headers = self._prepare_recover_request()
        # NOTE: We assume "payload is None" but pass it along anyway.
        response = _helpers.http_request(
            transport,
            method,
            url,
            data=payload,
            headers=headers,
            retry_strategy=self._retry_strategy,
        )
        self._process_recover_response(response)
        return response 
Example 41
Project: dbapi   Author: acrazing   File: base.py    MIT License 5 votes vote down vote up
def req(self, url, method='get', params=None, data=None, auth=False):
        """
        请求API

        :type url: str
        :param url: API
        
        :type method: str
        :param method: HTTP METHOD
        
        :type params: dict
        :param params: query
        
        :type data: dict
        :param data: body
        
        :type auth: bool
        :param auth: if True and session expired will raise exception
        
        :rtype: requests.Response
        :return: Response
        """
        self.logger.debug('fetch api<%s:%s>' % (method, url))
        if auth and self.user_alias is None:
            raise Exception('cannot fetch api<%s> without session' % url)
        s = requests.Session()
        r = s.request(method, url, params=params, data=data, cookies=self.cookies, headers=self.headers,
                      timeout=self.timeout)
        s.close()
        if r.url is not url and RE_SESSION_EXPIRE.search(r.url) is not None:
            self.expire()
            if auth:
                raise Exception('auth expired, could not fetch with<%s>' % url)
        return r 
Example 42
Project: bendercoin   Author: matejcik   File: util.py    MIT License 5 votes vote down vote up
def print_json(j):
    try:
        if isinstance(j, requests.Response):
            j = j.json()
        s = json.dumps(j, sort_keys=True, indent=4)
        print(s)
    except Exception as e:
        print("could not decode json:", e)
        print(j) 
Example 43
Project: jupyterlab_code_formatter   Author: ryantam626   File: test_handlers.py    MIT License 5 votes vote down vote up
def _format_code_request(
        self, formatter: str, code: t.List[str], options: t.Dict[str, t.Any]
    ) -> requests.Response:
        return self.request(
            verb="POST",
            path="/jupyterlab_code_formatter/format",
            data=json.dumps(
                {"code": code, "options": options, "formatter": formatter,}
            ),
        ) 
Example 44
Project: botbuilder-python   Author: microsoft   File: direct_line_client.py    MIT License 5 votes vote down vote up
def get_message(self, retry_count: int = 3) -> Tuple[Response, str]:
        """Get a response message back from the bot framework using direct line api"""

        url = "/".join(
            [self._base_url, "conversations", self._conversation_id, "activities"]
        )
        url = url + "?watermark=" + self._watermark

        success = False
        current_retry = 0
        bot_response = None
        while not success and current_retry < retry_count:
            bot_response = requests.get(
                url,
                headers=self._headers,
                json={"conversationId": self._conversation_id},
            )
            current_retry += 1
            if bot_response.status_code == 200:
                success = True
                json_response = bot_response.json()

                if "watermark" in json_response:
                    self._watermark = json_response["watermark"]

                if "activities" in json_response:
                    activities_count = len(json_response["activities"])
                    if activities_count > 0:
                        return (
                            bot_response,
                            json_response["activities"][activities_count - 1]["text"],
                        )
                    return bot_response, "No new messages"
        return bot_response, "error contacting bot for response" 
Example 45
Project: vr900-connector   Author: thomasgermain   File: apierror.py    MIT License 5 votes vote down vote up
def __init__(self, message: str, response: Response, payload=None):
        self.message = message
        self.response = response
        self.payload = payload 
Example 46
Project: yuque-py   Author: Zheaoli   File: client.py    MIT License 5 votes vote down vote up
def _get_request(
        request_url: str,
        requests_data: typing.Dict[str, typing.Any],
        request_header: typing.Dict[str, str],
    ) -> requests.Response:
        if requests_data:
            request_url = f"{request_url}&{urlencode(requests_data)}"

        return requests.get(request_url, headers=request_header) 
Example 47
Project: yuque-py   Author: Zheaoli   File: client.py    MIT License 5 votes vote down vote up
def _post_request(
        request_url: str,
        requests_data: typing.Dict[str, typing.Any],
        request_header: typing.Dict[str, str],
    ) -> requests.Response:
        return requests.post(request_url, json=requests_data, headers=request_header) 
Example 48
Project: yuque-py   Author: Zheaoli   File: client.py    MIT License 5 votes vote down vote up
def _put_request(
        request_url: str,
        requests_data: typing.Dict[str, typing.Any],
        request_header: typing.Dict[str, str],
    ) -> requests.Response:
        return requests.put(request_url, json=requests_data, headers=request_header) 
Example 49
Project: yuque-py   Author: Zheaoli   File: client.py    MIT License 5 votes vote down vote up
def _delete_request(
        request_url: str,
        requests_data: typing.Dict[str, typing.Any],
        request_header: typing.Dict[str, str],
    ) -> requests.Response:
        return requests.delete(request_url, headers=request_header) 
Example 50
Project: clashroyale   Author: cgrok   File: test_blocking.py    MIT License 5 votes vote down vote up
def test_get_response(self):
        """This test will test out:
        - BaseAttrDict.response
        """
        tag = '2P0LYQ'
        chests = self.cr.get_player_chests(tag)
        self.assertTrue(isinstance(chests.response, requests.Response)) 
Example 51
Project: pyatn-client   Author: ATNIO   File: atn.py    MIT License 5 votes vote down vote up
def on_cooperative_close_denied(self, dbot_address: str, response: Response = None) -> None:
        """Call back function when no valid closing signature received

        This function will be called when can not get valid closing signature in
        method `close_channel`

        :param dbot_address: address of the DBot contract
        :param response: response from DBot server when request closing signature
        """
        logger.warning('No valid closing signature received from DBot server({}).\n{}'.format(dbot_address, response.text))
        logger.warning('Closing noncooperatively on a balance of 0.')
        # if cooperative close denied, client close the channel with balance 0 unilaterally
        self.uncooperative_close_channel(dbot_address, 0) 
Example 52
Project: pyatn-client   Author: ATNIO   File: atn.py    MIT License 5 votes vote down vote up
def _request(
            self,
            channel: Channel,
            method: str,
            url: str,
            **requests_kwargs
    ) -> Tuple[Union[None, Response], bool]:
        """
        Performs a simple request to the HTTP server with headers representing the given
        channel state.
        """
        headers = Munch()
        headers.contract_address = self.channel_client.context.channel_manager.address
        if channel is not None:
            headers.balance = str(channel.balance)
            headers.balance_signature = encode_hex(channel.balance_sig)
            headers.sender_address = channel.sender
            headers.receiver_address = channel.receiver
            headers.open_block = str(channel.block)

        headers = HTTPHeaders.serialize(headers)
        if 'headers' in requests_kwargs:
            headers.update(requests_kwargs['headers'])
            requests_kwargs['headers'] = headers
        else:
            requests_kwargs['headers'] = headers
        return requests.request(method, url, **requests_kwargs) 
Example 53
Project: OAuth2Client   Author: antechrestos   File: credentials_manager.py    Apache License 2.0 5 votes vote down vote up
def _handle_bad_response(response: Response):
        try:
            error = response.json()
            raise OAuthError(response.status_code, error.get('error'), error.get('error_description'))
        except BaseException as ex:
            if type(ex) != OAuthError:
                _logger.exception(
                    '_handle_bad_response - error while getting error as json - %s - %s' % (type(ex), str(ex)))
                raise OAuthError(response.status_code, 'unknown_error', response.text)
            else:
                raise 
Example 54
Project: OAuth2Client   Author: antechrestos   File: credentials_manager.py    Apache License 2.0 5 votes vote down vote up
def get(self, url: str, params: Optional[dict] = None, **kwargs) -> Response:
        kwargs['params'] = params
        return self._bearer_request(self._get_session().get, url, **kwargs) 
Example 55
Project: OAuth2Client   Author: antechrestos   File: credentials_manager.py    Apache License 2.0 5 votes vote down vote up
def post(self, url: str, data: Optional[Any] = None, json: Optional[Any] = None, **kwargs) -> Response:
        kwargs['data'] = data
        kwargs['json'] = json
        return self._bearer_request(self._get_session().post, url, **kwargs) 
Example 56
Project: OAuth2Client   Author: antechrestos   File: credentials_manager.py    Apache License 2.0 5 votes vote down vote up
def put(self, url: str, data: Optional[Any] = None, json: Optional[Any] = None, **kwargs) -> Response:
        kwargs['data'] = data
        kwargs['json'] = json
        return self._bearer_request(self._get_session().put, url, **kwargs) 
Example 57
Project: OAuth2Client   Author: antechrestos   File: credentials_manager.py    Apache License 2.0 5 votes vote down vote up
def patch(self, url: str, data: Optional[Any] = None, json: Optional[Any] = None, **kwargs) -> Response:
        kwargs['data'] = data
        kwargs['json'] = json
        return self._bearer_request(self._get_session().patch, url, **kwargs) 
Example 58
Project: OAuth2Client   Author: antechrestos   File: credentials_manager.py    Apache License 2.0 5 votes vote down vote up
def _bearer_request(self, method: Callable[[Any], Response], url: str, **kwargs) -> Response:
        headers = kwargs.get('headers', None)
        if headers is None:
            headers = dict()
            kwargs['headers'] = headers
        _logger.debug("_bearer_request on %s - %s" % (method.__name__, url))
        response = method(url, **kwargs)
        if self.refresh_token is not None and self._is_token_expired(response):
            self._refresh_token()
            return method(url, **kwargs)
        else:
            return response 
Example 59
Project: OAuth2Client   Author: antechrestos   File: credentials_manager.py    Apache License 2.0 5 votes vote down vote up
def _is_token_expired(response: Response) -> bool:
        if response.status_code == HTTPStatus.UNAUTHORIZED.value:
            try:
                json_data = response.json()
                return json_data.get('error', '') == 'invalid_token'
            except BaseException:
                return False
        else:
            return False 
Example 60
Project: uplink   Author: prkumar   File: conftest.py    MIT License 5 votes vote down vote up
def mock_response(mocker):
    response = mocker.Mock(spec=requests.Response)
    return MockResponse(response) 
Example 61
Project: aiolocust   Author: kpidata   File: clients.py    MIT License 5 votes vote down vote up
def raise_for_status(self):
        if hasattr(self, 'error') and self.error:
            raise self.error
        Response.raise_for_status(self) 
Example 62
Project: kubeshift   Author: cdrage   File: helper.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def make_response(code, content):
    r = requests.Response()
    r.status_code = code
    if content is not None:
        r.raw = six.BytesIO(six.b(json.dumps(content)))
    return r 
Example 63
Project: ripe-atlas-tools   Author: RIPE-NCC   File: probe_search.py    GNU General Public License v3.0 5 votes vote down vote up
def test_location_google_wrong_output(self):
        """User passed location arg but google api gave not expected format"""
        with mock.patch('requests.get') as mock_get:
            mock_get.return_value = requests.Response()
            with mock.patch('requests.Response.json') as mock_json:
                mock_json.return_value = {"blaaa": "bla"}
                with self.assertRaises(RipeAtlasToolsException):
                    cmd = Command()
                    cmd.init_args(["--location", "blaaaa"])
                    cmd.run() 
Example 64
Project: ripe-atlas-tools   Author: RIPE-NCC   File: probe_search.py    GNU General Public License v3.0 5 votes vote down vote up
def test_location_arg(self):
        """User passed location arg"""
        with mock.patch('requests.get') as mock_get:
            mock_get.return_value = requests.Response()
            with mock.patch('requests.Response.json') as mock_json:
                mock_json.return_value = {"results": [
                    {"geometry": {"location": {"lat": 1, "lng": 2}}}]}
                cmd = Command()
                cmd.init_args(["--location", "blaaaa"])
                self.assertEquals(cmd.build_request_args(), {'radius': '1,2:15'}) 
Example 65
Project: ripe-atlas-tools   Author: RIPE-NCC   File: probe_search.py    GNU General Public License v3.0 5 votes vote down vote up
def test_location_arg_with_radius(self):
        """User passed location arg"""
        with mock.patch('requests.get') as mock_get:
            mock_get.return_value = requests.Response()
            with mock.patch('requests.Response.json') as mock_json:
                mock_json.return_value = {"results": [
                    {"geometry": {"location": {"lat": 1, "lng": 2}}}
                ]}
                cmd = Command()
                cmd.init_args(["--location", "blaaaa", "--radius", "4"])
                self.assertEquals(
                    cmd.build_request_args(),
                    {"radius": "1,2:4"}
                ) 
Example 66
Project: instaloader   Author: instaloader   File: instaloadercontext.py    MIT License 5 votes vote down vote up
def write_raw(self, resp: Union[bytes, requests.Response], filename: str) -> None:
        """Write raw response data into a file.

        .. versionadded:: 4.2.1"""
        self.log(filename, end=' ', flush=True)
        with open(filename, 'wb') as file:
            if isinstance(resp, requests.Response):
                shutil.copyfileobj(resp.raw, file)
            else:
                file.write(resp) 
Example 67
Project: spectacles   Author: spectacles-ci   File: utils.py    MIT License 5 votes vote down vote up
def details_from_http_error(response: requests.Response) -> str:
    try:
        response_json = response.json()
    # Requests raises a ValueError if the response is invalid JSON
    except ValueError:
        details = ""
    else:
        details = response_json.get("message")
    details = details.strip() if details else ""
    return details 
Example 68
Project: spectacles   Author: spectacles-ci   File: test_client.py    MIT License 5 votes vote down vote up
def mock_response():
    mock = Mock(spec=requests.Response)
    mock.status_code = 404
    mock.raise_for_status.side_effect = requests.exceptions.HTTPError(
        "An HTTP error occurred."
    )
    return mock 
Example 69
Project: spectacles   Author: spectacles-ci   File: test_client.py    MIT License 5 votes vote down vote up
def test_authenticate_sets_session_headers(mock_post, monkeypatch):
    mock_looker_version = Mock(spec=LookerClient.get_looker_release_version)
    mock_looker_version.return_value("1.2.3")
    monkeypatch.setattr(LookerClient, "get_looker_release_version", mock_looker_version)

    mock_post_response = Mock(spec=requests.Response)
    mock_post_response.json.return_value = {"access_token": "test_access_token"}
    mock_post.return_value = mock_post_response
    client = LookerClient(TEST_BASE_URL, TEST_CLIENT_ID, TEST_CLIENT_SECRET)
    assert client.session.headers == {"Authorization": f"token test_access_token"} 
Example 70
Project: nexus3-cli   Author: thiagofigueiro   File: nexus_client.py    MIT License 5 votes vote down vote up
def http_request(self, method, endpoint, service_url=None, **kwargs):
        """
        Performs a HTTP request to the Nexus REST API on the specified
        endpoint.

        :param method: one of ``get``, ``put``, ``post``, ``delete``.
        :type endpoint: str
        :param endpoint: URI path to be appended to the service URL.
        :type endpoint: str
        :param service_url: override the default URL to use for the request,
            which is created by joining :attr:`rest_url` and ``endpoint``.
        :type service_url: str
        :param kwargs: as per :py:func:`requests.request`.
        :rtype: requests.Response
        """
        service_url = service_url or self.rest_url
        url = urljoin(service_url, endpoint)

        try:
            response = requests.request(
                method=method, auth=self.config.auth, url=url,
                verify=self.config.x509_verify, **kwargs)
        except requests.exceptions.ConnectionError as e:
            raise exception.NexusClientConnectionError(str(e)) from None

        if response.status_code == 401:
            raise exception.NexusClientInvalidCredentials(
                'Try running `nexus3 login`')

        return response 
Example 71
Project: nexus3-cli   Author: thiagofigueiro   File: nexus_client.py    MIT License 5 votes vote down vote up
def http_get(self, endpoint):
        """
        Performs a HTTP GET request on the given endpoint.

        :param endpoint: name of the Nexus REST API endpoint.
        :type endpoint: str
        :rtype: requests.Response
        """
        return self.http_request('get', endpoint, stream=True) 
Example 72
Project: nexus3-cli   Author: thiagofigueiro   File: nexus_client.py    MIT License 5 votes vote down vote up
def http_head(self, endpoint):
        """
        Performs a HTTP HEAD request on the given endpoint.

        :param endpoint: name of the Nexus REST API endpoint.
        :type endpoint: str
        :rtype: requests.Response
        """
        return self.http_request('head', endpoint) 
Example 73
Project: nexus3-cli   Author: thiagofigueiro   File: nexus_client.py    MIT License 5 votes vote down vote up
def http_post(self, endpoint, **kwargs):
        """
        Performs a HTTP POST request on the given endpoint.

        :param endpoint: name of the Nexus REST API endpoint.
        :type endpoint: str
        :param kwargs: as per :py:func:`requests.request`.
        :rtype: requests.Response
        """
        return self.http_request('post', endpoint, **kwargs) 
Example 74
Project: nexus3-cli   Author: thiagofigueiro   File: nexus_client.py    MIT License 5 votes vote down vote up
def http_put(self, endpoint, **kwargs):
        """
        Performs a HTTP PUT request on the given endpoint.

        :param endpoint: name of the Nexus REST API endpoint.
        :type endpoint: str
        :param kwargs: as per :py:func:`requests.request`.
        :rtype: requests.Response
        """
        return self.http_request('put', endpoint, **kwargs) 
Example 75
Project: zmirror   Author: aploium   File: test_httpbin.py    MIT License 4 votes vote down vote up
def test_headers(self):
        """https://httpbin.org/headers"""
        with self.app.test_client() as c:
            self.rv = c.get(
                self.url("/headers"),
                environ_base=env(),
                headers=headers(
                    accept_encoding="gzip, deflate, sdch, br",
                    others={
                        "Host": self.C.my_host_name,
                        "Referer": self.url("/extdomains/eu.httpbin.org/headers"),
                        "Cookie": "_ga=GA1.2.1161994079.1471765883",
                        "Hello-World": "love_luciaz",
                    }),
            )  # type: Response

            # 白盒检查
            parse_values = attributes(self.zmirror.parse)
            self.assertEqual("application/json", self.zmirror.parse.content_type, msg=self.dump())

            self.assertEqual(
                "gzip, deflate",
                self.zmirror.parse.client_header['accept-encoding'],
                msg=parse_values
            )
            self.assertEqual(
                "https://eu.httpbin.org/headers",
                self.zmirror.parse.client_header['referer'],
                msg=self.dump()
            )
            self.assertEqual(
                "love_luciaz",
                self.zmirror.parse.client_header['hello-world'],
                msg=self.dump()
            )
            self.assertEqual("httpbin.org", self.zmirror.parse.remote_domain, msg=self.dump())
            self.assertEqual("/headers", self.zmirror.parse.remote_path, msg=self.dump())

            remote_resp = self.zmirror.parse.remote_response  # type: requests.Response
            remote_resp_json = json.loads(remote_resp.text)  # type: dict
            self.assertEqual(self.C.target_domain, remote_resp_json['headers']['Host'], msg=self.dump())

            # 黑盒检查
            h = load_rv_json(self.rv)['headers']
            self.assertEqual(self.C.my_host_name, h['Host'], msg=self.dump())
            self.assertEqual(self.url("/extdomains/eu.httpbin.org/headers"), h['Referer'], msg=self.dump())
            self.assertEqual("_ga=GA1.2.1161994079.1471765883", h['Cookie'], msg=self.dump())
            self.assertEqual("love_luciaz", h['Hello-World'], msg=self.dump())
            self.assertEqual("gzip, deflate", h['Accept-Encoding'], msg=self.dump()) 
Example 76
Project: anipy   Author: twissell-   File: core.py    MIT License 4 votes vote down vote up
def request(self, method, endpoint=None, data=None, headers=None):
        """
        Makes a *method* request to *endpoint* with *data* and *headers*.

        :param method: (:obj:`str`) String for the http method: GET, POST, PUT DELETE. Other methods are not supported.

        :param endpoint: (:obj:`str`, optional) String for the endpoint where the request aims. Remember, all endpoints
            refers to :any:`_URL`.

            If none, request will aim to :any:`_ENDPOINT`.

            Example: `'/api/user/demo/animelist/'`

        :param data: (:obj:`dict`, optional) Parameters to be included in the request.

            If none, no parameters will be sent.

        :param headers: (:obj:`dict`, optional) Headers to be included in the request.

            If none, default parameters will be used (see :any:`_headers`).

        :raise: See :any:`raise_from_response`

        :return: (:obj:`dict`) Response.
        """

        headers = headers or self._headers
        endpoint = endpoint or self._ENDPOINT
        data = dic_to_json(data)

        logger.debug('Resource request: %s %s' % (method, endpoint))
        logger.debug('Resource request body: %s' % str(data))
        logger.debug('Resource request headers: %s' % headers)

        response = self._pool.request(
            method,
            endpoint,
            body=data,
            headers=headers)
        raise_from_response(response)

        response = response_to_dic(response)
        logger.debug('Resource response: \n' + pprint.pformat(response))
        return response 
Example 77
Project: bigquerylayers   Author: smandaric   File: upload.py    GNU General Public License v3.0 4 votes vote down vote up
def initiate(
        self,
        transport,
        stream,
        metadata,
        content_type,
        total_bytes=None,
        stream_final=True,
    ):
        """Initiate a resumable upload.

        By default, this method assumes your ``stream`` is in a "final"
        state ready to transmit. However, ``stream_final=False`` can be used
        to indicate that the size of the resource is not known. This can happen
        if bytes are being dynamically fed into ``stream``, e.g. if the stream
        is attached to application logs.

        If ``stream_final=False`` is used, :attr:`chunk_size` bytes will be
        read from the stream every time :meth:`transmit_next_chunk` is called.
        If one of those reads produces strictly fewer bites than the chunk
        size, the upload will be concluded.

        Args:
            transport (~requests.Session): A ``requests`` object which can
                make authenticated requests.
            stream (IO[bytes]): The stream (i.e. file-like object) that will
                be uploaded. The stream **must** be at the beginning (i.e.
                ``stream.tell() == 0``).
            metadata (Mapping[str, str]): The resource metadata, such as an
                ACL list.
            content_type (str): The content type of the resource, e.g. a JPEG
                image has content type ``image/jpeg``.
            total_bytes (Optional[int]): The total number of bytes to be
                uploaded. If specified, the upload size **will not** be
                determined from the stream (even if ``stream_final=True``).
            stream_final (Optional[bool]): Indicates if the ``stream`` is
                "final" (i.e. no more bytes will be added to it). In this case
                we determine the upload size from the size of the stream. If
                ``total_bytes`` is passed, this argument will be ignored.

        Returns:
            ~requests.Response: The HTTP response returned by ``transport``.
        """
        method, url, payload, headers = self._prepare_initiate_request(
            stream,
            metadata,
            content_type,
            total_bytes=total_bytes,
            stream_final=stream_final,
        )
        response = _helpers.http_request(
            transport,
            method,
            url,
            data=payload,
            headers=headers,
            retry_strategy=self._retry_strategy,
        )
        self._process_initiate_response(response)
        return response 
Example 78
Project: misp42splunk   Author: remg427   File: validators.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def resolve_remote(self, uri):
        """
        Resolve a remote ``uri``.

        If called directly, does not check the store first, but after
        retrieving the document at the specified URI it will be saved in
        the store if :attr:`cache_remote` is True.

        .. note::

            If the requests_ library is present, ``jsonschema`` will use it to
            request the remote ``uri``, so that the correct encoding is
            detected and used.

            If it isn't, or if the scheme of the ``uri`` is not ``http`` or
            ``https``, UTF-8 is assumed.

        :argument str uri: the URI to resolve
        :returns: the retrieved document

        .. _requests: http://pypi.python.org/pypi/requests/

        """

        scheme = urlsplit(uri).scheme

        if scheme in self.handlers:
            result = self.handlers[scheme](uri)
        elif (
            scheme in [u"http", u"https"] and
            requests and
            getattr(requests.Response, "json", None) is not None
        ):
            # Requests has support for detecting the correct encoding of
            # json over http
            if callable(requests.Response.json):
                result = requests.get(uri).json()
            else:
                result = requests.get(uri).json
        else:
            # Otherwise, pass off to urllib and assume utf-8
            result = json.loads(urlopen(uri).read().decode("utf-8"))

        if self.cache_remote:
            self.store[uri] = result
        return result 
Example 79
Project: pycons3rt   Author: cons3rt   File: nexus.py    GNU General Public License v3.0 4 votes vote down vote up
def query_nexus(query_url, timeout_sec, basic_auth=None):
    """Queries Nexus for an artifact

    :param query_url: (str) Query URL
    :param timeout_sec: (int) query timeout
    :param basic_auth (HTTPBasicAuth) object or none
    :return: requests.Response object
    :raises: RuntimeError
    """
    log = logging.getLogger(mod_logger + '.query_nexus')

    # Attempt to query Nexus
    retry_sec = 5
    max_retries = 6
    try_num = 1
    query_success = False
    nexus_response = None
    while try_num <= max_retries:
        if query_success:
            break
        log.debug('Attempt # {n} of {m} to query the Nexus URL: {u}'.format(n=try_num, u=query_url, m=max_retries))
        try:
            nexus_response = requests.get(query_url, auth=basic_auth, stream=True, timeout=timeout_sec)
        except requests.exceptions.Timeout:
            _, ex, trace = sys.exc_info()
            msg = '{n}: Nexus initial query timed out after {t} seconds:\n{e}'.format(
                n=ex.__class__.__name__, t=timeout_sec, r=retry_sec, e=str(ex))
            log.warn(msg)
            if try_num < max_retries:
                log.info('Retrying query in {t} sec...'.format(t=retry_sec))
                time.sleep(retry_sec)
        except (requests.exceptions.RequestException, requests.exceptions.ConnectionError):
            _, ex, trace = sys.exc_info()
            msg = '{n}: Nexus initial query failed with the following exception:\n{e}'.format(
                n=ex.__class__.__name__, r=retry_sec, e=str(ex))
            log.warn(msg)
            if try_num < max_retries:
                log.info('Retrying query in {t} sec...'.format(t=retry_sec))
                time.sleep(retry_sec)
        else:
            query_success = True
        try_num += 1

    if not query_success:
        msg = 'Unable to query Nexus after {m} attempts using URL: {u}'.format(
            u=query_url, m=max_retries)
        log.error(msg)
        raise RuntimeError(msg)

    if nexus_response.status_code != 200:
        msg = 'Nexus request returned code {c}, unable to query Nexus using URL: {u}'.format(
            u=query_url, c=nexus_response.status_code)
        log.error(msg)
        raise RuntimeError(msg)
    return nexus_response 
Example 80
Project: core   Author: getavalon   File: validators.py    MIT License 4 votes vote down vote up
def resolve_remote(self, uri):
        """
        Resolve a remote ``uri``.

        Does not check the store first, but stores the retrieved document in
        the store if :attr:`RefResolver.cache_remote` is True.

        .. note::

            If the requests_ library is present, ``jsonschema`` will use it to
            request the remote ``uri``, so that the correct encoding is
            detected and used.

            If it isn't, or if the scheme of the ``uri`` is not ``http`` or
            ``https``, UTF-8 is assumed.

        :argument str uri: the URI to resolve
        :returns: the retrieved document

        .. _requests: http://pypi.python.org/pypi/requests/

        """

        scheme = urlsplit(uri).scheme

        if scheme in self.handlers:
            result = self.handlers[scheme](uri)
        elif (
            scheme in [u"http", u"https"] and
            requests and
            getattr(requests.Response, "json", None) is not None
        ):
            # Requests has support for detecting the correct encoding of
            # json over http
            if callable(requests.Response.json):
                result = requests.get(uri).json()
            else:
                result = requests.get(uri).json
        else:
            # Otherwise, pass off to urllib and assume utf-8
            result = json.loads(urlopen(uri).read().decode("utf-8"))

        if self.cache_remote:
            self.store[uri] = result
        return result