Python requests.PreparedRequest() Examples

The following are 30 code examples of requests.PreparedRequest(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module requests , or try the search function .
Example #1
Source File: resthttp.py    From py-stcrestclient with MIT License 6 votes vote down vote up
def make_url(self, container=None, resource=None, query_items=None):
        """Create a URL from the specified parts."""
        pth = [self._base_url]
        if container:
            pth.append(container.strip('/'))
        if resource:
            pth.append(resource)
        else:
            pth.append('')
        url = '/'.join(pth)
        if isinstance(query_items, (list, tuple, set)):
            url += RestHttp._list_query_str(query_items)
            query_items = None
        p = requests.PreparedRequest()
        p.prepare_url(url, query_items)
        return p.url 
Example #2
Source File: models.py    From schemathesis with MIT License 6 votes vote down vote up
def from_prepared_request(cls, prepared: requests.PreparedRequest) -> "Request":
        """A prepared request version is already stored in `requests.Response`."""
        body = prepared.body or b""

        if isinstance(body, str):
            # can be a string for `application/x-www-form-urlencoded`
            body = body.encode("utf-8")

        # these values have `str` type at this point
        uri = cast(str, prepared.url)
        method = cast(str, prepared.method)
        return cls(
            uri=uri,
            method=method,
            headers={key: [value] for (key, value) in prepared.headers.items()},
            body=base64.b64encode(body).decode(),
        ) 
Example #3
Source File: urls.py    From django-htk with MIT License 6 votes vote down vote up
def build_url_with_query_params(base_url, query_params):
    """Builds a URL with GET query parameters `query_params`
    """
    try:
        r = requests.PreparedRequest()
        r.prepare_url(base_url, query_params)
        url = r.url
    except requests.exceptions.MissingSchema:
        # `base_url` isn't a full URL, use another placeholder just to generate the query portion
        r = requests.PreparedRequest()
        r.prepare_url('http://hacktoolkit.com', query_params)
        temp_url = r.url
        url_parts = temp_url.split('?')
        query = url_parts[1] if len(url_parts) == 2 else None
        url = '%s?%s' % (base_url, query,)

    return url 
Example #4
Source File: http.py    From flex with MIT License 6 votes vote down vote up
def _normalize_requests_request(request):
    import requests

    if not isinstance(request, (requests.Request, requests.PreparedRequest)):
        raise TypeError("Cannot normalize this request object")

    url = request.url
    method = request.method.lower()
    content_type = request.headers.get('Content-Type')
    body = request.body

    return Request(
        url=url,
        body=body,
        method=method,
        content_type=content_type,
        request=request,
        headers=request.headers,
    ) 
Example #5
Source File: serializer.py    From ws-backend-community with GNU General Public License v3.0 6 votes vote down vote up
def decode(self, o):
        """
        Decode the contents of the given JSON dictionary as an object used by Web Sight.
        :param o: The JSON dictionary to process.
        :return: The contents of the given JSON dictionary deserialized into an object.
        """
        from lib.arin.response import BaseArinResponse
        from ..geolocation import IpGeolocation
        if "__class_type" not in o:
            raise UnsupportedDeserializationError("No class type specified in JSON dictionary: %s" % o)
        class_type = o["__class_type"]
        deserialization_class = get_class_from_import_string(class_type)
        if deserialization_class == PreparedRequest:
            return self.__deserialize_requests_prepared_request(o)
        elif deserialization_class == Response:
            return self.__deserialize_requests_response(o)
        elif issubclass(deserialization_class, BaseArinResponse):
            return self.__deserialize_arin_response(o)
        elif deserialization_class == IpGeolocation:
            return self.__deserialize_ip_geolocation(o)
        else:
            raise UnsupportedDeserializationError(
                "Class %s does not have a deserialization method."
                % deserialization_class
            ) 
Example #6
Source File: serializer.py    From ws-backend-community with GNU General Public License v3.0 6 votes vote down vote up
def encode(self, o):
        """
        Encode the contents of the given object into JSON.
        :param o: The object to process.
        :return: The contents of the given object in JSON format.
        """
        from lib.arin.response import BaseArinResponse
        from ..geolocation import IpGeolocation
        if isinstance(o, PreparedRequest):
            return self.__serialize_requests_prepared_request(o)
        elif isinstance(o, Response):
            return self.__serialize_requests_response(o)
        elif isinstance(o, BaseArinResponse):
            return self.__serialize_arin_response(o)
        elif isinstance(o, IpGeolocation):
            return self.__serialize_ip_geolocation(o)
        else:
            return super(WsSerializableJSONEncoder, self).encode(o)

    # Protected Methods

    # Private Methods 
Example #7
Source File: transport.py    From amundsenmetadatalibrary with Apache License 2.0 6 votes vote down vote up
def _make_extra_headers(self, url: str, headers: Mapping[str, Any]) -> Mapping[str, Any]:
        """
        Returns the headers we should pass for this request.  This includes both the AWS v4 signature
        authentication ones as well as the Sec-WebSocket-* ones (which might vary and mess up the signatures used in
        authentication)
        """
        raw_request: bytes = self._get_raw_request_for(url=url, header=headers, **self.extra_websocket_options)
        request: PreparedRequest = self._parse_raw_request(raw_request)
        before_auth = set([k.lower() for k in request.headers.keys()])
        # we're always supposed to exclude these but AWS4Auth will include them with include_hdrs='*', so just delete
        # from the fake PreparedRequest we pass to AWS4Auth
        for k in set(request.headers.keys()):
            if k.lower() in ('connection', 'x-amzn-trace-id'):
                del request.headers[k]
        # usually mutates request (contract is to return it though, so cover our bases)
        request = self.aws4auth(request)
        # keep header if added by websocket client or aws4auth
        extra_headers = dict()
        for k, v in request.headers.items():
            if k.lower() not in before_auth or k.lower().startswith('sec-websocket'):
                extra_headers[k] = v
        return extra_headers 
Example #8
Source File: common.py    From pyvera with GNU General Public License v2.0 6 votes vote down vote up
def new_instance(self, api_data: VeraApiData) -> VeraControllerData:
        """Create new instance of controller."""
        base_url = "http://127.0.0.1:123"

        def callback(req: requests.PreparedRequest) -> ResponsesResponse:
            nonlocal api_data
            return handle_request(req, api_data)

        self.rsps.add_callback(
            method=responses.GET,
            url=re.compile(f"{base_url}/data_request?.*"),
            callback=callback,
            content_type="application/json",
        )

        controller = VeraController("http://127.0.0.1:123")
        controller.data_request({"id": "sdata"})
        controller.start()

        # Stop the controller after the test stops and fixture is torn down.
        self.pytest_req.addfinalizer(controller.stop)

        return VeraControllerData(api_data=api_data, controller=controller) 
Example #9
Source File: exceptions.py    From webexteamssdk with MIT License 5 votes vote down vote up
def __init__(self, response):
        assert isinstance(response, requests.Response)

        # Extended exception attributes
        self.response = response
        """The :class:`requests.Response` object returned from the API call."""

        self.request = self.response.request
        """The :class:`requests.PreparedRequest` of the API call."""

        self.status_code = self.response.status_code
        """The HTTP status code from the API response."""

        self.status = self.response.reason
        """The HTTP status from the API response."""

        self.details = None
        """The parsed JSON details from the API response."""
        if "application/json" in \
                self.response.headers.get("Content-Type", "").lower():
            try:
                self.details = self.response.json()
            except ValueError:
                logger.warning("Error parsing JSON response body")

        self.message = self.details.get("message") if self.details else None
        """The error message from the parsed API response."""

        self.description = RESPONSE_CODES.get(self.status_code)
        """A description of the HTTP Response Code from the API docs."""

        super(ApiError, self).__init__(
            "[{status_code}]{status} - {message}".format(
                status_code=self.status_code,
                status=" " + self.status if self.status else "",
                message=self.message or self.description or "Unknown Error",
            )
        ) 
Example #10
Source File: serializer.py    From ws-backend-community with GNU General Public License v3.0 5 votes vote down vote up
def get_supported_serialization_classes():
    """
    Get a list of the classes that are currently supported for serialization.
    :return: A list of the classes that are currently supported for serialization.
    """
    from lib.arin.response import BaseArinResponse
    from ..geolocation import IpGeolocation
    return [
        PreparedRequest,
        Response,
        BaseArinResponse,
        IpGeolocation,
    ] 
Example #11
Source File: serializer.py    From ws-backend-community with GNU General Public License v3.0 5 votes vote down vote up
def __deserialize_requests_prepared_request(self, to_deserialize):
        """
        Create and return a PreparedRequest based on the contents of the given dictionary.
        :param to_deserialize: The dictionary to create the PreparedRequest from.
        :return: A PreparedRequest created by the contents of to_deserialize.
        """
        to_return = PreparedRequest()
        to_return.method = to_deserialize["method"]
        to_return.url = to_deserialize["url"]
        to_return.headers = to_deserialize["headers"]
        to_return.body = to_deserialize["body"]
        return to_return 
Example #12
Source File: serializer.py    From ws-backend-community with GNU General Public License v3.0 5 votes vote down vote up
def __serialize_requests_prepared_request(self, to_serialize):
        """
        Serialize the contents of the given requests library request object to a JSON dictionary. A request is
        populated by the requests session object as follows:

        def copy(self):
            p = PreparedRequest()
            p.method = self.method
            p.url = self.url
            p.headers = self.headers.copy() if self.headers is not None else None
            p._cookies = _copy_cookie_jar(self._cookies)
            p.body = self.body
            p.hooks = self.hooks
            p._body_position = self._body_position
            return p

        :param to_serialize: The request object to serialize.
        :return: A JSON object representing the contents of the given request.
        """
        return {
            "method": to_serialize.method,
            "url": to_serialize.url,
            "headers": dict(to_serialize.headers),
            "body": to_serialize.body,
            "__class_type": get_import_path_for_type(to_serialize),
        } 
Example #13
Source File: test_cli.py    From spectacles with MIT License 5 votes vote down vote up
def test_handle_exceptions_looker_error_should_log_response_and_status(caplog):
    caplog.set_level(logging.DEBUG)
    response = Mock(spec=requests.Response)
    response.request = Mock(spec=requests.PreparedRequest)
    response.request.url = "https://api.looker.com"
    response.request.method = "GET"
    response.json.return_value = {
        "message": "Not found",
        "documentation_url": "http://docs.looker.com/",
    }
    status = 404

    @handle_exceptions
    def raise_exception():
        raise LookerApiError(
            name="exception-name",
            title="An exception occurred.",
            detail="Couldn't handle the truth. Please try again.",
            status=status,
            response=response,
        )

    with pytest.raises(SystemExit) as pytest_error:
        raise_exception()
    captured = "\n".join(record.msg for record in caplog.records)
    assert str(status) in captured
    assert '"message": "Not found"' in captured
    assert '"documentation_url": "http://docs.looker.com/"' in captured
    assert pytest_error.value.code == 101 
Example #14
Source File: test_dev_hose_auth.py    From fiaas-deploy-daemon with Apache License 2.0 5 votes vote down vote up
def mock_request(self):
        r = mock.create_autospec(requests.PreparedRequest(), spec_set=True, instance=True)
        r.body = "payload"
        r.path_url = "uri"
        r.headers = {}
        return r 
Example #15
Source File: test_interfaces.py    From ckanext-extractor with GNU Affero General Public License v3.0 5 votes vote down vote up
def extractor_before_request(self, request):
        self.called += 1
        assert_true(isinstance(request, requests.PreparedRequest))
        request.url = 'http://test-url.example.com/file.pdf'
        return request 
Example #16
Source File: test_service.py    From oslo.vmware with Apache License 2.0 5 votes vote down vote up
def test_send_with_local_file_url(self, get_size_mock):
        transport = service.RequestsTransport()

        url = 'file:///foo'
        request = requests.PreparedRequest()
        request.url = url

        data = b"Hello World"
        get_size_mock.return_value = len(data)

        def read_mock():
            return data

        builtin_open = 'builtins.open'
        open_mock = mock.MagicMock(name='file_handle',
                                   spec=open)
        file_spec = list(set(dir(io.TextIOWrapper)).union(
            set(dir(io.BytesIO))))

        file_handle = mock.MagicMock(spec=file_spec)
        file_handle.write.return_value = None
        file_handle.__enter__.return_value = file_handle
        file_handle.read.side_effect = read_mock
        open_mock.return_value = file_handle

        with mock.patch(builtin_open, open_mock, create=True):
            resp = transport.session.send(request)
            self.assertEqual(data, resp.content) 
Example #17
Source File: registry_test.py    From freight_forwarder with MIT License 5 votes vote down vote up
def create_response_object(url, status_code, content=None):
    """
    The function generates a mock object that is properly formatted for the RegistryException and validates the input

    :param url: url to pass through for the mock request object
    :param status_code: status code to append to the response object
    :param content: **required** if not provided, this attribute will be blocked
    :return: Parent Mock: request.Reponse Child Mock: request - requests.PreparedRequest
    """
    if not isinstance(url, six.string_types):
        raise(TypeError("incorrect type provided for url"))

    if not isinstance(status_code, six.integer_types):
        raise(TypeError("incorrect type provided for http status code"))

    mock_object_request = mock.MagicMock(spec=requests.PreparedRequest, url=url)
    mock_object_response = mock.MagicMock(spec=requests.Response, request=mock_object_request)
    mock_object_response.status_code = status_code

    if content:
        mock_object_response.content = content
    else:
        # this blocks the content attribute from being present
        del mock_object_response.content

    return mock_object_response 
Example #18
Source File: utils_test.py    From freight_forwarder with MIT License 5 votes vote down vote up
def setUp(self):
        # Patch sys.stdout for stream
        self.patch_utils_stdout = mock.patch("freight_forwarder.utils.utils.stdout", name="utils_sys")
        self.mock_utils_stdout = self.patch_utils_stdout.start()

        # Patch normalized_values
        self.patch_utils_normalize_keys = mock.patch("freight_forwarder.utils.utils.normalize_keys",
                                                     name="utils_normalize_keys")
        self.mock_utils_normalize_keys = self.patch_utils_normalize_keys.start()

        # Patch Display Error
        self.patch_display_error = mock.patch("freight_forwarder.utils.utils._display_error",
                                              name="mock_display_error")
        self.mock_display_error = self.patch_display_error.start()

        # Patch Display Progress
        self.patch_display_progress = mock.patch("freight_forwarder.utils.utils._display_progress",
                                                 name="mock_display_error")
        self.mock_display_progress = self.patch_display_progress.start()

        # Patch Display Status
        self.patch_display_status = mock.patch("freight_forwarder.utils.utils._display_status",
                                               name="mock_display_status")
        self.mock_display_status = self.patch_display_status.start()

        # Patch Display Stream
        self.patch_display_stream = mock.patch("freight_forwarder.utils.utils._display_stream",
                                               name="mock_display_stream")
        self.mock_display_stream = self.patch_display_stream.start()

        # Mock Response Object
        self.mock_object_request = mock.MagicMock(spec=requests.PreparedRequest, url="https://docker-host.test.io:2376")
        self.mock_object_response = mock.MagicMock(spec=requests.Response, request=self.mock_object_request)

        self.mock_object_response.status_code = 404 
Example #19
Source File: RestService.py    From tm1py with MIT License 5 votes vote down vote up
def build_response_from_raw_bytes(data: bytes) -> Response:
        urllib_response = RestService.urllib3_response_from_bytes(data)

        adapter = HTTPAdapter()
        requests_response = adapter.build_response(requests.PreparedRequest(), urllib_response)
        # actual content of response needs to be set explicitly
        requests_response._content = urllib_response.data

        return requests_response 
Example #20
Source File: auth.py    From python-jss with GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, r):  # type: (requests.PreparedRequest) -> requests.PreparedRequest
        if self.expires is not None and self.expires < datetime.now():
            logger.debug("Token expiry has passed, fetching a new token.")
            self._get_token()

        r.headers['Authorization'] = 'jamf-token {}'.format(self.token)
        r.register_hook('response', self.handle_401)
        return r 
