Esempio n. 1
0
 def test_secondary_returned_if_fallback_on_empty(self):
     primary = geocode.Provider()
     primary.lookup = MagicMock(return_value=[])
     secondary = geocode.Provider()
     secondary.lookup = MagicMock(return_value=self.secondary_results)
     provider = ProviderWithFallback(primary,
                                     secondary,
                                     fallback_on_empty=True)
     results = provider.lookup(self.query)
     self.assertEqual(results, self.secondary_results)
Esempio n. 2
0
 def test_primary_empty_results_if_secondary_fails(self):
     primary = geocode.Provider()
     primary.lookup = MagicMock(return_value=[])
     secondary = geocode.Provider()
     secondary.lookup = MagicMock(side_effect=Exception('2'))
     provider = ProviderWithFallback(primary,
                                     secondary,
                                     fallback_on_empty=True,
                                     log_handled_exception=False)
     results = provider.lookup(self.query)
     self.assertEqual(results, [])
Esempio n. 3
0
 def test_raise_secondary_exception_if_both_fail(self):
     primary = geocode.Provider()
     primary_ex = Exception('1')
     primary.lookup = MagicMock(side_effect=primary_ex)
     secondary = geocode.Provider()
     secondary_ex = Exception('2')
     secondary.lookup = MagicMock(side_effect=secondary_ex)
     provider = ProviderWithFallback(primary,
                                     secondary,
                                     fallback_on_empty=False,
                                     log_handled_exception=False)
     with self.assertRaises(Exception) as exception_context:
         provider.lookup(self.query)
     self.assertEqual(exception_context.exception, secondary_ex)
Esempio n. 4
0
from google_maps_provider import GoogleMapsProvider
from logging_provider import ProviderWithLogging
from fallback_provider import ProviderWithFallback
from http_service import Response, json_response


with open('provider_config.json') as provider_config_file:
    provider_config = json.load(provider_config_file)

provider = ProviderWithFallback(
    primary=ProviderWithLogging(
        provider=GoogleMapsProvider(
            api_key=provider_config['google_maps']['api_key']),
        provider_name='google_maps'),

    secondary=ProviderWithLogging(
        provider=HereProvider(
            app_id=provider_config['here']['app_id'],
            app_code=provider_config['here']['app_code']),
        provider_name='here'),

    fallback_on_empty=True
)


def handle_geocode_request(request):
    address = request.get_single_param('address')
    if not address:
        response = Response.bad_request(
            'Unspecified address query parameter')
    else:
        results = provider.lookup(geocode.Query(address))