@@ -32,13 +32,19 @@ class ReplicationServer:
3232 Replication change files allow to keep local OSM data up-to-date without
3333 downloading the full dataset again.
3434
35+ `url` contains the base URL of the replication service. This is the
36+ directory that contains the state file with the current state. If the
37+ replication service serves something other than osc.gz files, set
38+ the `diff_type` to the given file suffix.
39+
3540 ReplicationServer may be used as a context manager. In this case, it
3641 internally keeps a connection to the server making downloads faster.
3742 """
3843
3944 def __init__ (self , url : str , diff_type : str = 'osc.gz' ) -> None :
4045 self .baseurl = url
4146 self .diff_type = diff_type
47+ self .extra_request_params : Mapping [str , Any ] = dict (timeout = 60 , stream = True )
4248 self .session : Optional [requests .Session ] = None
4349
4450 def close (self ) -> None :
@@ -55,39 +61,37 @@ def __enter__(self) -> 'ReplicationServer':
5561 def __exit__ (self , exc_type : Any , exc_value : Any , traceback : Any ) -> None :
5662 self .close ()
5763
64+ def set_request_parameter (self , key : str , value : Any ) -> None :
65+ """ Set a parameter which will be handed to the requests library
66+ when calling `requests.get()`. This
67+ may be used to set custom headers, timeouts and similar parameters.
68+ See the `requests documentation <https://requests.readthedocs.io/en/latest/api/?highlight=get#requests.request>`_
69+ for possible parameters. Per default, a timeout of 60 sec is set
70+ and streaming download enabled.
71+ """
72+ self .extra_request_params [key ] = value
73+
5874 def make_request (self , url : str ) -> urlrequest .Request :
5975 headers = {"User-Agent" : f"pyosmium/{ version .pyosmium_release } " }
6076 return urlrequest .Request (url , headers = headers )
6177
6278 def open_url (self , url : urlrequest .Request ) -> Any :
6379 """ Download a resource from the given URL and return a byte sequence
6480 of the content.
65-
66- This method has no support for cookies or any special authentication
67- methods. If you need these, you have to provide your own custom URL
68- opener. Overwrite open_url() with a method that receives an
69- urlrequest.Request object and returns a ByteIO-like object or a
70- requests.Response.
71-
72- Example::
73-
74- opener = urlrequest.build_opener()
75- opener.addheaders = [('X-Fancy-Header', 'important_content')]
76-
77- svr = ReplicationServer()
78- svr.open_url = opener.open
7981 """
80- headers = dict ()
81- for h in url .header_items ():
82- headers [h [0 ]] = h [1 ]
82+ if 'headers' in self .extra_request_params :
83+ get_params = self .extra_request_params
84+ else :
85+ get_params = dict (self .extra_request_params )
86+ get_params ['headers' ] = {k : v for k ,v in url .header_items ()}
8387
8488 if self .session is not None :
85- return self .session .get (url .get_full_url (), headers = headers , stream = True )
89+ return self .session .get (url .get_full_url (), ** get_params )
8690
8791 @contextmanager
8892 def _get_url_with_session () -> Iterator [requests .Response ]:
8993 with requests .Session () as session :
90- request = session .get (url .get_full_url (), headers = headers , stream = True )
94+ request = session .get (url .get_full_url (), ** get_params )
9195 yield request
9296
9397 return _get_url_with_session ()
0 commit comments