Exemple #1
0
    def wrapper(self,
                url: str,
                data: str = '',
                encoding='utf-8',
                async_requests_mode: Optional[bool] = None,
                **kwargs):
        # url encoding
        url, data = self._url_and_body(url=url, data=data, encoding=encoding)
        # execute request
        try:
            # determine async_requests_mode
            if async_requests_mode is None:
                async_requests_mode = self._async_requests_mode

            if not async_requests_mode:
                response = func(self, url, data, **kwargs)

            else:
                additional_header = {'Prefer': 'respond-async'}
                http_headers = kwargs.get('headers', dict())
                http_headers.update(additional_header)
                kwargs['headers'] = http_headers
                response = func(self, url, data, **kwargs)
                self.verify_response(response=response)

                if 'Location' not in response.headers or "'" not in response.headers[
                        'Location']:
                    raise ValueError(
                        f"Failed to retrieve async_id from request {func.__name__} '{url}'"
                    )
                async_id = response.headers.get('Location').split("'")[1]

                for wait in RestService.wait_time_generator(
                        kwargs.get('timeout', self._timeout)):
                    response = self.retrieve_async_response(async_id)
                    if response.status_code in [200, 201]:
                        break
                    time.sleep(wait)

                # all wait times consumed and still no 200
                if response.status_code not in [200, 201]:
                    raise TM1pyTimeout(method=func.__name__,
                                       url=url,
                                       timeout=kwargs['timeout'])

                response = self.build_response_from_raw_bytes(response.content)

            # verify
            self.verify_response(response=response)

            # response encoding
            response.encoding = encoding
            return response
        except Timeout:
            raise TM1pyTimeout(method=func.__name__,
                               url=url,
                               timeout=kwargs['timeout'])
Exemple #2
0
 def wrapper(self, url: str, data: str = '', encoding='utf-8', **kwargs):
     # url encoding
     url, data = self._url_and_body(url=url, data=data, encoding=encoding)
     # execute request
     try:
         response = func(self, url, data, **kwargs)
         # verify
         self.verify_response(response=response)
         # response encoding
         response.encoding = encoding
         return response
     except Timeout:
         raise TM1pyTimeout(method=func.__name__,
                            url=url,
                            timeout=kwargs['timeout'])