# Path: cameo/models/webmodels.py
# class NotFoundException(Exception):
#     def __init__(self, type, index, *args, **kwargs):
#         message = "Could not retrieve %s for entry with index %i" % (type, index)
#         Exception.__init__(self, message, *args, **kwargs)
# 
# def get_sbml_file(index, host="http://darwin.di.uminho.pt/models"):
#     temp = tempfile.NamedTemporaryFile(delete=False)
#     uri = host + "/models/%i.sbml" % index
#     try:
#         response = requests.get(uri)
#     except requests.ConnectionError as e:
#         logger.error("Cannot reach {}. Are you sure that you are connected to the internet?".format(host))
#         raise e
#     if response.ok:
# 
#         temp.write(response.text.encode('utf-8'))
#         temp.flush()
#         return temp
#     else:
#         raise NotFoundException("sbml", index)
# 
# def index_models_minho(host="http://darwin.di.uminho.pt/models"):
#     """
#     Retrieves a summary of all models in the database.
# 
#     Parameters
#     ----------
#     host: the service host (optional, default: http://darwin.di.uminho.pt/models)
# 
#     Returns
#     -------
#     pandas.DataFrame
#         summary of the models in the database
#     """
#     uri = host + "/models.json"
#     try:
#         response = requests.get(uri, timeout=3)
#     except requests.ConnectionError as e:
#         logger.error("Cannot reach %s. Are you sure that you are connected to the internet?" % host)
#         raise e
#     if response.ok:
#         try:
#             json = response.json()
#         except Exception as e:
#             logger.error('No json could be decoded from server response coming from {}.'.format(host))
#             raise e
#         else:
#             index = DataFrame(json, columns=["id", "name", "doi", "author",
#                                              "year", "formats", "organism",
#                                              "taxonomy", "optflux_validated"])
#             index.columns = ["id", "name", "doi", "author", "year", "formats", "organism", "taxonomy", "validated"]
#             return index
#     else:
#         raise Exception("Could not index available models. %s returned status code %d" % (host, response.status_code))


from tempfile import _TemporaryFileWrapper
from pandas import DataFrame
from cameo.models.webmodels import (NotFoundException, get_sbml_file,
                                    index_models_minho)
import pytest
import requests
# Copyright 2015 Novo Nordisk Foundation Center for Biosustainability, DTU.

# 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


# See the License for the specific language governing permissions and
# limitations under the License.

# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.



class TestWebModels:
    def test_invalid_host(self):
        with pytest.raises(requests.ConnectionError):
            index_models_minho(host="http://blabla")
        with pytest.raises(requests.ConnectionError):
            get_sbml_file(1, host="http://blabla")

    @pytest.mark.skipif(True, reason='too slow for testing')
    def test_index(self):
        try:

# Path: cameo/models/webmodels.py
# class NotFoundException(Exception):
#     def __init__(self, type, index, *args, **kwargs):
#         message = "Could not retrieve %s for entry with index %i" % (type, index)
#         Exception.__init__(self, message, *args, **kwargs)
# 
# def get_sbml_file(index, host="http://darwin.di.uminho.pt/models"):
#     temp = tempfile.NamedTemporaryFile(delete=False)
#     uri = host + "/models/%i.sbml" % index
#     try:
#         response = requests.get(uri)
#     except requests.ConnectionError as e:
#         logger.error("Cannot reach {}. Are you sure that you are connected to the internet?".format(host))
#         raise e
#     if response.ok:
# 
#         temp.write(response.text.encode('utf-8'))
#         temp.flush()
#         return temp
#     else:
#         raise NotFoundException("sbml", index)
# 
# def index_models_minho(host="http://darwin.di.uminho.pt/models"):
#     """
#     Retrieves a summary of all models in the database.
# 
#     Parameters
#     ----------
#     host: the service host (optional, default: http://darwin.di.uminho.pt/models)
# 
#     Returns
#     -------
#     pandas.DataFrame
#         summary of the models in the database
#     """
#     uri = host + "/models.json"
#     try:
#         response = requests.get(uri, timeout=3)
#     except requests.ConnectionError as e:
#         logger.error("Cannot reach %s. Are you sure that you are connected to the internet?" % host)
#         raise e
#     if response.ok:
#         try:
#             json = response.json()
#         except Exception as e:
#             logger.error('No json could be decoded from server response coming from {}.'.format(host))
#             raise e
#         else:
#             index = DataFrame(json, columns=["id", "name", "doi", "author",
#                                              "year", "formats", "organism",
#                                              "taxonomy", "optflux_validated"])
#             index.columns = ["id", "name", "doi", "author", "year", "formats", "organism", "taxonomy", "validated"]
#             return index
#     else:
#         raise Exception("Could not index available models. %s returned status code %d" % (host, response.status_code))


# Path: tests/test_webmodels.py
from tempfile import _TemporaryFileWrapper
from pandas import DataFrame
from cameo.models.webmodels import (NotFoundException, get_sbml_file,
                                    index_models_minho)
import pytest
import requests

# Copyright 2015 Novo Nordisk Foundation Center for Biosustainability, DTU.

# 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


# See the License for the specific language governing permissions and
# limitations under the License.

# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.



class TestWebModels:
    def test_invalid_host(self):
        with pytest.raises(requests.ConnectionError):
            index_models_minho(host="http://blabla")
        with pytest.raises(requests.ConnectionError):
            get_sbml_file(1, host="http://blabla")

    @pytest.mark.skipif(True, reason='too slow for testing')
    def test_index(self):
        try:

            index = index_models_minho()