def _handle_error(cls, response):
        try:
            rest_result = RestResult.deserialize(response.json())
            message = rest_result.message
        except (ValueError, KeyError):
            message = response.content

        status_code = response.status_code

        if status_code == 401:
            raise UnauthorizedException(message)

        if status_code == 403:
            raise ForbiddenException(message)

        if status_code == 404:
            raise NotFoundException(message)

        if status_code == 409:
            raise ConflictException(message)

        if status_code == 502:
            raise BadGatewayException(message)

        raise MediaPlatformException(message)
Exemple #2
0
    def deserialize(cls, data):
        # type: (dict) -> Job

        sources = [Source.deserialize(source) for source in data['sources']]
        date_created = datetime_serialization.deserialize(data['dateCreated'])
        date_updated = datetime_serialization.deserialize(data['dateUpdated'])
        callback_data = data.get('callback')
        callback = Callback.deserialize(
            callback_data) if callback_data else None
        specification = data['specification']
        if cls.specification_type:
            specification = cls.specification_type.deserialize(specification)

        result_data = data.get('result')
        if result_data:
            # todo: deserialize result payload as specific type
            result = RestResult.deserialize(result_data)
        else:
            result = None

        job = cls(data['id'], data['issuer'],
                  data['status'], specification, sources, callback,
                  data.get('flowId'), result, date_created, date_updated)
        job.type = data['type']
        return job
    def _handle_success(cls, response, payload_type):
        # type: (requests.Response, Type[Deserializable] or [Type[Deserializable]]) -> object or None
        try:
            rest_result = RestResult.deserialize(response.json())
            rest_result.raise_for_code()

            if rest_result.payload is None:
                return None

            if payload_type is None:
                raise MediaPlatformException(
                    'Unexpected payload (expected None)')

            return payload_type.deserialize(rest_result.payload)

        except (ValueError, KeyError) as e:
            raise MediaPlatformException('Bad response format', e)