<reponame>zsoltn/python-otcextensions
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# import six
from openstack import exceptions
from openstack import resource


# Helper class to parse the returned object
class RecordsSpec(resource.Resource):
    data = resource.Body('data', type=str)
    explicit_hash_key = resource.Body('explicit_hash_key', type=str)
    partition_id = resource.Body('partition_id', type=str)
    partition_key = resource.Body('partition_key', type=str)
    sequence_number = resource.Body('sequence_number', type=str)
    timestamp = resource.Body('timestamp', type=int)
    timestamp_type = resource.Body('timestamp_type', type=str)


class Records(resource.Resource):
    base_path = '/records'

    # resources_key = 'streams'
    # resource_key = 'stream'

    allow_create = True
    allow_list = True
    allow_commit = True
    allow_delete = True
    allow_fetch = True
    allow_patch = True

    # Properties
    #: Stream names
    stream_name = resource.Body('stream_name', type=str)
    #: Specify whether there are more matching DIS streams to list
    records = resource.Body('records', type=list, list_type=RecordsSpec)
    failed_record_count = resource.Body('failed_record_count', type=int)
    next_partition_cursor = resource.Body('next_partition_cursor', type=str)
    partition_cursor = resource.Body('partition_cursor', type=str)

    @classmethod
    def list(cls, session, ignore_missing=True, base_path=None, **params):
        session = cls._get_session(session)

        base_path = f'records?partition-cursor={params["partition_cursor"]}'

        microversion = cls._get_microversion_for_list(session)

        data = session.get(
            base_path,
            headers={"Accept": "application/json"},
            params=None,
            microversion=microversion)
        exceptions.raise_from_response(data)
        result = data.json()
        print(result)
        if result is not None:
            return result

        if ignore_missing:
            return None
        raise exceptions.ResourceNotFound(
            "No %s found for %s" % (cls.__name__, params['name_or_id']))

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# import six
from openstack import exceptions
from openstack import resource


# Helper class to parse the returned object
class RecordsSpec(resource.Resource):
    data = resource.Body('data', type=str)
    explicit_hash_key = resource.Body('explicit_hash_key', type=str)
    partition_id = resource.Body('partition_id', type=str)
    partition_key = resource.Body('partition_key', type=str)
    sequence_number = resource.Body('sequence_number', type=str)
    timestamp = resource.Body('timestamp', type=int)
    timestamp_type = resource.Body('timestamp_type', type=str)


class Records(resource.Resource):
    base_path = '/records'

    # resources_key = 'streams'
    # resource_key = 'stream'

    allow_create = True
    allow_list = True
    allow_commit = True
    allow_delete = True
    allow_fetch = True
    allow_patch = True

    # Properties
    #: Stream names
    stream_name = resource.Body('stream_name', type=str)
    #: Specify whether there are more matching DIS streams to list
    records = resource.Body('records', type=list, list_type=RecordsSpec)
    failed_record_count = resource.Body('failed_record_count', type=int)
    next_partition_cursor = resource.Body('next_partition_cursor', type=str)
    partition_cursor = resource.Body('partition_cursor', type=str)

    @classmethod
    def list(cls, session, ignore_missing=True, base_path=None, **params):
        session = cls._get_session(session)

        base_path = f'records?partition-cursor={params["partition_cursor"]}'

        microversion = cls._get_microversion_for_list(session)

        data = session.get(
            base_path,
            headers={"Accept": "application/json"},
            params=None,
            microversion=microversion)
        exceptions.raise_from_response(data)
        result = data.json()
        print(result)
        if result is not None:
            return result

        if ignore_missing:
            return None
        raise exceptions.ResourceNotFound(
            "No %s found for %s" % (cls.__name__, params['name_or_id']))
# Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # #      http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # import six # Helper class to parse the returned object # resources_key = 'streams' # resource_key = 'stream' # Properties #: Stream names #: Specify whether there are more matching DIS streams to list