Skip to content

Commit 201e454

Browse files
committed
set request parameters via function
Using a function instead of a parameter in __init__ is easier to handle and makes it simple to detect if the new functionality is available.
1 parent 42d3b7b commit 201e454

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

src/osmium/replication/server.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,14 @@ class ReplicationServer:
3737
replication service serves something other than osc.gz files, set
3838
the `diff_type` to the given file suffix.
3939
40-
`extra_parameters` may be used to define additional parameters to be
41-
handed to the `requests.get()` method when downloading files. This
42-
may be used to set custom headers, timeouts and similar parameters.
43-
See the `requests documentation <https://requests.readthedocs.io/en/latest/api/?highlight=get#requests.request>`_
44-
for possible parameters. The default is to set a timeout of 60 sec
45-
and enable streaming download.
46-
4740
ReplicationServer may be used as a context manager. In this case, it
4841
internally keeps a connection to the server making downloads faster.
4942
"""
5043

51-
def __init__(self, url: str, diff_type: str = 'osc.gz',
52-
extra_request_params: Optional[Mapping[str, Any]] = None) -> None:
44+
def __init__(self, url: str, diff_type: str = 'osc.gz') -> None:
5345
self.baseurl = url
5446
self.diff_type = diff_type
55-
if extra_request_params is None:
56-
self.extra_request_params = dict(timeout=60, stream=True)
57-
else:
58-
self.extra_request_params = extra_request_params
47+
self.extra_request_params: Mapping[str, Any] = dict(timeout=60, stream=True)
5948
self.session: Optional[requests.Session] = None
6049

6150
def close(self) -> None:
@@ -72,6 +61,16 @@ def __enter__(self) -> 'ReplicationServer':
7261
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
7362
self.close()
7463

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+
7574
def make_request(self, url: str) -> urlrequest.Request:
7675
headers = {"User-Agent" : f"pyosmium/{version.pyosmium_release}"}
7776
return urlrequest.Request(url, headers=headers)

tools/pyosmium-get-changes

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cookies to the server and will save received cookies to the jar file.
2727

2828
from argparse import ArgumentParser, RawDescriptionHelpFormatter, ArgumentTypeError
2929
import datetime as dt
30-
import http.cookiejar as cookiejarlib
30+
import http.cookiejar
3131

3232
from osmium.replication import server as rserv
3333
from osmium.replication import newest_change_from_file
@@ -207,15 +207,14 @@ def main(args):
207207
or 'https://planet.osm.org/replication/minute/'
208208
logging.info("Using replication server at %s" % url)
209209

210-
extra_request_params = dict(stream=True,
211-
timeout=options.socket_timeout or None)
210+
with rserv.ReplicationServer(url) as svr:
211+
svr.set_request_parameter('timeout', options.socket_timeout or None)
212212

213-
if options.cookie is not None:
214-
cookie_jar = cookiejarlib.MozillaCookieJar(options.cookie)
215-
cookie_jar.load(options.cookie)
216-
extra_request_params['cookies'] = cookie_jar
213+
if options.cookie is not None:
214+
cookie_jar = http.cookiejar.MozillaCookieJar(options.cookie)
215+
cookie_jar.load(options.cookie)
216+
svr.set_request_parameter('cookies', cookie_jar)
217217

218-
with rserv.ReplicationServer(url, extra_request_params=extra_request_params) as svr:
219218
startseq = options.start.get_sequence(svr)
220219
if startseq is None:
221220
log.error("Cannot read state file from server. Is the URL correct?")

tools/pyosmium-up-to-date

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,15 @@ def update_from_osm_server(ts, options):
6161

6262
def update_from_custom_server(url, seq, ts, options):
6363
"""Update from a custom URL, simply using the diff sequence as is."""
64-
extra_request_params = dict(stream=True,
65-
timeout=options.socket_timeout or None)
64+
with rserv.ReplicationServer(url, "osc.gz") as svr:
65+
log.info("Using replication service at %s", url)
6666

67-
if options.cookie is not None:
68-
cookie_jar = http.cookiejar.MozillaCookieJar(options.cookie)
69-
cookie_jar.load(options.cookie)
70-
extra_request_params['cookies'] = cookie_jar
67+
svr.set_request_parameter('timeout', options.socket_timeout or None)
7168

72-
with rserv.ReplicationServer(url, "osc.gz", extra_request_params=extra_request_params) as svr:
73-
log.info("Using replication service at %s", url)
69+
if options.cookie is not None:
70+
cookie_jar = http.cookiejar.MozillaCookieJar(options.cookie)
71+
cookie_jar.load(options.cookie)
72+
svr.set_request_parameter('cookies', cookie_jar)
7473

7574
current = svr.get_state_info()
7675
if current is None:

0 commit comments

Comments
 (0)