Example #21
Source File: api.py    From django-htk with MIT License 5 votes vote down vote up
def get_home_worth_url(self, property_id, listing_id=None):
        """https://www.redfin.com/what-is-my-home-worth?propertyId={property_id}&listingId={listing_id}
        """
        base_url = 'https://www.redfin.com/what-is-my-home-worth'
        params = {
            'propertyId' : property_id,
            'listingId' : listing_id,
        }
        url = build_url_with_query_params(base_url, params)
        r = requests.PreparedRequest()
        r.prepare_url(base_url, params)
        return r.url 
Example #22
Source File: test_auth.py    From msrest-for-python with MIT License 5 votes vote down vote up
def test_token_auth(self):

        token = {
            'access_token': '123456789'
        }
        auth = OAuthTokenAuthentication("client_id", token)
        session = auth.signed_session()

        request = PreparedRequest()
        request.prepare("GET", "https://example.org")
        session.auth(request)
        assert request.headers == {'Authorization': 'Bearer 123456789'} 
Example #23
Source File: transport.py    From amundsenmetadatalibrary with Apache License 2.0 5 votes vote down vote up
def _parse_raw_request(cls, raw_request: bytes) -> PreparedRequest:
        """
        ok, this is kind of janky, but AWS4Auth is meant to work with requests, so expects a PreparedRequest
        """
        body: Optional[str] = None
        headers, body = raw_request.decode('utf-8').split('\r\n\r\n', 1)
        # strip the trailing \r\n if present
        if len(body) == 0:
            body = None
        elif body.endswith('\r\n'):
            body = body[:-2]
        # hi!  if you get here looking for folded headers, that's obsolete and we ought not be generating them
        method_et_al, headers = headers.split('\r\n', 1)
        headers_as_dict: Mapping[str, str] = \
            dict([(k.strip(), v.strip()) for k, v in [h.split(':', 1) for h in headers.split('\r\n')]])
        # this is a little janky, really should be one or more spaces
        method, path_et_al, version = method_et_al.split(' ', 2)
        # this is very sketchy looking but I promise that we don't care about the host, port, or scheme here
        url = 'https://nope/' + path_et_al
        req = PreparedRequest()
        req.prepare_method(method)
        req.prepare_url(url, {})
        req.prepare_headers(headers_as_dict)
        req.prepare_body(data=body, files=None)
        # don't req.prepare_content_length, we already had that in headers surely
        return req 
