Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user_agent_comment option and set to "OpenCage CLI" for CLI requests #59

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions opencage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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) 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
Expand Down Expand Up @@ -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) 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 }

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
2 changes: 1 addition & 1 deletion opencage/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.0.2'
__version__ = '3.0.3'
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