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

Better HTTP user agent + add Python 3.12 to test matrix #53

Merged
merged 2 commits into from
Dec 9, 2023
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
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
pull_request:
schedule:
- cron: '11 22 2 * *'
- cron: "11 22 2 * *"

jobs:
tox:
Expand All @@ -13,6 +13,7 @@ jobs:
fail-fast: false
matrix:
py:
- "3.12"
- "3.11"
- "3.10"
- "3.9"
Expand Down
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
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[tox]
envlist = py37,py38,py39,py310,py311,lint
envlist = py37,py38,py39,py310,py311,py312,lint

[gh]
python =
3.12 = py312
3.11 = py311
3.10 = py310
3.9 = py39
Expand Down