Skip to content

Commit

Permalink
Add user_agent_comment option and set to "OpenCage CLI" for CLI requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbscully committed Nov 8, 2024
1 parent b253462 commit d21272b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions opencage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }

Expand Down
11 changes: 9 additions & 2 deletions opencage/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions test/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit d21272b

Please sign in to comment.