def test_response_to_model_for_news(self):
     """
     Test that the response is converted to a bus model
     """
     api_translator = APITranslator("news", 1)
     models = api_translator.response_to_model(NEWS_RESPONSE)
     self.assertEqual(models[0].__class__.__name__, 'NewsAPIData')
예제 #2
0
    def fetch_bikes_data():
        """ Cache bikes data """

        api_translator = APITranslator("bikes", 1)
        response = api_translator.build_api_request()
        models = api_translator.response_to_model(response)
        cache.set(CACHE_KEYS['bikes'], models)
 def test_response_to_model_for_bikes(self):
     """
     Test that the response is converted to a bikes model
     """
     api_translator = APITranslator("bikes", 1)
     models = api_translator.response_to_model(BIKES_RESPONSE)
     self.assertEqual(models[0].__class__.__name__, 'BikesAPIData')
 def test_response_to_model_for_pollution(self):
     """
     Test that the response is converted to a pollution model
     """
     api_translator = APITranslator("pollution", 1)
     models = api_translator.response_to_model(POLLUTION_RESPONSE)
     self.assertEqual(models[0].__class__.__name__, 'PollutionAPIData')
    def update_bikes_data():
        """ Update bikes data """
        api_translator = APITranslator("bikes", 1)
        response = api_translator.build_api_request()
        models = api_translator.response_to_model(response)

        for model in models:
            model.save()
    def update_pollution_data():
        """ Update pollution data """
        api_translator = APITranslator("pollution", 1)
        response = api_translator.build_api_request()
        models = api_translator.response_to_model(response)

        for model in models:
            model.save()
    def list(self, request):
        """
        List bikes API data
        """
        models = TheCacher.get_cached_bus_data()
        if not models:
            api_translator = APITranslator("bus", 1)
            response = api_translator.build_api_request()
            models = api_translator.response_to_model(response)

        json_content = self.serializer_class(models, many=True)
        return Response(json_content.data, status=200)
    def list(self, request):
        """
        Update data for all apis
        """

        api_translator = APITranslator("pollution", 1)
        response = api_translator.build_api_request()
        models = api_translator.response_to_model(response)
        for model in models:
            model.save()

        return Response(status=200)
    def list(self, request):
        """
        Method to provide API details
        """
        models = TheCacher.get_cached_pollution_data()
        if not models:
            api_translator = APITranslator("pollution", 1)
            response = api_translator.build_api_request()
            models = api_translator.response_to_model(response)

        json_content = self.serializer_class(models, many=True)

        return Response(json_content.data, status=200)
    def get_latest_news():
        """
        Get the latest news. If news is not cached fetch latest news and cache
        """

        news = cache.get(CACHE_KEYS['latest_news'])
        updated_at = cache.get(CACHE_KEYS['news_updated_at'])

        if updated_at:
            time_delta = datetime.now() - updated_at

        # If the cache exceeds 24 hours, fetch latest
        if not news or not updated_at or time_delta > timedelta(hours=24):
            api_translator = APITranslator("news", 1)
            response = api_translator.build_api_request()
            models = api_translator.response_to_model(response)

            news = models
            cache.set(CACHE_KEYS['latest_news'], news)
            cache.set(CACHE_KEYS['news_updated_at'], datetime.now())

        return news
예제 #11
0
 def fetch_pollution_data():
     """ Cache pollution data """
     api_translator = APITranslator("pollution", 1)
     response = api_translator.build_api_request()
     models = api_translator.response_to_model(response)
     cache.set(CACHE_KEYS['pollution'], models)