Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_matches(self):
f = utils.make_filter(foobar='barfoo')
with self.assertRaises(exceptions.NoMatchesError):
f.find(self.objects)
def errors(self):
try:
return self.json()['meta']['errors']
except ValueError as e:
raise exceptions.InvalidJsonError(self._resp) from e
except KeyError as e:
raise exceptions.MissingMetaError(self._resp) from e
def data(self):
try:
return self.json()['response']
except ValueError as e:
raise exceptions.InvalidJsonError(self._resp) from e
except KeyError as e:
try:
return self.json()['payload']
except KeyError:
raise exceptions.MissingResponseError(self._resp) from e
def check_if_ready(self):
"""Check for and fetch the results if ready."""
try:
results = self.manager.check(self.results_id)
except exceptions.ResultsNotReady as e:
self._is_ready = False
self._not_ready_exception = e
except exceptions.ResultsExpired as e:
self._is_ready = True
self._expired_exception = e
else:
failures = self.get_failed_requests(results)
members = self.get_new_members(results)
self.results = self.__class__.Results(list(members), list(failures))
self._is_ready = True
self._not_ready_exception = None
def find(self, objects):
"""Find exactly one match in the list of objects.
:param objects: objects to filter
:type objects: :class:`list`
:return: the one matching object
:raises groupy.exceptions.NoMatchesError: if no objects match
:raises groupy.exceptions.MultipleMatchesError: if multiple objects match
"""
matches = list(self.__call__(objects))
if not matches:
raise exceptions.NoMatchesError(objects, self.tests)
elif len(matches) > 1:
raise exceptions.MultipleMatchesError(objects, self.tests,
matches=matches)
return matches[0]
def _check_if_ready(self):
try:
results = self.manager.check(self.results_id)
self._is_ready = True
self._not_ready_exception = None
self._process_new_members(results)
except exceptions.ResultsNotReady as e:
self._is_ready = False
self._not_ready_exception = e
except exceptions.ResultsExpired as e:
self._is_ready = True
self._expired_exception = e
def check(self, results_id):
"""Check for results of a membership request.
:param str results_id: the ID of a membership request
:return: successfully created memberships
:rtype: :class:`list`
:raises groupy.exceptions.ResultsNotReady: if the results are not ready
:raises groupy.exceptions.ResultsExpired: if the results have expired
"""
path = 'results/{}'.format(results_id)
url = utils.urljoin(self.url, path)
response = self.session.get(url)
if response.status_code == 503:
raise exceptions.ResultsNotReady(response)
if response.status_code == 404:
raise exceptions.ResultsExpired(response)
return response.data['members']
def data(self):
try:
return self.json()['response']
except ValueError as e:
raise exceptions.InvalidJsonError(self._resp) from e
except KeyError as e:
try:
return self.json()['payload']
except KeyError:
raise exceptions.MissingResponseError(self._resp) from e
def _check_if_ready(self):
try:
results = self.manager.check(self.results_id)
self._is_ready = True
self._not_ready_exception = None
self._process_new_members(results)
except exceptions.ResultsNotReady as e:
self._is_ready = False
self._not_ready_exception = e
except exceptions.ResultsExpired as e:
self._is_ready = True
self._expired_exception = e
def request(self, *args, **kwargs):
# ensure we reraise exceptions as our own
try:
response = super().request(*args, **kwargs)
response.raise_for_status()
return Response(response)
except requests.HTTPError as e:
logger.exception('received a bad response')
raise exceptions.BadResponse(response) from e
except requests.RequestException as e:
logger.exception('could not receive a response')
raise exceptions.NoResponse(e.request) from e