[Answered ]-Nasa API pass the data so slow

1👍

NASA’s API apparently takes 8 seconds to respond for a 4-day period of data.

Your processing code takes a very short time (less than 0.01 seconds) to process that data.

There’s not much you can do about NASA’s API being slow, but you could cache the data for a given period locally, if that’s okay for your application; subsequent requests for that range would then be near-instant.

You can use e.g. Django’s cache (make sure it’s configured to be something else than the DummyCache to see an effect):

from django.core.cache import cache

@api_view(['GET'])
def getDates(request, start_date, end_date):
    cache_key = f'nasa_neo_{start_date}_{end_date}'
    json_data = cache.get(cache_key)
    if not json_data:
        response = requests.get("https://api.nasa.gov/neo/rest/v1/feed?", params={
            'api_key': api_key,
            'start_date': start_date,
            'end_date': end_date
        })
        response.raise_for_status()
        json_data = orjson.loads(response.text)
        cache.set(cache_key, json_data, timeout=86400)

    date_asteroids = ...
👤AKX

Leave a comment