Django: Fetching data from an open source API

Solution 1:

Here you have some clues, how to do that in simple way:

fetch_and_save/views.py

import json
import requests


def get_public_holidays(request, year, tag):
    response = requests.get(f'https://date.nager.at/api/v2/PublicHolidays/{year}/{tag}')
    with open(f'public_holidays_{tag}_{year}.json', 'w') as outfile:
        json.dump(response.text, outfile)
    ...

fetch_and_save/urls.py

urlpatterns = [
    path('<int:year>/<str:tag>/', views.get_public_holidays)
]

Then if you add i.e. /2020/DE you will fetch the public holidays 2020 of Germany and save it to the json file with a name public_holidays_DE_2020.json.