Skip to content

Commit

Permalink
Add useragent with library/python/http client versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbscully committed Dec 9, 2023
1 parent e6df2e3 commit 75d7d1d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
3 changes: 2 additions & 1 deletion opencage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" Base module for OpenCage stuff. """

from .version import __version__

__author__ = "OpenCage GmbH"
__email__ = '[email protected]'
__version__ = '2.3.1'
21 changes: 19 additions & 2 deletions opencage/geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import collections

import os
import sys
import requests
import backoff
from .version import __version__

try:
import aiohttp
Expand Down Expand Up @@ -259,9 +261,9 @@ async def reverse_geocode_async(self, lat, lng, **kwargs):
def _opencage_request(self, params):

if self.session:
response = self.session.get(self.url, params=params)
response = self.session.get(self.url, params=params, headers=self._opencage_headers('aiohttp'))
else:
response = requests.get(self.url, params=params) # pylint: disable=missing-timeout
response = requests.get(self.url, params=params, headers=self._opencage_headers('requests')) # pylint: disable=missing-timeout

try:
response_json = response.json()
Expand Down Expand Up @@ -290,6 +292,21 @@ def _opencage_request(self, params):

return response_json

def _opencage_headers(self, client):
if client == 'requests':
client_version = requests.__version__
elif client == 'aiohttp':
client_version = aiohttp.__version__

return {
'User-Agent': 'opencage-python/%s Python/%s %s/%s' % (
__version__,
'.'.join(str(x) for x in sys.version_info[0:3]),
client,
client_version
)
}

async def _opencage_async_request(self, params):
try:
async with self.session.get(self.url, params=params, ssl=self.sslcontext) as response:
Expand Down
1 change: 1 addition & 0 deletions opencage/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '2.3.1'
30 changes: 30 additions & 0 deletions test/test_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: utf-8

from pathlib import Path

import os
import re
import httpretty

from httpretty import httprettified
from opencage.geocoder import OpenCageGeocode

# reduce maximum backoff retry time from 120s to 1s
os.environ['BACKOFF_MAX_TIME'] = '1'

geocoder = OpenCageGeocode('abcde')

user_agent_format = re.compile(r'^opencage-python/[\d\.]+ Python/[\d\.]+ (requests|aiohttp)/[\d\.]+$')

@httprettified
def test_sync():
httpretty.register_uri(
httpretty.GET,
geocoder.url,
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8")
)

geocoder.geocode("EC1M 5RF")
user_agent = httpretty.last_request().headers['User-Agent']

assert user_agent_format.match(user_agent) is not None

0 comments on commit 75d7d1d

Please sign in to comment.