1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Helper functions for accessing URL.
 
  10 from typing import IO  # noqa
 
  12 import urllib.request as urlrequest
 
  14 from ..version import NOMINATIM_VERSION
 
  16 LOG = logging.getLogger()
 
  19 def get_url(url: str) -> str:
 
  20     """ Get the contents from the given URL and return it as a UTF-8 string.
 
  22         This version makes sure that an appropriate user agent is sent.
 
  24     headers = {"User-Agent": f"Nominatim/{NOMINATIM_VERSION!s}"}
 
  27         request = urlrequest.Request(url, headers=headers)
 
  28         with urlrequest.urlopen(request) as response:  # type: IO[bytes]
 
  29             return response.read().decode('utf-8')
 
  31         LOG.fatal('Failed to load URL: %s', url)