From d21272b82947f5d0f2091e00e6b592c46701a617 Mon Sep 17 00:00:00 2001 From: Samuel Scully Date: Fri, 8 Nov 2024 10:52:33 +0000 Subject: [PATCH 1/3] Add user_agent_comment option and set to "OpenCage CLI" for CLI requests --- opencage/batch.py | 4 ++-- opencage/geocoder.py | 11 +++++++++-- test/test_headers.py | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/opencage/batch.py b/opencage/batch.py index 1e3b74e..08bb040 100644 --- a/opencage/batch.py +++ b/opencage/batch.py @@ -74,7 +74,7 @@ async def geocode(self, csv_input, csv_output): async def test_request(self): try: - async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext) as geocoder: + async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext, user_agent_comment='OpenCage CLI') as geocoder: result = await geocoder.geocode_async('Kendall Sq, Cambridge, MA', raw_response=True) free = False @@ -170,7 +170,7 @@ def on_backoff(details): max_tries=self.options.retries, on_backoff=on_backoff) async def _geocode_one_address(): - async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext) as geocoder: + async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext, user_agent_comment='OpenCage CLI') as geocoder: geocoding_results = None params = { 'no_annotations': 1, **self.options.optional_api_params } diff --git a/opencage/geocoder.py b/opencage/geocoder.py index 4622ce5..b4a91d0 100644 --- a/opencage/geocoder.py +++ b/opencage/geocoder.py @@ -131,7 +131,7 @@ class OpenCageGeocode: session = None - def __init__(self, key, protocol='https', domain=DEFAULT_DOMAIN, sslcontext=None): + def __init__(self, key, protocol='https', domain=DEFAULT_DOMAIN, sslcontext=None, user_agent_comment=None): """Constructor.""" self.key = key @@ -142,6 +142,8 @@ def __init__(self, key, protocol='https', domain=DEFAULT_DOMAIN, sslcontext=None # https://docs.aiohttp.org/en/stable/client_advanced.html#ssl-control-for-tcp-sockets self.sslcontext = sslcontext + self.user_agent_comment = user_agent_comment + def __enter__(self): self.session = requests.Session() return self @@ -290,8 +292,13 @@ def _opencage_headers(self, client): client_version = aiohttp.__version__ py_version = '.'.join(str(x) for x in sys.version_info[0:3]) + + comment = '' + if self.user_agent_comment: + comment = f" ({self.user_agent_comment})" + return { - 'User-Agent': f"opencage-python/{__version__} Python/{py_version} {client}/{client_version}" + 'User-Agent': f"opencage-python/{__version__} Python/{py_version} {client}/{client_version}{comment}" } async def _opencage_async_request(self, params): diff --git a/test/test_headers.py b/test/test_headers.py index 6bbff19..b48b4a8 100644 --- a/test/test_headers.py +++ b/test/test_headers.py @@ -12,9 +12,9 @@ # reduce maximum backoff retry time from 120s to 1s os.environ['BACKOFF_MAX_TIME'] = '1' -geocoder = OpenCageGeocode('abcde') +geocoder = OpenCageGeocode('abcde', user_agent_comment='OpenCage Test') -user_agent_format = re.compile(r'^opencage-python/[\d\.]+ Python/[\d\.]+ (requests|aiohttp)/[\d\.]+$') +user_agent_format = re.compile(r'^opencage-python/[\d\.]+ Python/[\d\.]+ (requests|aiohttp)/[\d\.]+ \(OpenCage Test\)$') @httprettified def test_sync(): From b652864cee3d88aede39a37fffceddc2ba837763 Mon Sep 17 00:00:00 2001 From: Samuel Scully Date: Fri, 8 Nov 2024 11:14:46 +0000 Subject: [PATCH 2/3] Bump version --- opencage/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opencage/version.py b/opencage/version.py index da4039b..d298347 100644 --- a/opencage/version.py +++ b/opencage/version.py @@ -1 +1 @@ -__version__ = '3.0.2' +__version__ = '3.0.3' From b32a6262ac06ac985b9e934abf1caebde625f5ba Mon Sep 17 00:00:00 2001 From: Samuel Scully Date: Fri, 8 Nov 2024 11:17:29 +0000 Subject: [PATCH 3/3] Move user_agent_comment for CLI to instance variable --- opencage/batch.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/opencage/batch.py b/opencage/batch.py index 08bb040..b38f252 100644 --- a/opencage/batch.py +++ b/opencage/batch.py @@ -22,6 +22,7 @@ class OpenCageBatchGeocoder(): def __init__(self, options): self.options = options self.sslcontext = ssl.create_default_context(cafile=certifi.where()) + self.user_agent_comment = 'OpenCage CLI' self.write_counter = 1 def __call__(self, *args, **kwargs): @@ -74,7 +75,7 @@ async def geocode(self, csv_input, csv_output): async def test_request(self): try: - async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext, user_agent_comment='OpenCage CLI') as geocoder: + async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext, user_agent_comment=self.user_agent_comment) as geocoder: result = await geocoder.geocode_async('Kendall Sq, Cambridge, MA', raw_response=True) free = False @@ -170,7 +171,7 @@ def on_backoff(details): max_tries=self.options.retries, on_backoff=on_backoff) async def _geocode_one_address(): - async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext, user_agent_comment='OpenCage CLI') as geocoder: + async with OpenCageGeocode(self.options.api_key, domain=self.options.api_domain, sslcontext=self.sslcontext, user_agent_comment=self.user_agent_comment) as geocoder: geocoding_results = None params = { 'no_annotations': 1, **self.options.optional_api_params }