diff --git a/overpy/__init__.py b/overpy/__init__.py index 53d8a10..fc8efee 100644 --- a/overpy/__init__.py +++ b/overpy/__init__.py @@ -1,7 +1,7 @@ from datetime import datetime from decimal import Decimal from functools import partial -from urllib.request import urlopen +from urllib.request import urlopen, Request from urllib.error import HTTPError from xml.sax import handler, make_parser import xml.etree.ElementTree @@ -64,6 +64,7 @@ class Overpass: :param xml_parser: The xml parser to use :param max_retry_count: Max number of retries (Default: default_max_retry_count) :param retry_timeout: Time to wait between tries (Default: default_retry_timeout) + :param headers: Dict of headers to send with the request """ #: Global max number of retries (Default: 0) @@ -75,18 +76,25 @@ class Overpass: #: Default URL of the Overpass server default_url: ClassVar[str] = "http://overpass-api.de/api/interpreter" + default_headers: ClassVar[Dict[str, str]] = {} + def __init__( self, url: Optional[str] = None, xml_parser: int = XML_PARSER_SAX, max_retry_count: int = None, - retry_timeout: float = None): + retry_timeout: float = None, + headers: dict[str, str] = None): #: URL to use for this instance self.url = self.default_url if url is not None: self.url = url + self.headers = self.default_headers + if headers is not None: + self.headers = headers + self._regex_extract_error_msg = re.compile(br"\(?P\") self._regex_remove_tag = re.compile(b"<[^>]*?>") @@ -140,7 +148,8 @@ def query(self, query: Union[bytes, str]) -> "Result": response = b"" try: - with urlopen(self.url, query) as f: + req = Request(self.url, data=query, headers=self.headers) + with urlopen(req) as f: response = f.read() except HTTPError as exc: f = exc