Skip to content

Commit bd6d843

Browse files
authored
Better HTTP user agent + add Python 3.12 to test matrix (#53)
* Add 3.12 to test matrix * Add useragent with library/python/http client versions
1 parent 4683422 commit bd6d843

6 files changed

Lines changed: 56 additions & 5 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
pull_request:
66
schedule:
7-
- cron: '11 22 2 * *'
7+
- cron: "11 22 2 * *"
88

99
jobs:
1010
tox:
@@ -13,6 +13,7 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
py:
16+
- "3.12"
1617
- "3.11"
1718
- "3.10"
1819
- "3.9"

opencage/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Base module for OpenCage stuff. """
22

3+
from .version import __version__
4+
35
__author__ = "OpenCage GmbH"
46
__email__ = 'support@opencagedata.com'
5-
__version__ = '2.3.1'

opencage/geocoder.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import collections
66

77
import os
8+
import sys
89
import requests
910
import backoff
11+
from .version import __version__
1012

1113
try:
1214
import aiohttp
@@ -259,9 +261,9 @@ async def reverse_geocode_async(self, lat, lng, **kwargs):
259261
def _opencage_request(self, params):
260262

261263
if self.session:
262-
response = self.session.get(self.url, params=params)
264+
response = self.session.get(self.url, params=params, headers=self._opencage_headers('aiohttp'))
263265
else:
264-
response = requests.get(self.url, params=params) # pylint: disable=missing-timeout
266+
response = requests.get(self.url, params=params, headers=self._opencage_headers('requests')) # pylint: disable=missing-timeout
265267

266268
try:
267269
response_json = response.json()
@@ -290,6 +292,21 @@ def _opencage_request(self, params):
290292

291293
return response_json
292294

295+
def _opencage_headers(self, client):
296+
if client == 'requests':
297+
client_version = requests.__version__
298+
elif client == 'aiohttp':
299+
client_version = aiohttp.__version__
300+
301+
return {
302+
'User-Agent': 'opencage-python/%s Python/%s %s/%s' % (
303+
__version__,
304+
'.'.join(str(x) for x in sys.version_info[0:3]),
305+
client,
306+
client_version
307+
)
308+
}
309+
293310
async def _opencage_async_request(self, params):
294311
try:
295312
async with self.session.get(self.url, params=params, ssl=self.sslcontext) as response:

opencage/version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '2.3.1'

test/test_headers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# encoding: utf-8
2+
3+
from pathlib import Path
4+
5+
import os
6+
import re
7+
import httpretty
8+
9+
from httpretty import httprettified
10+
from opencage.geocoder import OpenCageGeocode
11+
12+
# reduce maximum backoff retry time from 120s to 1s
13+
os.environ['BACKOFF_MAX_TIME'] = '1'
14+
15+
geocoder = OpenCageGeocode('abcde')
16+
17+
user_agent_format = re.compile(r'^opencage-python/[\d\.]+ Python/[\d\.]+ (requests|aiohttp)/[\d\.]+$')
18+
19+
@httprettified
20+
def test_sync():
21+
httpretty.register_uri(
22+
httpretty.GET,
23+
geocoder.url,
24+
body=Path('test/fixtures/uk_postcode.json').read_text(encoding="utf-8")
25+
)
26+
27+
geocoder.geocode("EC1M 5RF")
28+
user_agent = httpretty.last_request().headers['User-Agent']
29+
30+
assert user_agent_format.match(user_agent) is not None

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[tox]
2-
envlist = py37,py38,py39,py310,py311,lint
2+
envlist = py37,py38,py39,py310,py311,py312,lint
33

44
[gh]
55
python =
6+
3.12 = py312
67
3.11 = py311
78
3.10 = py310
89
3.9 = py39

0 commit comments

Comments
 (0)