Ejemplo n.º 1
0
    async def execute(self) -> ResponseData:
        """
        Executes request and handles response from spanreed endpoint.
        """
        params = copy.copy(self.endpoint_settings.query_params)
        params.update(self.query_params)
        for key, value in self.projection.items():
            params["project." + key] = str(value)
        convert_params_headers(params)

        if self._paging is not None:
            self._paging.offset += self._paging.offset_start
            params["paging-offset"] = str(self._paging.offset)
            params["paging-limit"] = str(self._paging.limit)

        headers = copy.copy(self.endpoint_settings.headers)
        headers.update(self.headers)
        convert_params_headers(self.headers)

        if self.mimetype_accept is not None:
            headers["Accept"] = MimeType.to_string(self.mimetype_accept)

        base_url = (f"{self.client.protocol}://{self.client.host_name}"
                    f"{self.endpoint_settings.endpoint}")
        url = base_url.format(**self.path_params)

        req_schema = self.endpoint_settings.req_schema

        try:
            data = encode_content(
                content=self.media,
                mimetype=self.mimetype_send,
                headers=headers,
                data_schema=req_schema,
                encoders=self.client._ENCODERS,
            )
        except ContentTypeUnknownBase as error:
            raise ContentTypeUnknownError(str(error), response=None)

        # allow for method to be passed in caps.
        method = self.endpoint_settings.method.lower()
        method_func = getattr(self.client.session, method)

        response = await method_func(url=url,
                                     params=params,
                                     headers=headers,
                                     data=data)

        self.executed = True

        return await handle_response_aio(
            response=response,
            valid_status_codes=self.endpoint_settings.resp_codes,
            data_schema=self.endpoint_settings.resp_schema,
            api_errors_additional=self.client.api_error_index,
            current_data_object=self.update_obj,
            data_object_updater=self.endpoint_settings.data_updater,
            decoders=self.client._DECODERS,
        )
Ejemplo n.º 2
0
    def test_from_headers(self, mimetype):
        headers = {"Content-Type": MimeType.to_string(mimetype)}
        extracted = MimeType.from_headers(headers)

        if isinstance(mimetype, MimeType):
            assert extracted is mimetype
        else:
            assert extracted == mimetype
Ejemplo n.º 3
0
    def __post_init__(self) -> None:
        self.content_type = None
        mock_text = self._text

        if self._json is not None:
            self.mock_json(self._json)
        elif self._yaml is not None:
            self.mock_yaml(self._yaml)
        elif self._bson is not None:
            self.mock_bson(self._bson)
        elif mock_text is not None:
            self.mock_text(mock_text)

        if self._content_type is not None:
            self.content_type = MimeType.to_string(
                self._content_type)  # type: ignore

        if self.content_type is not None:
            MimeType.add_to_headers(self.headers, self.content_type)

        if self._exception is not None:
            self.mock_exception(self._exception)
Ejemplo n.º 4
0
 def test_to_string_none_error(self):
     with pytest.raises(ValueError):
         MimeType.to_string(None)