Example #24
Source File: common.py    From pyvera with GNU General Public License v2.0 5 votes vote down vote up
def handle_request(
    req: requests.PreparedRequest, api_data: VeraApiData
) -> ResponsesResponse:
    """Handle a request for data from the controller."""
    url_parts = urlparse(req.url)
    qs_parts: dict = parse_qs(url_parts.query)
    payload = {}
    for key, value in qs_parts.items():
        payload[key] = value[0]

    payload_id = payload.get("id")

    response: ResponsesResponse = (200, {}, "")
    if payload_id == "sdata":
        response = 200, {}, json.dumps(api_data.sdata)
    if payload_id == "status":
        response = 200, {}, json.dumps(api_data.status)
    if payload_id == "lu_sdata":
        response = 200, {}, json.dumps(api_data.lu_sdata)
    if payload_id == "action":
        response = 200, {}, json.dumps({})
    if payload_id == "variableget":
        response = handle_variable_get(payload, api_data)
    if payload_id == "lu_action":
        response = handle_lu_action(payload, api_data)

    return response 
Example #25
Source File: test_jsonrpc.py    From manila with Apache License 2.0 5 votes vote down vote up
def fake_response(self, method, path, payload, code, content):
        request = requests.PreparedRequest()
        request.method = method
        request.url = self.proxy.url(path)
        request.headers = {'Content-Type': 'application/json'}
        request.body = None
        if method in ['get', 'delete']:
            request.params = payload
        elif method in ['put', 'post']:
            request.data = json.dumps(payload)
        response = requests.Response()
        response.request = request
        response.status_code = code
        response._content = json.dumps(content) if content else ''
        return response 
