# Path: contentful/sync_page.py
# class SyncPage(Resource):
#     """
#     API Reference: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/synchronization
#     """
# 
#     def __init__(self, item, **kwargs):
#         super(SyncPage, self).__init__(item, **kwargs)
# 
#         self.next_sync_url = item.get('nextSyncUrl', '')
#         self.next_page_url = item.get('nextPageUrl', '')
#         self.next_sync_token = self._get_sync_token()
#         self.items = self._hydrate_items()
# 
#     def next(self, client):
#         """Fetches next SyncPage
# 
#         :param client: CDA Client.
#         :return: :class:`SyncPage <SyncPage>`
#         :rtype: contentful.sync_page.SyncPage
#         """
# 
#         return client.sync({'sync_token': self.next_sync_token})
# 
#     def _get_sync_token(self):
#         url_parts = urlsplit(self.next_sync_url or self.next_page_url)
#         querystring = parse_qs(url_parts.query)
#         return querystring['sync_token'][0]
# 
#     def _hydrate_items(self):
#         from .resource_builder import ResourceBuilder
#         items = []
#         for item in self.raw.get('items', []):
#             items.append(
#                 ResourceBuilder(
#                     self.default_locale,
#                     True,
#                     item
#                 ).build()
#             )
# 
#         return items
# 
#     def __repr__(self):
#         return "<SyncPage next_sync_token='{0}'>".format(
#             self.next_sync_token
#         )


from unittest import TestCase
from contentful.sync_page import SyncPage
                        "likes": {
                            "en-US": [
                                "lasagna"
                            ]
                        },
                        "color": {
                            "en-US": "orange"
                        },
                        "birthday": {
                            "en-US": "1979-06-18T23:00:00+00:00"
                        },
                        "lifes": {
                            "en-US": 1
                        },
                        "lives": {
                            "en-US": 9
                        }
                    }
                }
            ],
            "nextSyncUrl": "https://cdn.contentful.com/spaces/cfexampleapi/environments/master/sync?sync_token=w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1wrzCrBzCqMOpZAwOOcOvCcOAwqHDv0XCiMKaOcOxZA8BJUzDr8K-wo1lNx7DnHE"
        })

        self.assertEqual(sync_page.next_sync_url,  "https://cdn.contentful.com/spaces/cfexampleapi/environments/master/sync?sync_token=w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1wrzCrBzCqMOpZAwOOcOvCcOAwqHDv0XCiMKaOcOxZA8BJUzDr8K-wo1lNx7DnHE")
        self.assertEqual(sync_page.next_sync_token, "w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1wrzCrBzCqMOpZAwOOcOvCcOAwqHDv0XCiMKaOcOxZA8BJUzDr8K-wo1lNx7DnHE")
        self.assertEqual(len(sync_page.items), 14)
        self.assertEqual(str(sync_page.items[-1]), "<Entry[cat] id='garfield'>")
        self.assertEqual(str(sync_page.items[-2]), "<Asset id='nyancat' url='//images.contentful.com/cfexampleapi/4gp6taAwW4CmSgumq2ekUm/9da0cd1936871b8d72343e895a00d611/Nyan_cat_250px_frame.png'>")

    def test__when_sync_has_multiple_pages__extracts_sync_token_from_next_page_url(self):

# Path: contentful/sync_page.py
# class SyncPage(Resource):
#     """
#     API Reference: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/synchronization
#     """
# 
#     def __init__(self, item, **kwargs):
#         super(SyncPage, self).__init__(item, **kwargs)
# 
#         self.next_sync_url = item.get('nextSyncUrl', '')
#         self.next_page_url = item.get('nextPageUrl', '')
#         self.next_sync_token = self._get_sync_token()
#         self.items = self._hydrate_items()
# 
#     def next(self, client):
#         """Fetches next SyncPage
# 
#         :param client: CDA Client.
#         :return: :class:`SyncPage <SyncPage>`
#         :rtype: contentful.sync_page.SyncPage
#         """
# 
#         return client.sync({'sync_token': self.next_sync_token})
# 
#     def _get_sync_token(self):
#         url_parts = urlsplit(self.next_sync_url or self.next_page_url)
#         querystring = parse_qs(url_parts.query)
#         return querystring['sync_token'][0]
# 
#     def _hydrate_items(self):
#         from .resource_builder import ResourceBuilder
#         items = []
#         for item in self.raw.get('items', []):
#             items.append(
#                 ResourceBuilder(
#                     self.default_locale,
#                     True,
#                     item
#                 ).build()
#             )
# 
#         return items
# 
#     def __repr__(self):
#         return "<SyncPage next_sync_token='{0}'>".format(
#             self.next_sync_token
#         )


# Path: tests/sync_page_test.py
from unittest import TestCase
from contentful.sync_page import SyncPage

                        "likes": {
                            "en-US": [
                                "lasagna"
                            ]
                        },
                        "color": {
                            "en-US": "orange"
                        },
                        "birthday": {
                            "en-US": "1979-06-18T23:00:00+00:00"
                        },
                        "lifes": {
                            "en-US": 1
                        },
                        "lives": {
                            "en-US": 9
                        }
                    }
                }
            ],
            "nextSyncUrl": "https://cdn.contentful.com/spaces/cfexampleapi/environments/master/sync?sync_token=w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1wrzCrBzCqMOpZAwOOcOvCcOAwqHDv0XCiMKaOcOxZA8BJUzDr8K-wo1lNx7DnHE"
        })

        self.assertEqual(sync_page.next_sync_url,  "https://cdn.contentful.com/spaces/cfexampleapi/environments/master/sync?sync_token=w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1wrzCrBzCqMOpZAwOOcOvCcOAwqHDv0XCiMKaOcOxZA8BJUzDr8K-wo1lNx7DnHE")
        self.assertEqual(sync_page.next_sync_token, "w5ZGw6JFwqZmVcKsE8Kow4grw45QdybCnV_Cg8OASMKpwo1UY8K8bsKFwqJrw7DDhcKnM2RDOVbDt1E-wo7CnDjChMKKGsK1wrzCrBzCqMOpZAwOOcOvCcOAwqHDv0XCiMKaOcOxZA8BJUzDr8K-wo1lNx7DnHE")
        self.assertEqual(len(sync_page.items), 14)
        self.assertEqual(str(sync_page.items[-1]), "<Entry[cat] id='garfield'>")
        self.assertEqual(str(sync_page.items[-2]), "<Asset id='nyancat' url='//images.contentful.com/cfexampleapi/4gp6taAwW4CmSgumq2ekUm/9da0cd1936871b8d72343e895a00d611/Nyan_cat_250px_frame.png'>")

    def test__when_sync_has_multiple_pages__extracts_sync_token_from_next_page_url(self):

        sync_page = SyncPage({