예제 #1
0
 def test(self, data):
     """Test the credential endpoint."""
     response = self.connection.post(urljoin(str(self.url), 'test/'), data)
     exception = exception_from_status_code(response.status_code)
     exc_str = "%s (%s) received" % (http.responses[response.status_code],
                                     response.status_code)
     if exception:
         raise exception(exc_str, response.json())
     elif response.status_code == http.FORBIDDEN:
         raise exc.Forbidden(exc_str, response.json())
     return response
예제 #2
0
    def page_identity(self, response, request_json=None):
        """Takes a `requests.Response` and
        returns a new __item_class__ instance if the request method is not a get, or returns
           a __class__ instance if the request path is different than the caller's `endpoint`.
        """
        request_path = response.request.path_url
        if request_path == '/migrations_notran/':
            raise exc.IsMigrating(
                'You have been redirected to the migration-in-progress page.')
        request_method = response.request.method.lower()

        self.last_elapsed = response.elapsed

        if isinstance(request_json, dict) and 'ds' in request_json:
            ds = request_json.ds
        else:
            ds = None

        try:
            data = response.json()
        except ValueError as e:  # If there was no json to parse
            data = dict()
            if response.text or response.status_code not in (200, 202, 204):
                text = response.text
                if len(text) > 1024:
                    text = text[:1024] + '... <<< Truncated >>> ...'
                log.debug(
                    "Unable to parse JSON response ({0.status_code}): {1} - '{2}'"
                    .format(response, e, text))

        exc_str = "%s (%s) received" % (http.responses[response.status_code],
                                        response.status_code)

        exception = exception_from_status_code(response.status_code)
        if exception:
            raise exception(exc_str, data)

        if response.status_code in (http.OK, http.CREATED, http.ACCEPTED):

            # Not all JSON responses include a URL.  Grab it from the request
            # object, if needed.
            if 'url' in data:
                endpoint = data['url']
            else:
                endpoint = request_path

            data = objectify_response_json(response)

            if request_method in ('get', 'patch', 'put'):
                #  Update existing resource and return it
                if are_same_endpoint(self.endpoint, request_path):
                    self.json = data
                    self.r = response
                    return self

            registered_type = get_registered_page(request_path, request_method)
            return registered_type(self.connection,
                                   endpoint=endpoint,
                                   json=data,
                                   last_elapsed=response.elapsed,
                                   r=response,
                                   ds=ds)

        elif response.status_code == http.FORBIDDEN:
            if is_license_invalid(response):
                raise exc.LicenseInvalid(exc_str, data)
            elif is_license_exceeded(response):
                raise exc.LicenseExceeded(exc_str, data)
            else:
                raise exc.Forbidden(exc_str, data)

        elif response.status_code == http.BAD_REQUEST:
            if is_license_invalid(response):
                raise exc.LicenseInvalid(exc_str, data)
            if is_duplicate_error(response):
                raise exc.Duplicate(exc_str, data)
            else:
                raise exc.BadRequest(exc_str, data)
        else:
            raise exc.Unknown(exc_str, data)