Example #26
Source File: FeedRecordedFuture.py    From content with MIT License 5 votes vote down vote up
def _build_request(self, service, indicator_type):
        """Builds the request for the Recorded Future feed.
        Args:
            service (str): The service from recorded future. Can be 'connectApi' or 'fusion'
            indicator_type (str) The indicator type. Can be 'domain', 'ip', 'hash' or 'url'

        Returns:
            requests.PreparedRequest: The prepared request which will be sent to the server
        """
        if service == 'connectApi':
            if self.risk_rule is None:
                url = self.BASE_URL + indicator_type + '/risklist'
            else:
                url = self.BASE_URL + indicator_type + '/risklist?list=' + self.risk_rule

            response = requests.Request(
                'GET',
                url,
                headers=self.headers,
                params=self.PARAMS
            )

        elif service == 'fusion':
            url = self.BASE_URL + 'fusion/files/?path='
            if self.fusion_file_path is None:
                fusion_path = '/public/risklists/default_' + indicator_type + '_risklist.csv'
            else:
                fusion_path = self.fusion_file_path

            fusion_path = urllib.parse.quote_plus(fusion_path)
            response = requests.Request('GET',
                                        url + fusion_path,
                                        headers=self.headers,
                                        params=self.PARAMS)
        return response.prepare() 
