Esempio n. 1
0
    def _create_client(connect_responses):
        response_iterator = iter(connect_responses)

        def _execute_http_call(self, method, url, kwargs):
            res = next(response_iterator)

            query, ordering, select = _parse_qs(url)
            if res.query:
                assert query == res.query, 'RQL query does not match.'
            if res.ordering:
                assert ordering == res.ordering, 'RQL ordering does not match.'
            if res.select:
                assert select == res.select, 'RQL select does not match.'

            mock_kwargs = {
                'match_querystring': False,
            }
            if res.count is not None:
                end = 0 if res.count == 0 else res.count - 1
                mock_kwargs['status'] = 200
                mock_kwargs['headers'] = {'Content-Range': f'items 0-{end}/{res.count}'}
                mock_kwargs['json'] = []

            if isinstance(res.value, Iterable):
                count = len(res.value)
                end = 0 if count == 0 else count - 1
                mock_kwargs['status'] = 200
                mock_kwargs['json'] = res.value
                mock_kwargs['headers'] = {
                    'Content-Range': f'items 0-{end}/{count}',
                }
            elif isinstance(res.value, dict):
                mock_kwargs['status'] = res.status or 200
                mock_kwargs['json'] = res.value
            elif res.value is None:
                if res.exception:
                    mock_kwargs['body'] = res.exception
                else:
                    mock_kwargs['status'] = res.status or 200
            else:
                mock_kwargs['status'] = res.status or 200
                mock_kwargs['body'] = str(res.value)

            with responses.RequestsMock() as rsps:
                rsps.add(
                    method.upper(),
                    url,
                    **mock_kwargs,
                )
                self.response = requests.request(method, url, **kwargs)

                if self.response.status_code >= 400:
                    self.response.raise_for_status()

        client = ConnectClient('Key', use_specs=False)
        client._execute_http_call = MethodType(_execute_http_call, client)
        return client
Esempio n. 2
0
    def _create_sync_client(connect_responses):
        response_iterator = iter(connect_responses)

        def _execute_http_call(self, method, url, kwargs):
            mock_kwargs = _mock_kwargs_generator(response_iterator, url)
            with responses.RequestsMock() as rsps:
                rsps.add(
                    method.upper(),
                    url,
                    **mock_kwargs,
                )
                self.response = requests.request(method, url, **kwargs)
                if self.response.status_code >= 400:
                    self.response.raise_for_status()

        client = ConnectClient('Key', use_specs=False)
        client._execute_http_call = MethodType(_execute_http_call, client)
        return client