# Path: marvin_python_toolbox/common/http_client.py
# class ApiClient(HttpClient):
#     """
#     Data Api client abstracttion.
#     See `scanboo_common.http_client.HttpClient` for more info.
#     """
# 
#     @property
#     def host(self):
#         # url = Configuration.get('api.url')
#         url = 'http://localhost:8000'
#         return url[:-1] if url.endswith('/') else url
# 
# class ListResultSet(object):
#     """
#     Used to encapsulate the result of requests of lists.
#     """
# 
#     def __init__(self, path, data=None, limit=50, page=1):
#         self.path = path
#         self.params = data or {}
#         self.limit = limit
#         self.page = page
# 
#         self.response = None
#         self._objects = []
# 
#         self._process()
# 
#     def __len__(self):
#         return self.response.data.get('total', 0)
# 
#     def __iter__(self):
#         more_results = True
# 
#         while more_results:
#             for item in self._objects:
#                 yield item
# 
#             next_page = self._next_page()
#             if next_page:
#                 self.page = next_page
#                 self._process()
#             else:
#                 more_results = False
# 
#     def _next_page(self):
#         new_page = None
#         if math.ceil(self.response.data['total'] / float(self.limit)) >= (self.page + 1):
#             new_page = self.page + 1
#         return new_page
# 
#     def _process(self):
#         url = api_client.url(self.path)
#         self.params.update({'page': self.page, 'per_page': self.limit})
# 
#         response = requests.get(url, params=self.params, headers=api_client.request_header())
#         response = api_client.parse_response(response)
# 
#         try:
#             self._objects = response.data['objects']
#         except TypeError:
#             raise HTTPException(response.errors)
#         self.response = response
# 
# Path: marvin_python_toolbox/common/exceptions.py
# class HTTPException(HTTPExceptionBase):
#     """
#     HTTP exception
#     """


import json
import pytest
import httpretty
from httpretty import httpretty as httpretty_object
from marvin_python_toolbox.common.http_client import ApiClient, ListResultSet
from marvin_python_toolbox.common.exceptions import HTTPException
#
# 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.




class TestHttpClient:

    @httpretty.activate
    def test_list_result_set(self):
        data = [{'id': str(n)} for n in range(100)]
        per_page = 2
        total_pages = len(data) / per_page

        def fake_items(start=0):
            httpretty.register_uri(
                httpretty.GET, "http://localhost:8000/service1/",
                body=json.dumps({
                    'objects': data[start:start + per_page],
                    'total': len(data),
                }),
                content_type="application/json",
                status=200,
            )

        fake_items(0)

# Path: marvin_python_toolbox/common/http_client.py
# class ApiClient(HttpClient):
#     """
#     Data Api client abstracttion.
#     See `scanboo_common.http_client.HttpClient` for more info.
#     """
# 
#     @property
#     def host(self):
#         # url = Configuration.get('api.url')
#         url = 'http://localhost:8000'
#         return url[:-1] if url.endswith('/') else url
# 
# class ListResultSet(object):
#     """
#     Used to encapsulate the result of requests of lists.
#     """
# 
#     def __init__(self, path, data=None, limit=50, page=1):
#         self.path = path
#         self.params = data or {}
#         self.limit = limit
#         self.page = page
# 
#         self.response = None
#         self._objects = []
# 
#         self._process()
# 
#     def __len__(self):
#         return self.response.data.get('total', 0)
# 
#     def __iter__(self):
#         more_results = True
# 
#         while more_results:
#             for item in self._objects:
#                 yield item
# 
#             next_page = self._next_page()
#             if next_page:
#                 self.page = next_page
#                 self._process()
#             else:
#                 more_results = False
# 
#     def _next_page(self):
#         new_page = None
#         if math.ceil(self.response.data['total'] / float(self.limit)) >= (self.page + 1):
#             new_page = self.page + 1
#         return new_page
# 
#     def _process(self):
#         url = api_client.url(self.path)
#         self.params.update({'page': self.page, 'per_page': self.limit})
# 
#         response = requests.get(url, params=self.params, headers=api_client.request_header())
#         response = api_client.parse_response(response)
# 
#         try:
#             self._objects = response.data['objects']
#         except TypeError:
#             raise HTTPException(response.errors)
#         self.response = response
# 
# Path: marvin_python_toolbox/common/exceptions.py
# class HTTPException(HTTPExceptionBase):
#     """
#     HTTP exception
#     """


# Path: tests/common/test_http_client.py
import json
import pytest
import httpretty
from httpretty import httpretty as httpretty_object
from marvin_python_toolbox.common.http_client import ApiClient, ListResultSet
from marvin_python_toolbox.common.exceptions import HTTPException

#
# 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.




class TestHttpClient:

    @httpretty.activate
    def test_list_result_set(self):
        data = [{'id': str(n)} for n in range(100)]
        per_page = 2
        total_pages = len(data) / per_page

        def fake_items(start=0):
            httpretty.register_uri(
                httpretty.GET, "http://localhost:8000/service1/",
                body=json.dumps({
                    'objects': data[start:start + per_page],
                    'total': len(data),
                }),
                content_type="application/json",
                status=200,
            )

        fake_items(0)

        result_set = ListResultSet(path='/service1/', limit=per_page)