Example #27
Source File: rss_proxy.py    From rssant with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request_by_proxy(self, method, url, timeout=None, **kwargs) -> requests.Response:
        if not self.has_rss_proxy:
            raise ValueError("rss_proxy_url not provided")
        if timeout is None:
            timeout = _DEFAULT_TIMEOUT
        request: requests.PreparedRequest
        request = requests.Request(method, url, **kwargs).prepare()
        request_body = None
        if request.body:
            if isinstance(request.body, bytes):
                request_body = request.body.decode('utf-8')
            elif isinstance(request.body, str):
                request_body = request.body
            else:
                msg = f'not support request body type {type(request.body).__name__}'
                raise ValueError(msg)
        headers = dict(request.headers)
        headers['user-agent'] = _DEFAULT_USER_AGENT
        proxy_data = {
            'method': request.method,
            'token': self.rss_proxy_token,
            'url': request.url,
            'headers': headers,
            'body': request_body,
        }
        response = requests.post(self.rss_proxy_url, json=proxy_data, timeout=timeout)
        response.close()
        proxy_status = response.headers.get('x-rss-proxy-status', None)
        if response.status_code != 200 or proxy_status == 'ERROR':
            msg = 'status={} {}'.format(response.status_code, response.text)
            raise RSSProxyClientError(msg)
        response.status_code = int(proxy_status)
        return response 
Example #28
Source File: base.py    From webhooks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_signature(self, payload, secret):
        if not isinstance(secret,bytes):
            secret = secret.encode('utf-8')
        if not isinstance(payload,string_types):
            # Data will be forms encoded
            payload = requests.PreparedRequest()._encode_params(payload)
        hmac = SHA256.new(secret)
        if not isinstance(payload,bytes):
            payload = payload.encode('utf-8')
        hmac.update(payload)
        return 'sha256=' + hmac.hexdigest() 
Example #29
Source File: web.py    From CloudBot with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, request: PreparedRequest, message: str):
        super().__init__(message)
        self.request = request 
Example #30
Source File: cassettes.py    From schemathesis with MIT License 5 votes vote down vote up
def get_prepared_request(data: Dict[str, Any]) -> requests.PreparedRequest:
    """Create a `requests.PreparedRequest` from a serialized one."""
    prepared = requests.PreparedRequest()
    prepared.method = data["method"]
    prepared.url = data["uri"]
    prepared._cookies = RequestsCookieJar()  # type: ignore
    encoded = data["body"]["base64_string"]
    if encoded:
        prepared.body = base64.b64decode(encoded)
    # There is always 1 value in a request
    headers = [(key, value[0]) for key, value in data["headers"].items()]
    prepared.headers = CaseInsensitiveDict(headers)
    return prepared